Skip to content

Commit

Permalink
add numFilesLimitHandler option
Browse files Browse the repository at this point in the history
  • Loading branch information
jseppi committed Oct 27, 2020
1 parent 1216f4f commit 58ca337
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
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
10 changes: 10 additions & 0 deletions lib/processMultipart.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ module.exports = (options, req, res, next) => {
uploadTimer.set();
});

busboy.on('filesLimit', () => {
debugLog(options, `Number of files limit reached for ${field}`);
// Reset upload timer in case of file limit reached.
uploadTimer.clear();
// 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();
});
});
});
});

0 comments on commit 58ca337

Please sign in to comment.