init commit
This commit is contained in:
25
backend/middlewares/uploadMiddleware.js
Normal file
25
backend/middlewares/uploadMiddleware.js
Normal file
@ -0,0 +1,25 @@
|
||||
const multer = require('multer');
|
||||
|
||||
// Configure storage
|
||||
const storage = multer.diskStorage({
|
||||
destination: (req, file, cb) => {
|
||||
cb(null, 'uploads/');
|
||||
},
|
||||
filename: (req, file, cb) => {
|
||||
cb(null, `${Date.now()}-${file.originalname}`);
|
||||
},
|
||||
});
|
||||
|
||||
// File filter
|
||||
const fileFilter = (req, file, cb) => {
|
||||
const allowedTypes = ['image/jpeg', 'image/png', 'image/jpg'];
|
||||
if (allowedTypes.includes(file.mimetype)) {
|
||||
cb(null, true);
|
||||
} else {
|
||||
cb(new Error('Only .jpeg, .jpg and .png formats are allowed'), false);
|
||||
}
|
||||
};
|
||||
|
||||
const upload = multer({ storage, fileFilter });
|
||||
|
||||
module.exports = upload;
|
||||
Reference in New Issue
Block a user