-
Notifications
You must be signed in to change notification settings - Fork 2
/
upload.js
36 lines (33 loc) · 1.11 KB
/
upload.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
const multer = require('multer');
const path = require('path');
const fileFilter = (req, file, cb) => {
// 확장자 필터링
if (
file.mimetype === "image/jpg" ||
file.mimetype === "image/gif" ||
file.mimetype === "image/jpeg"
) {
cb(null, true); // 해당 mimetype만 받겠다는 의미
} else {
// 다른 mimetype은 저장되지 않음
req.fileValidationError = "jpg,jpeg,png,gif,webp 파일만 업로드 가능합니다.";
cb(null, false);
}
};
const upload = multer({
storage: multer.diskStorage({
//폴더위치 지정
destination: (req, file, done) => {
done(null, __dirname + "/public/assets/uploads");
},
filename: (req, file, done) => {
const ext = path.extname(file.originalname);
// aaa.txt => aaa+&&+129371271654.txt
const fileName = path.basename("content_file_", ext) + Date.now() + ext;
done(null, fileName);
},
}),
fileFilter : fileFilter,
limits: { fileSize: 300 * 1024 * 1024 },
});
module.exports = { upload };