-
Notifications
You must be signed in to change notification settings - Fork 0
/
multer.js
54 lines (47 loc) · 1.37 KB
/
multer.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
//file upload middleware
const multer = require('multer');
//storage settings (Location and filename) for user file upload
const storage = multer.diskStorage({
destination: function(req, file, callback) {
callback(null, 'public/img/profiles/');
},
filename: function(req, file, callback) {
var extension = file.mimetype.substring(6, file.mimetype.length);
callback(null, req.params.userId + '.' + extension);
}
});
//storage settings (Location and filename) for recipe upload
const storageRecipe = multer.diskStorage({
destination: function(req, file, callback) {
callback(null, 'public/img/');
},
filename: function(req, file, callback) {
var extension = file.mimetype.substring(6, file.mimetype.length);
callback(null, 'recipe_' + req.body.recipeId + '.' + extension);
}
});
//file size limit set to 5mb
const limits = {
fileSize: 1024 * 1024 * 5
}
//Accept jpeg and png types only
const fileFilter = (req, file, callback) => {
if(file.mimetype === 'image/jpeg' || file.mimetype === 'image/png') {
callback(null, true);
}
else {
callback(null, false);
}
};
const upload = multer({
storage: storage,
limits: limits,
fileFilter: fileFilter
});
//uploader for recipe
const uploadRecipe = multer({
storage: storageRecipe,
limits: limits,
fileFilter: fileFilter
});
module.exports = {upload, uploadRecipe};