From f37e2ac4f332a46fbb4b9280d1500c7ccf2de4a5 Mon Sep 17 00:00:00 2001 From: amy Date: Tue, 3 Dec 2024 21:54:35 +0330 Subject: [PATCH] honestly i have no idea what i added --- package.json | 1 + pnpm-lock.yaml | 23 +++++++++++++++++ src/endpoints/fileupload.ts | 51 ++++++++++++++++++++----------------- src/main.ts | 5 +++- 4 files changed, 56 insertions(+), 24 deletions(-) diff --git a/package.json b/package.json index 6a00a34..a012f8c 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "packageManager": "pnpm@9.12.3+sha512.cce0f9de9c5a7c95bef944169cc5dfe8741abfb145078c0d508b868056848a87c81e626246cb60967cbd7fd29a6c062ef73ff840d96b3c86c40ac92cf4a813ee", "dependencies": { "@fastify/cookie": "^11.0.1", + "@fastify/cors": "^10.0.1", "@fastify/multipart": "^9.0.1", "@fastify/oauth2": "^8.1.0", "@prisma/client": "6.0.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 92c97d7..a8d829a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -11,6 +11,9 @@ importers: '@fastify/cookie': specifier: ^11.0.1 version: 11.0.1 + '@fastify/cors': + specifier: ^10.0.1 + version: 10.0.1 '@fastify/multipart': specifier: ^9.0.1 version: 9.0.1 @@ -57,6 +60,9 @@ packages: '@fastify/cookie@11.0.1': resolution: {integrity: sha512-n1Ooz4bgQ5LcOlJQboWPfsMNxIrGV0SgU85UkctdpTlCQE0mtA3rlspOPUdqk9ubiiZn053ucnia4DjTquI4/g==} + '@fastify/cors@10.0.1': + resolution: {integrity: sha512-O8JIf6448uQbOgzSkCqhClw6gFTAqrdfeA6R3fc/3gwTJGUp7gl8/3tbNB+6INuu4RmgVOq99BmvdGbtu5pgOA==} + '@fastify/deepmerge@2.0.0': resolution: {integrity: sha512-fsaybTGDyQ5KpPsplQqb9yKdCf2x/pbNpMNk8Tvp3rRz7lVcupKysH4b2ELMN2P4Hak1+UqTYdTj/u4FNV2p0g==} @@ -292,9 +298,15 @@ packages: make-error@1.3.6: resolution: {integrity: sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==} + mnemonist@0.39.8: + resolution: {integrity: sha512-vyWo2K3fjrUw8YeeZ1zF0fy6Mu59RHokURlld8ymdUPjMlD9EC9ov1/YPqTgqRvUN9nTr3Gqfz29LYAmu0PHPQ==} + ms@2.1.3: resolution: {integrity: sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==} + obliterator@2.0.4: + resolution: {integrity: sha512-lgHwxlxV1qIg1Eap7LgIeoBWIMFibOjbrYPIPJZcI1mmGAI2m3lNYpK12Y+GBdPQ0U1hRwSord7GIaawz962qQ==} + on-exit-leak-free@2.1.2: resolution: {integrity: sha512-0eJJY6hXLGf1udHwfNftBqH+g73EU4B504nZeKpz1sYRKafAghwxEJunB2O7rDZkL4PGfsMVnTXZ2EjibbqcsA==} engines: {node: '>=14.0.0'} @@ -499,6 +511,11 @@ snapshots: cookie: 1.0.2 fastify-plugin: 5.0.1 + '@fastify/cors@10.0.1': + dependencies: + fastify-plugin: 5.0.1 + mnemonist: 0.39.8 + '@fastify/deepmerge@2.0.0': {} '@fastify/error@4.0.0': {} @@ -744,8 +761,14 @@ snapshots: make-error@1.3.6: {} + mnemonist@0.39.8: + dependencies: + obliterator: 2.0.4 + ms@2.1.3: {} + obliterator@2.0.4: {} + on-exit-leak-free@2.1.2: {} peek-readable@5.3.1: {} diff --git a/src/endpoints/fileupload.ts b/src/endpoints/fileupload.ts index d9bfcfa..38e09a7 100644 --- a/src/endpoints/fileupload.ts +++ b/src/endpoints/fileupload.ts @@ -25,35 +25,40 @@ export default function (fastify: FastifyInstance, prisma: PrismaClient) { fastify.post('/api/upload', async (request, reply) => { const user = await getUserFromApiToken(request.headers.authorization, prisma); - if (Object.keys(user).length==0) return reply.status(401).send('Not Authorized'); + if (!user) return reply.status(401).send('Not Authorized'); + const data = await request.file({ limits: { - fileSize: 10 * 1024 * 1024, - } - }) - const fileName = Array.from({length: 15}, () => 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'[Math.floor(Math.random() * 62)]).join(''); + fileSize: 10 * 1024 * 1024, // 10MB limit + }, + }); + + const fileName = Array.from({ length: 15 }, () => + 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'[ + Math.floor(Math.random() * 62) + ] + ).join(''); try { const newImagePost = await prisma.filePost.create({ data: { fileName, - user: { - connect: {id: user.id}, - } - } - }) - const filePath = path.join(uploadDir, newImagePost.id) - await new Promise((resolve, reject) => { - const fileStream = fs.createWriteStream(filePath) - data.file.pipe(fileStream) - fileStream.on('finish', resolve) - fileStream.on('error', reject) - }) - reply.code(201).send(newImagePost) - } catch (error) { - fastify.log.error(error) - reply.code(500).send({error: 'Error uploading file or saving record'}) - } + user: { connect: { id: user.id } }, + }, + }); - }) + const filePath = path.join(uploadDir, newImagePost.id); + await new Promise((resolve, reject) => { + const fileStream = fs.createWriteStream(filePath); + data.file.pipe(fileStream); + fileStream.on('finish', resolve); + fileStream.on('error', reject); + }); + + reply.code(201).send(newImagePost); + } catch (error) { + fastify.log.error(error); + reply.code(500).send({ error: 'Error uploading file or saving record' }); + } + }); } \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index f1bbc30..47c383f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2,6 +2,7 @@ import Fastify, {FastifyRequest} from 'fastify' import {PrismaClient} from '@prisma/client' import fastifyMultipart from "@fastify/multipart"; import fastifyOauth2 from "@fastify/oauth2"; +import cors from '@fastify/cors' import fs from 'fs' import path from 'path' import dotenv from 'dotenv'; @@ -57,7 +58,9 @@ const fastify = Fastify({logger: true}) fastify.register(fastifyMultipart) - +await fastify.register(cors, { + origin: true +}) fastify.register(fastifyOauth2, {