From 10263ab74174765d7b0ac9466900f79dd141a889 Mon Sep 17 00:00:00 2001 From: Mateusz Mrowiec Date: Fri, 1 Dec 2017 11:10:09 +0100 Subject: [PATCH 1/3] Handle 'limit' event when file is over fileSize --- lib/index.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/index.js b/lib/index.js index 24978ff..712ff78 100644 --- a/lib/index.js +++ b/lib/index.js @@ -73,6 +73,11 @@ function processMultipart(options, req, res, next) { const buffers = []; let safeFileNameRegex = /[^\w-]/g; + file.on('limit', () => { + res.writeHead(413, {'Connection': 'close'}); + res.end('File size limit has been reached'); + }); + file.on('data', function(data) { buffers.push(data); From 91d944f50429d451842c1c8deb1c92d1ab9d7d5b Mon Sep 17 00:00:00 2001 From: Mateusz Mrowiec Date: Wed, 6 Dec 2017 07:15:56 +0100 Subject: [PATCH 2/3] Add file limit upload test --- test/fileLimitUploads.spec.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 test/fileLimitUploads.spec.js diff --git a/test/fileLimitUploads.spec.js b/test/fileLimitUploads.spec.js new file mode 100644 index 0000000..852319f --- /dev/null +++ b/test/fileLimitUploads.spec.js @@ -0,0 +1,31 @@ +'use strict'; + +const path = require('path'); +const request = require('supertest'); +const server = require('./server'); +const app = server.setup({ + limits: {fileSize: 200 * 1024} // set 200kb upload limit +}); +const fileDir = server.fileDir; + +describe('Test Single File Upload With File Size Limit', function() { + it(`upload 'basketball.png' (~154kb) with 200kb size limit`, function(done) { + let filePath = path.join(fileDir, 'basketball.png'); + + request(app) + .post('/upload/single') + .attach('testFile', filePath) + .expect(200) + .end(done); + }); + + it(`fail when uploading 'car.png' (~269kb) with 200kb size limit`, function(done) { + let filePath = path.join(fileDir, 'car.png'); + + request(app) + .post('/upload/single') + .attach('testFile', filePath) + .expect(413) + .end(done); + }); +}); From 1c52f44d8759a0abde3545e4ce34649c2ca19e44 Mon Sep 17 00:00:00 2001 From: Mateusz Mrowiec Date: Wed, 6 Dec 2017 07:26:24 +0100 Subject: [PATCH 3/3] Add clearUploadsDir --- test/fileLimitUploads.spec.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/fileLimitUploads.spec.js b/test/fileLimitUploads.spec.js index 852319f..c1278ef 100644 --- a/test/fileLimitUploads.spec.js +++ b/test/fileLimitUploads.spec.js @@ -6,12 +6,15 @@ const server = require('./server'); const app = server.setup({ limits: {fileSize: 200 * 1024} // set 200kb upload limit }); +const clearUploadsDir = server.clearUploadsDir; const fileDir = server.fileDir; describe('Test Single File Upload With File Size Limit', function() { it(`upload 'basketball.png' (~154kb) with 200kb size limit`, function(done) { let filePath = path.join(fileDir, 'basketball.png'); + clearUploadsDir(); + request(app) .post('/upload/single') .attach('testFile', filePath) @@ -22,6 +25,8 @@ describe('Test Single File Upload With File Size Limit', function() { it(`fail when uploading 'car.png' (~269kb) with 200kb size limit`, function(done) { let filePath = path.join(fileDir, 'car.png'); + clearUploadsDir(); + request(app) .post('/upload/single') .attach('testFile', filePath)