Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add numFilesLimitHandler option so user can provide handler for busboy's filesLimit event #253

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ preserveExtension | <ul><li><code>false</code>&nbsp;**(default)**</li><li><code>
abortOnLimit | <ul><li><code>false</code>&nbsp;**(default)**</li><li><code>true</code></ul> | Returns a HTTP 413 when the file is bigger than the size limit if true. Otherwise, it will add a <code>truncated = true</code> to the resulting file structure.
responseOnLimit | <ul><li><code>'File size limit has been reached'</code>&nbsp;**(default)**</li><li><code>*String*</code></ul> | Response which will be send to client if file size limit exceeded when abortOnLimit set to true.
limitHandler | <ul><li><code>false</code>&nbsp;**(default)**</li><li><code>function(req, res, next)</code></li></ul> | User defined limit handler which will be invoked if the file is bigger than configured limits.
numFilesLimitHandler | <ul><li><code>false</code>&nbsp;**(default)**</li><li><code>function(req, res, next)</code></li></ul> | User defined handler which will be invoked if the number of files is greater than the configured `limits.files`.
useTempFiles | <ul><li><code>false</code>&nbsp;**(default)**</li><li><code>true</code></ul> | By default this module uploads files into RAM. Setting this option to True turns on using temporary files instead of utilising RAM. This avoids memory overflow issues when uploading large files or in case of uploading lots of files at same time.
tempFileDir | <ul><li><code>String</code>&nbsp;**(path)**</li></ul> | Path to store temporary files.<br />Used along with the <code>useTempFiles</code> option. By default this module uses 'tmp' folder in the current working directory.<br />You can use trailing slash, but it is not necessary.
parseNested | <ul><li><code>false</code>&nbsp;**(default)**</li><li><code>true</code></li></ul> | By default, req.body and req.files are flattened like this: <code>{'name': 'John', 'hobbies[0]': 'Cinema', 'hobbies[1]': 'Bike'}</code><br /><br/>When this option is enabled they are parsed in order to be nested like this: <code>{'name': 'John', 'hobbies': ['Cinema', 'Bike']}</code>
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const DEFAULT_OPTIONS = {
abortOnLimit: false,
responseOnLimit: 'File size limit has been reached',
limitHandler: false,
numFilesLimitHandler: false,
createParentPath: false,
parseNested: false,
useTempFiles: false,
Expand Down
8 changes: 8 additions & 0 deletions lib/processMultipart.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ module.exports = (options, req, res, next) => {
uploadTimer.set();
});

busboy.on('filesLimit', () => {
debugLog(options, `Number of files limit reached`);
// Run a user defined limit handler if it has been set.
if (isFunc(options.numFilesLimitHandler)){
return options.numFilesLimitHandler(req, res, next);
}
});

busboy.on('finish', () => {
debugLog(options, `Busboy finished parsing request.`);
if (options.parseNested) {
Expand Down
62 changes: 62 additions & 0 deletions test/numFilesLimitHandler.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
'use strict';

const path = require('path');
const request = require('supertest');
const assert = require('assert');
const server = require('./server');
const clearUploadsDir = server.clearUploadsDir;
const fileDir = server.fileDir;

describe('Test Multiple File Upload With Files Limit Handler', function() {
let app, numFilesLimitHandlerRun;

beforeEach(function() {
clearUploadsDir();
numFilesLimitHandlerRun = false;
});

describe('Run numFilesLimitHandler on limit reached.', function(){
before(function() {
app = server.setup({
limits: { files: 3 }, // set limit of 3 files
numFilesLimitHandler: (req, res) => { // set limit handler
res.writeHead(500, { Connection: 'close', 'Content-Type': 'application/json'});
res.end(JSON.stringify({response: 'Too many files!'}));
numFilesLimitHandlerRun = true;
}
});
});

it('Runs handler when too many files', (done) => {
const req = request(app).post('/upload/multiple');

['car.png', 'tree.png', 'basketball.png', 'car.png'].forEach((fileName, index) => {
let filePath = path.join(fileDir, fileName);
req.attach(`testFile${index+1}`, filePath);
});

req
.expect(500, {response: 'Too many files!'})
.end(() => {
assert.ok(numFilesLimitHandlerRun, 'handler was run');
done();
});
});

it('Does not run handler when number of files is below limit', (done) => {
const req = request(app).post('/upload/multiple');

['car.png', 'tree.png', 'basketball.png'].forEach((fileName, index) => {
let filePath = path.join(fileDir, fileName);
req.attach(`testFile${index+1}`, filePath);
});

req
.expect(200)
.end(() => {
assert.ok(!numFilesLimitHandlerRun, 'handler was not run');
done();
});
});
});
});