Skip to content

Commit 4eaccda

Browse files
Merge pull request #59 from Haggus/handle-file-limit
Handle 'limit' event when file is over fileSize
2 parents dc923e2 + 1c52f44 commit 4eaccda

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

lib/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ function processMultipart(options, req, res, next) {
7474
const buffers = [];
7575
let safeFileNameRegex = /[^\w-]/g;
7676

77+
file.on('limit', () => {
78+
res.writeHead(413, {'Connection': 'close'});
79+
res.end('File size limit has been reached');
80+
});
81+
7782
file.on('data', function(data) {
7883
buffers.push(data);
7984

test/fileLimitUploads.spec.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
const request = require('supertest');
5+
const server = require('./server');
6+
const app = server.setup({
7+
limits: {fileSize: 200 * 1024} // set 200kb upload limit
8+
});
9+
const clearUploadsDir = server.clearUploadsDir;
10+
const fileDir = server.fileDir;
11+
12+
describe('Test Single File Upload With File Size Limit', function() {
13+
it(`upload 'basketball.png' (~154kb) with 200kb size limit`, function(done) {
14+
let filePath = path.join(fileDir, 'basketball.png');
15+
16+
clearUploadsDir();
17+
18+
request(app)
19+
.post('/upload/single')
20+
.attach('testFile', filePath)
21+
.expect(200)
22+
.end(done);
23+
});
24+
25+
it(`fail when uploading 'car.png' (~269kb) with 200kb size limit`, function(done) {
26+
let filePath = path.join(fileDir, 'car.png');
27+
28+
clearUploadsDir();
29+
30+
request(app)
31+
.post('/upload/single')
32+
.attach('testFile', filePath)
33+
.expect(413)
34+
.end(done);
35+
});
36+
});

0 commit comments

Comments
 (0)