From 272a753fea33805598b05883d68f68e2e2bde687 Mon Sep 17 00:00:00 2001 From: Roman Burunkov Date: Wed, 6 Feb 2019 19:58:41 +0300 Subject: [PATCH 1/5] Update tempFileHandler.js Fix multiple files upload in the same time with temp files handler. --- lib/tempFileHandler.js | 57 ++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 30 deletions(-) diff --git a/lib/tempFileHandler.js b/lib/tempFileHandler.js index b6dcb38..409d45a 100644 --- a/lib/tempFileHandler.js +++ b/lib/tempFileHandler.js @@ -1,42 +1,39 @@ const fs = require('fs'); -let writeStream; -let tempFilePath; - -module.exports.getTempFilePath = function() { - return tempFilePath; -}; - -module.exports.cleanupStream = function() { - writeStream.end(); - - fs.unlink(tempFilePath, function(err) { - if (err) throw err; - }); -}; - -module.exports.complete = function(){ - writeStream.end(); -}; module.exports.tempFileHandler = function(options, fieldname, filename) { - const dir = options.tempFileDir || process.cwd() + '/tmp/'; + const dir = __dirname + (options.tempFileDir || '/tmp/'); if (!fs.existsSync(dir)) { fs.mkdirSync(dir); } - tempFilePath = dir + 'tmp' + Date.now(); - writeStream = fs.createWriteStream(tempFilePath); + let tempFilePath = dir + 'tmp' + Date.now(); + let writeStream = fs.createWriteStream(tempFilePath); let fileSize = 0; // eslint-disable-line - return function(data) { - writeStream.write(data); - fileSize += data.length; - if (options.debug) { - return console.log( // eslint-disable-line - `Uploaded ${data.length} bytes for `, - fieldname, - filename - ); + return { + handler: function(data) { + writeStream.write(data); + fileSize += data.length; + if (options.debug) { + return console.log( // eslint-disable-line + `Uploaded ${data.length} bytes for `, + fieldname, + filename + ); + } + }, + getTempFilePath: function(){ + return tempFilePath; + }, + cleanupStream: function(){ + writeStream.end(); + + fs.unlink(tempFilePath, function(err) { + if (err) throw err; + }); + }, + complete: function(){ + writeStream.end(); } }; }; From 42050cb4c07e57c69410a155734cbb3d081b631c Mon Sep 17 00:00:00 2001 From: Roman Burunkov Date: Wed, 6 Feb 2019 19:59:30 +0300 Subject: [PATCH 2/5] Update processMultipart.js Fix multiple file upload in the same time --- lib/processMultipart.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/processMultipart.js b/lib/processMultipart.js index 9341863..81bad5b 100644 --- a/lib/processMultipart.js +++ b/lib/processMultipart.js @@ -1,11 +1,6 @@ const Busboy = require('busboy'); const fileFactory = require('./fileFactory'); -const { - getTempFilePath, - complete, - cleanupStream, - tempFileHandler -} = require('./tempFileHandler'); +const {tempFileHandler} = require('./tempFileHandler'); const processNested = require('./processNested'); /** @@ -64,8 +59,12 @@ module.exports = function processMultipart(options, req, res, next) { return console.log('Uploading %s -> %s', fieldname, filename); // eslint-disable-line } }; + + const tempHandlerGen = tempFileHandler(options, fieldname, filename); + const {getTempFilePath, complete, cleanupStream} = tempHandlerGen; + const dataHandler = options.useTempFiles - ? tempFileHandler(options, fieldname, filename) + ? tempHandlerGen.handler : memHandler; file.on('limit', () => { From c11471c68d7c18dac8c778ec99bdc903473f55b7 Mon Sep 17 00:00:00 2001 From: Roman Burunkov Date: Thu, 7 Feb 2019 11:26:39 +0300 Subject: [PATCH 3/5] Fixes dir value Fix dir value expression, which probable came from some old version: const dir = options.tempFileDir || process.cwd() + '/tmp/'; insted of const dir = __dirname + (options.tempFileDir || '/tmp/'); --- lib/tempFileHandler.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tempFileHandler.js b/lib/tempFileHandler.js index 409d45a..bb98414 100644 --- a/lib/tempFileHandler.js +++ b/lib/tempFileHandler.js @@ -1,7 +1,7 @@ const fs = require('fs'); module.exports.tempFileHandler = function(options, fieldname, filename) { - const dir = __dirname + (options.tempFileDir || '/tmp/'); + const dir = options.tempFileDir || process.cwd() + '/tmp/'; if (!fs.existsSync(dir)) { fs.mkdirSync(dir); From d864fe10f1d0171195638c94dbeff84c60d15261 Mon Sep 17 00:00:00 2001 From: Roman Burunkov Date: Thu, 7 Feb 2019 12:34:41 +0300 Subject: [PATCH 4/5] Update tempFileHandler.js Use path module to deal with paths to temp directory and temp files. --- lib/tempFileHandler.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/tempFileHandler.js b/lib/tempFileHandler.js index bb98414..ba5bf7c 100644 --- a/lib/tempFileHandler.js +++ b/lib/tempFileHandler.js @@ -1,12 +1,13 @@ const fs = require('fs'); +const path = require('path'); module.exports.tempFileHandler = function(options, fieldname, filename) { - const dir = options.tempFileDir || process.cwd() + '/tmp/'; + const dir = path.normalize(options.tempFileDir || process.cwd() + '/tmp/'); if (!fs.existsSync(dir)) { fs.mkdirSync(dir); } - let tempFilePath = dir + 'tmp' + Date.now(); + let tempFilePath = path.join(dir, 'tmp' + Date.now()); let writeStream = fs.createWriteStream(tempFilePath); let fileSize = 0; // eslint-disable-line From 96218579b51793e3d2c89a0163c83768b59b8c35 Mon Sep 17 00:00:00 2001 From: Roman Burunkov Date: Thu, 7 Feb 2019 19:18:13 +0300 Subject: [PATCH 5/5] Update README.md Change description about necessary trailing slashed in temporary files path. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index dd228d7..bd7f068 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ safeFileNames |
  • false **(default)**
  • true preserveExtension |
    • false **(default)**
    • true
    • *Number*
    | Preserves filename extension when using safeFileNames option. If set to true, will default to an extension length of 3. If set to *Number*, this will be the max allowable extension length. If an extension is smaller than the extension length, it remains untouched. If the extension is longer, it is shifted.

    **Example #1 (true):**
    app.use(fileUpload({ safeFileNames: true, preserveExtension: true }));
    *myFileName.ext* --> *myFileName.ext*

    **Example #2 (max extension length 2, extension shifted):**
    app.use(fileUpload({ safeFileNames: true, preserveExtension: 2 }));
    *myFileName.ext* --> *myFileNamee.xt* abortOnLimit |
    • false **(default)**
    • true
    | Returns a HTTP 413 when the file is bigger than the size limit if true. Otherwise, it will add a truncate = true to the resulting file structure. useTempFiles |
    • false **(default)**
    • true
    | Will use temporary files at the specified tempDir for managing uploads rather than using buffers in memory. This avoids memory issues when uploading large files. -tempFileDir |
    • String **(path)**
    | Used with the useTempFiles option. Path to the directory where temp files will be stored during the upload process. Add trailing slash. +tempFileDir |
    • String **(path)**
    | Used with the useTempFiles option. Path to the directory where temp files will be stored during the upload process. Feel free to add trailing slash, but it is not necessary. parseNested |
    • false **(default)**
    • true
    | By default, req.body and req.files are flattened like this: {'name': 'John', 'hobbies[0]': 'Cinema', 'hobbies[1]': 'Bike'}

    When this option is enabled they are parsed in order to be nested like this: {'name': 'John', 'hobbies': ['Cinema', 'Bike']} # Help Wanted