diff --git a/lib/imageserv.js b/lib/imageserv.js index fdf38f42..3215f3af 100644 --- a/lib/imageserv.js +++ b/lib/imageserv.js @@ -29,7 +29,7 @@ const storage = multer.diskStorage({ // multers disk storage settings const upload = multer({ storage, fileFilter(req, file, cb) { - const extension = path.extname(file.originalname); + const extension = path.extname(file.originalname).toLowerCase(); if (!allowedExtensions.includes(extension)) { const allowed = allowedExtensions.map((e) => `'${e}'`).join(', '); return cb(new Error(`Allowed extensions: ${allowed}, but '${extension}' was passed.`)); @@ -65,7 +65,7 @@ exports.uploadImage = async (req, res) => { const buffer = readChunk.sync(req.file.path, 0, 4100); const type = await FileType.fromBuffer(buffer); - const originalExtension = path.extname(req.file.originalname); + const originalExtension = path.extname(req.file.originalname).toLowerCase(); const determinedExtension = (type && type.ext ? `.${type.ext}` : 'unknown'); if (originalExtension !== determinedExtension || !allowedExtensions.includes(determinedExtension)) { diff --git a/test/api/file-upload.test.js b/test/api/file-upload.test.js index 9f2a4866..27d0b65a 100644 --- a/test/api/file-upload.test.js +++ b/test/api/file-upload.test.js @@ -113,6 +113,26 @@ describe('File upload', () => { expect(fs.existsSync(imgPath)).toEqual(true); }); + it('should upload a file if it\'s valid, but has extension in capital letters', async () => { + const res = await request({ + uri: '/single/' + event.id + '/upload', + method: 'POST', + headers: { 'X-Auth-Token': 'blablabla' }, + formData: { + head_image: fs.createReadStream('./test/assets/valid_second_image.PNG') + } + }); + + expect(res.statusCode).toEqual(200); + expect(res.body.success).toEqual(true); + expect(res.body).toHaveProperty('message'); + + const eventFromDb = await Event.findByPk(event.id); + + const imgPath = path.join(__dirname, '..', '..', config.media_dir, 'headimages', eventFromDb.image); + expect(fs.existsSync(imgPath)).toEqual(true); + }); + it('should remove the old file', async () => { // Uploading const firstRequest = await request({ diff --git a/test/assets/valid_second_image.PNG b/test/assets/valid_second_image.PNG new file mode 100644 index 00000000..cff2bd3e Binary files /dev/null and b/test/assets/valid_second_image.PNG differ