Skip to content

Instantly share code, notes, and snippets.

@infysumanta
Last active June 6, 2023 06:43
Show Gist options
  • Save infysumanta/70a7a01fd32bfe5a05688ff174b2ce47 to your computer and use it in GitHub Desktop.
Save infysumanta/70a7a01fd32bfe5a05688ff174b2ce47 to your computer and use it in GitHub Desktop.
const multer = require("multer");
const uuidv4 = require("uuid/v4");
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, "./public/upload");
},
filename: (req, file, cb) => {
const fileName =
uuidv4() + "-" + file.originalname.toLowerCase().split(" ").join("-");
console.log(fileName);
cb(null, fileName);
},
});
exports.upload = multer({
storage: storage,
fileFilter: (req, file, cb) => {
if (
file.mimetype === "image/png" ||
file.mimetype === "image/jpg" ||
file.mimetype === "image/jpeg"
) {
cb(null, true);
} else {
cb(null, false);
return cb(new Error("Only .png, .jpg and .jpeg format allowed!"));
}
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment