Skip to content

Commit

Permalink
add file size limit of 1 MB
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleyleal committed Jul 30, 2024
1 parent 01fb818 commit 100ea25
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions server/src/middlewares/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const storage = multer.diskStorage({

const upload = multer({
storage,
limits: { fileSize: 1 * 1024 * 1024 }, // 1MB limit
fileFilter(req, file, callback) {
if (file.mimetype == 'application/pdf') {
callback(null, true);
Expand All @@ -30,13 +31,25 @@ const upload = multer({
callback(null, false);
}
},
});
}).single('waiver');

router.post('/upload-waiver', (req, res) => {
upload(req, res, (err) => {
if (err instanceof multer.MulterError) {
if (err.code === 'LIMIT_FILE_SIZE') {
return res.status(400).send('File is too large. Maximum size is 1MB.');
}
return res.status(400).send('An error occurred while uploading the file.');
} else if (err) {
return res.status(400).send('An unknown error occurred.');
}

router.post('/upload-waiver', upload.single('waiver'), (req, res) => {
if (!req.file) {
return res.status(400).send('No file uploaded.');
}
res.status(200).send('File uploaded successfully.');
// all good
if (!req.file) {
return res.status(400).send('No file uploaded.');
}
res.status(200).send('File uploaded successfully.');
});
});

module.exports = router;
Binary file not shown.

0 comments on commit 100ea25

Please sign in to comment.