Last active
June 6, 2023 06:43
-
-
Save infysumanta/70a7a01fd32bfe5a05688ff174b2ce47 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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