From 7d9a8f29af8a8fe7f9dad27a60d76da913069904 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bjarne=20=C3=98verli?= Date: Tue, 23 Aug 2022 22:14:44 +0200 Subject: [PATCH] fix: wrap the entire decorator in the if statement to check if file actually exists, else skip all --- src/server/decorators/attachment-upload.js | 42 +++++++++++----------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/src/server/decorators/attachment-upload.js b/src/server/decorators/attachment-upload.js index a4f48e7b..aaaeca7b 100644 --- a/src/server/decorators/attachment-upload.js +++ b/src/server/decorators/attachment-upload.js @@ -44,34 +44,32 @@ module.exports = fp(async (fastify) => { const file = await req.body.file; const { encryptionKey } = req.secret; - if (!file?.mimetype) { - done(); - } - - const fileData = await file.toBuffer(); - - const { ext, mime } = await FileType.fromBuffer(fileData); - - if (file?.filename && !acceptedFileType(mime)) { - return reply.code(415).send({ - error: `This file type "${mime}" is not supported, yet.`, - }); - } + if (file.mimetype) { + const fileData = await file.toBuffer(); - if (file?.filename) { - const byteLength = Buffer.byteLength(fileData); + const { ext, mime } = await FileType.fromBuffer(fileData); - if (byteLength > MAX_FILE_BYTES) { - return reply.code(413).send({ - error: `The file size (${prettyBytes( - byteLength - )}) exceeded our limit of ${prettyBytes(MAX_FILE_BYTES)}.`, + if (file?.filename && !acceptedFileType(mime)) { + return reply.code(415).send({ + error: `This file type "${mime}" is not supported, yet.`, }); } - const imageData = await upload(encryptionKey, fileData); + if (file?.filename) { + const byteLength = Buffer.byteLength(fileData); + + if (byteLength > MAX_FILE_BYTES) { + return reply.code(413).send({ + error: `The file size (${prettyBytes( + byteLength + )}) exceeded our limit of ${prettyBytes(MAX_FILE_BYTES)}.`, + }); + } - Object.assign(req.secret, { file: { ext, mime, key: imageData.key } }); + const imageData = await upload(encryptionKey, fileData); + + Object.assign(req.secret, { file: { ext, mime, key: imageData.key } }); + } } }); });