diff --git a/README.md b/README.md index b150f07..7c8ac4b 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,7 @@ preserveExtension |
false
**(default)**
abortOnLimit | false
**(default)**true
| Returns a HTTP 413 when the file is bigger than the size limit if true. Otherwise, it will add a truncated = true
to the resulting file structure.
responseOnLimit | 'File size limit has been reached'
**(default)***String*
| Response which will be send to client if file size limit exceeded when abortOnLimit set to true.
limitHandler | false
**(default)**function(req, res, next)
| User defined limit handler which will be invoked if the file is bigger than configured limits.
+numFilesLimitHandler | false
**(default)**function(req, res, next)
| User defined handler which will be invoked if the number of files is greater than the configured `limits.files`.
useTempFiles | false
**(default)**true
| 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 | String
**(path)**
| Path to store temporary files.
Used along with the useTempFiles
option. By default this module uses 'tmp' folder in the current working directory.
You can use trailing slash, but it is not necessary.
parseNested | false
**(default)**true
| By default, req.body and req.files are flattened like this: {'name': 'John', 'hobbies[0]': 'Cinema', 'hobbies[1]': 'Bike'}
When this option is enabled they are parsed in order to be nested like this: {'name': 'John', 'hobbies': ['Cinema', 'Bike']}
diff --git a/lib/index.js b/lib/index.js
index 41437a6..a454357 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -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,
diff --git a/lib/processMultipart.js b/lib/processMultipart.js
index 861960b..feb98c5 100644
--- a/lib/processMultipart.js
+++ b/lib/processMultipart.js
@@ -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) {
diff --git a/test/numFilesLimitHandler.spec.js b/test/numFilesLimitHandler.spec.js
new file mode 100644
index 0000000..56d9f93
--- /dev/null
+++ b/test/numFilesLimitHandler.spec.js
@@ -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();
+ });
+ });
+ });
+});