honestly i have no idea what i added
This commit is contained in:
parent
0756a5dfea
commit
f37e2ac4f3
4 changed files with 56 additions and 24 deletions
|
@ -14,6 +14,7 @@
|
||||||
"packageManager": "pnpm@9.12.3+sha512.cce0f9de9c5a7c95bef944169cc5dfe8741abfb145078c0d508b868056848a87c81e626246cb60967cbd7fd29a6c062ef73ff840d96b3c86c40ac92cf4a813ee",
|
"packageManager": "pnpm@9.12.3+sha512.cce0f9de9c5a7c95bef944169cc5dfe8741abfb145078c0d508b868056848a87c81e626246cb60967cbd7fd29a6c062ef73ff840d96b3c86c40ac92cf4a813ee",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@fastify/cookie": "^11.0.1",
|
"@fastify/cookie": "^11.0.1",
|
||||||
|
"@fastify/cors": "^10.0.1",
|
||||||
"@fastify/multipart": "^9.0.1",
|
"@fastify/multipart": "^9.0.1",
|
||||||
"@fastify/oauth2": "^8.1.0",
|
"@fastify/oauth2": "^8.1.0",
|
||||||
"@prisma/client": "6.0.0",
|
"@prisma/client": "6.0.0",
|
||||||
|
|
23
pnpm-lock.yaml
generated
23
pnpm-lock.yaml
generated
|
@ -11,6 +11,9 @@ importers:
|
||||||
'@fastify/cookie':
|
'@fastify/cookie':
|
||||||
specifier: ^11.0.1
|
specifier: ^11.0.1
|
||||||
version: 11.0.1
|
version: 11.0.1
|
||||||
|
'@fastify/cors':
|
||||||
|
specifier: ^10.0.1
|
||||||
|
version: 10.0.1
|
||||||
'@fastify/multipart':
|
'@fastify/multipart':
|
||||||
specifier: ^9.0.1
|
specifier: ^9.0.1
|
||||||
version: 9.0.1
|
version: 9.0.1
|
||||||
|
@ -57,6 +60,9 @@ packages:
|
||||||
'@fastify/cookie@11.0.1':
|
'@fastify/cookie@11.0.1':
|
||||||
resolution: {integrity: sha512-n1Ooz4bgQ5LcOlJQboWPfsMNxIrGV0SgU85UkctdpTlCQE0mtA3rlspOPUdqk9ubiiZn053ucnia4DjTquI4/g==}
|
resolution: {integrity: sha512-n1Ooz4bgQ5LcOlJQboWPfsMNxIrGV0SgU85UkctdpTlCQE0mtA3rlspOPUdqk9ubiiZn053ucnia4DjTquI4/g==}
|
||||||
|
|
||||||
|
'@fastify/cors@10.0.1':
|
||||||
|
resolution: {integrity: sha512-O8JIf6448uQbOgzSkCqhClw6gFTAqrdfeA6R3fc/3gwTJGUp7gl8/3tbNB+6INuu4RmgVOq99BmvdGbtu5pgOA==}
|
||||||
|
|
||||||
'@fastify/deepmerge@2.0.0':
|
'@fastify/deepmerge@2.0.0':
|
||||||
resolution: {integrity: sha512-fsaybTGDyQ5KpPsplQqb9yKdCf2x/pbNpMNk8Tvp3rRz7lVcupKysH4b2ELMN2P4Hak1+UqTYdTj/u4FNV2p0g==}
|
resolution: {integrity: sha512-fsaybTGDyQ5KpPsplQqb9yKdCf2x/pbNpMNk8Tvp3rRz7lVcupKysH4b2ELMN2P4Hak1+UqTYdTj/u4FNV2p0g==}
|
||||||
|
|
||||||
|
@ -292,9 +298,15 @@ packages:
|
||||||
make-error@1.3.6:
|
make-error@1.3.6:
|
||||||
resolution: {integrity: sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==}
|
resolution: {integrity: sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==}
|
||||||
|
|
||||||
|
mnemonist@0.39.8:
|
||||||
|
resolution: {integrity: sha512-vyWo2K3fjrUw8YeeZ1zF0fy6Mu59RHokURlld8ymdUPjMlD9EC9ov1/YPqTgqRvUN9nTr3Gqfz29LYAmu0PHPQ==}
|
||||||
|
|
||||||
ms@2.1.3:
|
ms@2.1.3:
|
||||||
resolution: {integrity: sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==}
|
resolution: {integrity: sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==}
|
||||||
|
|
||||||
|
obliterator@2.0.4:
|
||||||
|
resolution: {integrity: sha512-lgHwxlxV1qIg1Eap7LgIeoBWIMFibOjbrYPIPJZcI1mmGAI2m3lNYpK12Y+GBdPQ0U1hRwSord7GIaawz962qQ==}
|
||||||
|
|
||||||
on-exit-leak-free@2.1.2:
|
on-exit-leak-free@2.1.2:
|
||||||
resolution: {integrity: sha512-0eJJY6hXLGf1udHwfNftBqH+g73EU4B504nZeKpz1sYRKafAghwxEJunB2O7rDZkL4PGfsMVnTXZ2EjibbqcsA==}
|
resolution: {integrity: sha512-0eJJY6hXLGf1udHwfNftBqH+g73EU4B504nZeKpz1sYRKafAghwxEJunB2O7rDZkL4PGfsMVnTXZ2EjibbqcsA==}
|
||||||
engines: {node: '>=14.0.0'}
|
engines: {node: '>=14.0.0'}
|
||||||
|
@ -499,6 +511,11 @@ snapshots:
|
||||||
cookie: 1.0.2
|
cookie: 1.0.2
|
||||||
fastify-plugin: 5.0.1
|
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/deepmerge@2.0.0': {}
|
||||||
|
|
||||||
'@fastify/error@4.0.0': {}
|
'@fastify/error@4.0.0': {}
|
||||||
|
@ -744,8 +761,14 @@ snapshots:
|
||||||
|
|
||||||
make-error@1.3.6: {}
|
make-error@1.3.6: {}
|
||||||
|
|
||||||
|
mnemonist@0.39.8:
|
||||||
|
dependencies:
|
||||||
|
obliterator: 2.0.4
|
||||||
|
|
||||||
ms@2.1.3: {}
|
ms@2.1.3: {}
|
||||||
|
|
||||||
|
obliterator@2.0.4: {}
|
||||||
|
|
||||||
on-exit-leak-free@2.1.2: {}
|
on-exit-leak-free@2.1.2: {}
|
||||||
|
|
||||||
peek-readable@5.3.1: {}
|
peek-readable@5.3.1: {}
|
||||||
|
|
|
@ -25,35 +25,40 @@ export default function (fastify: FastifyInstance, prisma: PrismaClient) {
|
||||||
|
|
||||||
fastify.post('/api/upload', async (request, reply) => {
|
fastify.post('/api/upload', async (request, reply) => {
|
||||||
const user = await getUserFromApiToken(request.headers.authorization, prisma);
|
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({
|
const data = await request.file({
|
||||||
limits: {
|
limits: {
|
||||||
fileSize: 10 * 1024 * 1024,
|
fileSize: 10 * 1024 * 1024, // 10MB limit
|
||||||
}
|
},
|
||||||
})
|
});
|
||||||
const fileName = Array.from({length: 15}, () => 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'[Math.floor(Math.random() * 62)]).join('');
|
|
||||||
|
const fileName = Array.from({ length: 15 }, () =>
|
||||||
|
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'[
|
||||||
|
Math.floor(Math.random() * 62)
|
||||||
|
]
|
||||||
|
).join('');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const newImagePost = await prisma.filePost.create({
|
const newImagePost = await prisma.filePost.create({
|
||||||
data: {
|
data: {
|
||||||
fileName,
|
fileName,
|
||||||
user: {
|
user: { connect: { id: user.id } },
|
||||||
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'})
|
|
||||||
}
|
|
||||||
|
|
||||||
})
|
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' });
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
|
@ -2,6 +2,7 @@ import Fastify, {FastifyRequest} from 'fastify'
|
||||||
import {PrismaClient} from '@prisma/client'
|
import {PrismaClient} from '@prisma/client'
|
||||||
import fastifyMultipart from "@fastify/multipart";
|
import fastifyMultipart from "@fastify/multipart";
|
||||||
import fastifyOauth2 from "@fastify/oauth2";
|
import fastifyOauth2 from "@fastify/oauth2";
|
||||||
|
import cors from '@fastify/cors'
|
||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
import dotenv from 'dotenv';
|
import dotenv from 'dotenv';
|
||||||
|
@ -57,7 +58,9 @@ const fastify = Fastify({logger: true})
|
||||||
|
|
||||||
|
|
||||||
fastify.register(fastifyMultipart)
|
fastify.register(fastifyMultipart)
|
||||||
|
await fastify.register(cors, {
|
||||||
|
origin: true
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
fastify.register(fastifyOauth2, {
|
fastify.register(fastifyOauth2, {
|
||||||
|
|
Loading…
Add table
Reference in a new issue