Reputation: 292
I'm trying to set an upload limit in busboy. Without upload limit i'm able to upload the file successfully.
But when i want to redirect if the upload file size exceeds requirement, i realize that the code is asynchronous and the writing of the file or uploading happens anyway.
What i want to do is, that if the limit reaches configured value, it should redirect to the page without uploading the file. I have tried using Javasript Promise, but it doesn't help.
My busboy code is like this.
var express = require('express');
var router = express.Router();
var inspect = require('util').inspect;
var Busboy = require('busboy');
router.all('/', function(req, res, next) {
if (req.method === 'POST') {
var busboy = new Busboy({ headers: req.headers, limits: { files: 1, fileSize: 30000000 } });
busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
file.on('limit', function(data) {
console.log("limit...");
console.log("ONEEE");
});
console.log("TWOOO");
console.log('File [' + fieldname + ']: filename: ' + filename + ', encoding: ' + encoding + ', mimetype: ' + mimetype);
file.on('data', function(data) {
console.log('File [' + fieldname + '] got ' + data.length + ' bytes');
});
file.on('end', function() {
console.log('File [' + fieldname + '] Finished');
});
});
busboy.on('finish', function() {
console.log('Done parsing form!');
res.writeHead(303, { Connection: 'close', Location: '/test_upload' });
res.end();
});
req.pipe(busboy);
}
});
module.exports = router;
Here i have specified the fileSize limit as 30 MB.. But when i upload a file of say 40 MB, i still get "TWOO" in the console and then "ONEEE"... This is obviously due to the reason that this is happening asynchronously, so what is the solution...?
Basically, if Limit is reached i want to log "ONEEE" and redirect, avoiding logging "TWOOO" to the console and avoiding file processing.
Also, if i try and check inside file.on('data' ... of busboy.on('file' ... i get irregular file size uploads on filesystem on using file.pipe.
For example, this is not working consistently (it shows file as 0 bytes when its 354 byte or similar):
busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
file.on('data', function(data){
fstream = fs.createWriteStream("/home/vibs/temp_01.txt");
file.pipe(fstream);
});
});
Although, if i remove file.on('data' from inside busboy.on('file' ... The stream is written correctly onto the disk, but then there is no way to check if file size has exceeded....
So this is working correctly to write the file to the filesystem... But i can't check if file size exceeded allowable limit...
busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
fstream = fs.createWriteStream("/home/vibs/temp_01.txt");
file.pipe(fstream);
});
So my question is how can i check for file size limit and redirect without executing upload procedure... file.on('data' ... fails since the stream goes corrupt inside the function... And file.on('limit' gets called asynchronously so there is no way to avoid running the file write script first avoiding unnecessary upload..
Upvotes: 4
Views: 2647
Reputation: 373
This will stop uploading a file if it hits the max size.
var fstream;
req.pipe(req.busboy);
req.busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
var path = __dirname + "/../public/";
var limit_reach = false;
var limit_reach_err = "File is too large!";
// Creating a writestream using fs module
fstream = fs.createWriteStream(path);
file.pipe(fstream);
// If the file is larger than the set limit
// delete partially uploaded file
file.on('limit', function(){
fs.unlink(path, function(){
limit_reach = true;
res.status(455).send(limit_reach_err);
});
});
// Despite being asynchronous limit_reach
// will be seen here as true if it hits max size
// as set in file.on.limit because once it hits
// max size the stream will stop and on.finish
// will be triggered.
req.busboy.on('finish', function() {
if(!limit_reach){
res.send("Image saved successfully!");
}
});
});
Upvotes: 3