-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileManager.js
67 lines (49 loc) · 1.56 KB
/
fileManager.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
const fs = require('fs')
const multer = require('multer')
const imageFileFilter = (req, file, cb) => {
if(!file.originalname.match(/\.(jpg|jpeg|png|gif)$/)) {
return cb(new Error('You can only upload image files!'), false);
}
cb(null, true);
};
// Multer configuration (for ItemModel)
const storage1 = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'public/images' );
},
filename: (req, file, cb) => {
// console.log("File name : " + Date.now() + file.originalname);
cb(null, Date.now() + '-' + file.originalname)
}
});
const upload1 = multer({
storage: storage1,
limits: {fileSize: 1024 * 1024 * 5}, // maximum file size allowed is 5 MB
fileFilter: imageFileFilter
});
module.exports.ItemImage = upload1;
// Multer configuration (for CartModel)
const storage2 = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'public/images' );
},
filename: (req, file, cb) => {
// console.log("File name : " + Date.now() + file.originalname);
cb(null, Date.now() + '-' + file.originalname)
}
});
const upload2 = multer({
storage: storage2,
limits: {fileSize: 1024 * 1024 * 5}, // maximum file size allowed is 5 MB
fileFilter: imageFileFilter
});
module.exports.CartImage = upload2;
//For deleting files
async function DeleteFile(_path)
{
// console.log("_path : " + _path);
fs.unlink(_path, (err) =>{
console.log(err);
});
}
module.exports.DeleteFile = DeleteFile;