Skip to content

Commit

Permalink
fix: wrap the entire decorator in the if statement to check if file a…
Browse files Browse the repository at this point in the history
…ctually exists, else skip all
  • Loading branch information
bjarneo committed Aug 23, 2022
1 parent f719e4b commit 7d9a8f2
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions src/server/decorators/attachment-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 } });
}
}
});
});

0 comments on commit 7d9a8f2

Please sign in to comment.