обновОЧКИ
This commit is contained in:
@@ -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
|
||||
},
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user