Skip to content

Commit 62e3419

Browse files
authoredApr 8, 2020
Merge pull request #213 from RomanBurunkov/master
Destroy file stream in case of upload timeout.
2 parents 37f824b + 055ceac commit 62e3419

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed
 

‎lib/processMultipart.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ module.exports = (options, req, res, next) => {
5959
: memHandler(options, field, filename); // Upload into RAM.
6060
// Define upload timer.
6161
const uploadTimer = new UploadTimer(options.uploadTimeout, () => {
62-
debugLog(options, `Upload timeout ${field}->${filename}, bytes:${getFileSize()}`);
63-
cleanup();
62+
// After destroy an error event will be emitted and file clean up will be done.
63+
file.destroy(new Error(`Upload timeout ${field}->${filename}, bytes:${getFileSize()}`));
6464
});
6565

6666
file.on('limit', () => {
@@ -109,9 +109,8 @@ module.exports = (options, req, res, next) => {
109109
});
110110

111111
file.on('error', (err) => {
112-
// Reset upload timer in case of errors.
113-
uploadTimer.clear();
114-
debugLog(options, `Error ${field}->${filename}, bytes:${getFileSize()}, error:${err}`);
112+
uploadTimer.clear(); // Reset upload timer in case of errors.
113+
debugLog(options, err);
115114
cleanup();
116115
next();
117116
});

‎lib/tempFileHandler.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module.exports = (options, fieldname, filename) => {
2323
let writePromise = Promise.resolve();
2424

2525
const createWriteStream = () => {
26+
debugLog(options, `Opening write stream for ${fieldname}->${filename}...`);
2627
writeStream = fs.createWriteStream(tempFilePath);
2728
writePromise = new Promise((resolve, reject) => {
2829
writeStream.on('finish', () => {
@@ -62,7 +63,10 @@ module.exports = (options, fieldname, filename) => {
6263
if (writeStream !== false) {
6364
debugLog(options, `Cleaning up temporary file ${tempFilePath}...`);
6465
writeStream.end();
65-
deleteFile(tempFilePath, (err) => { if (err) throw err; });
66+
deleteFile(tempFilePath, err => (err
67+
? debugLog(options, `Cleaning up temporary file ${tempFilePath} failed: ${err}`)
68+
: debugLog(options, `Cleaning up temporary file ${tempFilePath} done.`)
69+
));
6670
}
6771
},
6872
getWritePromise: () => writePromise

0 commit comments

Comments
 (0)
Please sign in to comment.