0
down vote
favorite
I'm handling a multipart form with multer that consists of 5 different image fields and one video field.
I am encountering the problem that if two files are too big, the code crashes.
This is the code:
var tenmpName;
var tempNames = new String();
var phase = {
picture_1: new Object(),
picture_2: new Object(),
picture_3: new Object(),
picture_4: new Object(),
picture_5: new Object(),
video_1: new Object()
};
var max_image_size = 204800;
var max_video_size = 2048000;
var storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, './public/assets/profiles/' + req.session.user_name.toLowerCase());
},
filename: function (req, file, callback) {
tempName = Date.now() + '-' + file.originalname;
phase[file.fieldname].name = true;
if(tempNames == ''){
tempNames = tempNames + tempName;
} else {
tempNames = tempNames + ';' + tempName;
}
callback(null, tempName);
}
});
var fileFilter = function(req, file, cb){
if(file.fieldname.includes('picture') == true){
if(file.mimetype.includes("image") == true){
phase[file.fieldname].type = true;
cb(null, true);
} else {
cb(null, false);
}
} else if(file.fieldname.includes('video') == true){
if(file.mimetype.includes("video") == true){
phase[file.fieldname].type = true;
cb(null, true);
} else {
cb(null, false);
}
}
};
var limits = {
fileSize: max_image_size
};
multer({ storage : storage, fileFilter: fileFilter, limits: limits }).fields([
{ name: 'picture_1', maxCount: 1 },
{ name: 'picture_2', maxCount: 1 },
{ name: 'picture_3', maxCount: 1 },
{ name: 'picture_4', maxCount: 1 },
{ name: 'picture_5', maxCount: 1 },
{ name: 'video_1', maxCount: 1 }
])(req, res, function(err) {
if(err){
console.log(err);
}
console.log(req.body);
console.log(tempNames);
console.log(phase);
});
This is the error including all output before:
{ [Error: File too large]
code: 'LIMIT_FILE_SIZE',
field: 'picture_1',
storageErrors: [] }
{ text: 'asa' }
1478102481509-giphy (3).gif;1478102484345-giphy (3).gif
{ picture_1: { type: true, name: true },
picture_2: { type: true, name: true },
picture_3: {},
picture_4: {},
picture_5: {},
video_1: {} }
fs.js:927
binding.unlink(pathModule._makeLong(path), req);
^
TypeError: path must be a string
at TypeError (native)
at Object.fs.unlink (fs.js:927:11)
at DiskStorage._removeFile (/home/workspace/node_modules/multer/storage/disk.js:61:6)
at remove (/home/workspace/node_modules/multer/lib/make-middleware.js:64:19)
at handleFile (/home/workspace/node_modules/multer/lib/remove-uploaded-files.js:10:5)
at removeUploadedFiles (/home/workspace/node_modules/multer/lib/remove-uploaded-files.js:25:3)
at EventEmitter.<anonymous> (/home/workspace/node_modules/multer/lib/make-middleware.js:67:9)
at EventEmitter.g (events.js:260:16)
at emitNone (events.js:72:20)
at EventEmitter.emit (events.js:166:7)
[nodemon] app crashed - waiting for file changes before starting...
Am I doing smth wrong or is it supposed to crash if two files fail limitations?
0
down vote
favorite
I'm handling a multipart form with multer that consists of 5 different image fields and one video field.
I am encountering the problem that if two files are too big, the code crashes.
This is the code:
This is the error including all output before:
Am I doing smth wrong or is it supposed to crash if two files fail limitations?