-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuploadFile.js
81 lines (70 loc) · 2.03 KB
/
uploadFile.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const multer = require('multer');
const path = require('path');
const storageCategory = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, './public/category');
},
filename: function(req, file, cb) {
// Check file type based on its extension
const filetypes = /jpeg|jpg|png/;
const extname = filetypes.test(path.extname(file.originalname).toLowerCase());
if (extname) {
cb(null, Date.now() + "_" + Math.floor(Math.random() * 1000) + path.extname(file.originalname));
} else {
cb("Error: only .jpeg, .jpg, .png files are allowed!");
}
}
});
const uploadCategory = multer({
storage: storageCategory,
limits: {
fileSize: 1024 * 1024 * 5 // limit filesize to 5MB
},
});
const storageProduct = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, './public/products');
},
filename: function(req, file, cb) {
// Check file type based on its extension
const filetypes = /jpeg|jpg|png/;
const extname = filetypes.test(path.extname(file.originalname).toLowerCase());
if (extname) {
cb(null, Date.now() + "_" + file.originalname);
} else {
cb("Error: only .jpeg, .jpg, .png files are allowed!");
}
}
});
const uploadProduct = multer({
storage: storageProduct,
limits: {
fileSize: 1024 * 1024 * 5 // limit filesize to 5MB
},
});
const storagePoster = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, './public/posters');
},
filename: function(req, file, cb) {
// Check file type based on its extension
const filetypes = /jpeg|jpg|png/;
const extname = filetypes.test(path.extname(file.originalname).toLowerCase());
if (extname) {
cb(null, Date.now() + "_" + file.originalname);
} else {
cb("Error: only .jpeg, .jpg, .png files are allowed!");
}
}
});
const uploadPosters = multer({
storage: storagePoster,
limits: {
fileSize: 1024 * 1024 * 5 // limit filesize to 5MB
},
});
module.exports = {
uploadCategory,
uploadProduct,
uploadPosters,
};