-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmulterArray.js
42 lines (38 loc) · 1020 Bytes
/
multerArray.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const [multer, dotenv, path] = [
require("multer"),
require("dotenv"),
require("path")
];
const upload = multer({
storage: multer.diskStorage({
// destination: "./public/uploads/",
filename(req, file, cb) {
cb(
null,
file.originalname.replace(path.extname(file.originalname), "") +
"_" +
Date.now() +
path.extname(file.originalname)
);
}
}),
limits: { fileSize: 1024 * 1024 },
fileFilter(req, file, cb) {
checktype(file, cb);
}
}).single("banner");
let checktype = (file, cb) => {
// Allowed ext
const filetypes = /jpeg|jpg|png|gif/;
// Check ext
const extname = filetypes.test(path.extname(file.originalname).toLowerCase());
// Check mime
const mimetype = filetypes.test(file.mimetype);
if (mimetype && extname) {
return cb(null, true);
} else {
// cb("Error: Images Only!");
return res.status(406).json({success : true, error : `Error Only Images are accepted.`})
}
};
module.exports = upload;