Skip to content

Commit b81d1c7

Browse files
authored
Added max upload size for files (#357)
* Added max upload size * Self reference for size fix
1 parent 7a36a8e commit b81d1c7

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

lib/incoming_form.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ function IncomingForm(opts) {
2525

2626
this.maxFields = opts.maxFields || 1000;
2727
this.maxFieldsSize = opts.maxFieldsSize || 2 * 1024 * 1024;
28+
this.maxFileSize = opts.maxFileSize || 2 * 1024 * 1024;
2829
this.keepExtensions = opts.keepExtensions || false;
2930
this.uploadDir = opts.uploadDir || (os.tmpdir && os.tmpdir()) || os.tmpDir();
3031
this.encoding = opts.encoding || 'utf-8';
@@ -39,6 +40,7 @@ function IncomingForm(opts) {
3940
this._parser = null;
4041
this._flushing = 0;
4142
this._fieldsSize = 0;
43+
this._fileSize = 0;
4244
this.openedFiles = [];
4345

4446
return this;
@@ -214,6 +216,11 @@ IncomingForm.prototype.handlePart = function(part) {
214216
this.openedFiles.push(file);
215217

216218
part.on('data', function(buffer) {
219+
self._fileSize += buffer.length;
220+
if (self._fileSize > self.maxFileSize) {
221+
self._error(new Error('maxFileSize exceeded, received '+self._fileSize+' bytes of file data'));
222+
return;
223+
}
217224
if (buffer.length == 0) {
218225
return;
219226
}
@@ -552,4 +559,3 @@ IncomingForm.prototype._maybeEnd = function() {
552559

553560
this.emit('end');
554561
};
555-

0 commit comments

Comments
 (0)