Skip to content

Commit

Permalink
Comment extra condition in fileFactory(issue #1), add more logging
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanBurunkov committed Jul 16, 2020
1 parent d57ee02 commit b24233d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/fileFactory.js
Expand Up @@ -39,7 +39,7 @@ module.exports = (options, fileUploadOptions = {}) => {
// see: https://github.com/richardgirges/express-fileupload/issues/14
// firefox uploads empty file in case of cache miss when f5ing page.
// resulting in unexpected behavior. if there is no file data, the file is invalid.
if (!fileUploadOptions.useTempFiles && !options.buffer.length) return;
// if (!fileUploadOptions.useTempFiles && !options.buffer.length) return;

// Create and return file object.
return {
Expand Down
13 changes: 11 additions & 2 deletions lib/processMultipart.js
Expand Up @@ -59,6 +59,8 @@ module.exports = (options, req, res, next) => {
: memHandler(options, field, filename); // Upload into RAM.
// Define upload timer.
const uploadTimer = new UploadTimer(options.uploadTimeout, () => {
file.removeAllListeners('data');
file.resume();
// After destroy an error event will be emitted and file clean up will be done.
file.destroy(new Error(`Upload timeout ${field}->${filename}, bytes:${getFileSize()}`));
});
Expand Down Expand Up @@ -88,9 +90,12 @@ module.exports = (options, req, res, next) => {
debugLog(options, `Upload finished ${field}->${filename}, bytes:${size}`);
// Reset upload timer in case of end event.
uploadTimer.clear();
// See https://github.com/richardgirges/express-fileupload/issues/191
// Do not add file instance to the req.files if original name and size are empty.
// Empty name and zero size indicates empty file field in the posted form.
if (!name && size === 0) return;
if (!name && size === 0) {
return debugLog(options, `Don't add file instance if original name and size are empty`);
}
req.files = buildFields(req.files, field, fileFactory({
buffer: complete(),
name: filename,
Expand Down Expand Up @@ -122,6 +127,7 @@ module.exports = (options, req, res, next) => {
});

busboy.on('finish', () => {
debugLog(options, `Busboy finished parsing request.`);
if (options.parseNested) {
req.body = processNested(req.body);
req.files = processNested(req.files);
Expand All @@ -139,7 +145,10 @@ module.exports = (options, req, res, next) => {
});
});

busboy.on('error', next);
busboy.on('error', (err) => {
debugLog(options, `Busboy error`);
next(err);
});

req.pipe(busboy);
};

0 comments on commit b24233d

Please sign in to comment.