Skip to content

Commit

Permalink
Merge pull request #59 from Haggus/handle-file-limit
Browse files Browse the repository at this point in the history
Handle 'limit' event when file is over fileSize
  • Loading branch information
richardgirges authored Jan 19, 2018
2 parents dc923e2 + 1c52f44 commit 4eaccda
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,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);

Expand Down
36 changes: 36 additions & 0 deletions test/fileLimitUploads.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'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 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)
.expect(200)
.end(done);
});

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)
.expect(413)
.end(done);
});
});

0 comments on commit 4eaccda

Please sign in to comment.