Skip to content

Commit

Permalink
Merge pull request #116 from RomanBurunkov/master
Browse files Browse the repository at this point in the history
Fix multiple files upload in the same time with tempfile handler.
  • Loading branch information
richardgirges authored Feb 8, 2019
2 parents 59534c5 + 9621857 commit f9d7bbd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 38 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ safeFileNames | <ul><li><code>false</code>&nbsp;**(default)**</li><li><code>true
preserveExtension | <ul><li><code>false</code>&nbsp;**(default)**</li><li><code>true</code></li><li><code>*Number*</code></li></ul> | Preserves filename extension when using <code>safeFileNames</code> option. If set to <code>true</code>, will default to an extension length of 3. If set to <code>*Number*</code>, 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.<br /><br />**Example #1 (true):**<br /><code>app.use(fileUpload({ safeFileNames: true, preserveExtension: true }));</code><br />*myFileName.ext* --> *myFileName.ext*<br /><br />**Example #2 (max extension length 2, extension shifted):**<br /><code>app.use(fileUpload({ safeFileNames: true, preserveExtension: 2 }));</code><br />*myFileName.ext* --> *myFileNamee.xt*
abortOnLimit | <ul><li><code>false</code>&nbsp;**(default)**</li><li><code>true</code></ul> | Returns a HTTP 413 when the file is bigger than the size limit if true. Otherwise, it will add a <code>truncate = true</code> to the resulting file structure.
useTempFiles | <ul><li><code>false</code>&nbsp;**(default)**</li><li><code>true</code></ul> | 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 | <ul><li><code>String</code>&nbsp;**(path)**</li></ul> | Used with the <code>useTempFiles</code> option. Path to the directory where temp files will be stored during the upload process. Add trailing slash.
tempFileDir | <ul><li><code>String</code>&nbsp;**(path)**</li></ul> | Used with the <code>useTempFiles</code> 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 | <ul><li><code>false</code>&nbsp;**(default)**</li><li><code>true</code></li></ul> | By default, req.body and req.files are flattened like this: <code>{'name': 'John', 'hobbies[0]': 'Cinema', 'hobbies[1]': 'Bike'}</code><br /><br/>When this option is enabled they are parsed in order to be nested like this: <code>{'name': 'John', 'hobbies': ['Cinema', 'Bike']}</code>

# Help Wanted
Expand Down
13 changes: 6 additions & 7 deletions lib/processMultipart.js
Original file line number Diff line number Diff line change
@@ -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');

/**
Expand Down Expand Up @@ -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', () => {
Expand Down
58 changes: 28 additions & 30 deletions lib/tempFileHandler.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,40 @@
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();
};
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);
}
tempFilePath = dir + 'tmp' + Date.now();
writeStream = fs.createWriteStream(tempFilePath);
let tempFilePath = path.join(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();
}
};
};

0 comments on commit f9d7bbd

Please sign in to comment.