const multer = require("multer");
const MIME_TYPE_MAP = {
"image/png": "png",
"image/jpg": "jpg",
"image/jpeg": "jpeg",
};
const uuid = require('uuid/v1')
const fileUpload = multer({
limits:500000,
storage:multer.diskStorage({
destination: (req, file, cb) => {
cb(null, "images");
},
filename: (req, file, cb) => {
const name = file.originalname.split(" ").join("-");
const ext = MIME_TYPE_MAP[file.mimetype];
cb(null, name + "-" + uuid() + "." + ext);
},
}),
fileFilter:(req,file,cb)=>{
const isValid = MIME_TYPE_MAP[file.mimetype]
let error = isValid ? null : new Error('Invalid file type')
cb(error, true)
}
})
module.exports = fileUpload.single('image')