обновОЧКИ

This commit is contained in:
2026-05-29 04:28:09 +06:00
parent 3c885edc46
commit 0dd9efb9fb
15 changed files with 542 additions and 44 deletions

View File

@@ -27,6 +27,7 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
},
},
async (req, reply) => {
const user = req.user!
const data = await req.file()
if (!data) {
@@ -36,6 +37,7 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
const meta = await fastify.prisma.attachment.create({
data: {
name: data.filename,
username: user.username,
mimetype: data.mimetype,
size: 0,
},
@@ -47,13 +49,25 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
const filePath = path.join(process.cwd(), 'uploads', meta.id)
let fileSize = 0
await new Promise((resolve, reject) => {
const writeStream = fs.createWriteStream(filePath)
data.file.pipe(writeStream)
data.file.on('data', (chunk) => {
fileSize += chunk.length
})
data.file.on('end', resolve)
data.file.on('error', reject)
})
await fastify.prisma.attachment.update({
where: { id: meta.id },
data: { size: fileSize },
})
// await new Promise(resolve => setTimeout(resolve, Math.random() * 10000))
return meta.id
},
)