Merge pull request #116 from RomanBurunkov/master
Fix multiple files upload in the same time with tempfile handler.dev 1.1.1-alpha.2
commit
f9d7bbd792
3 changed files with 35 additions and 38 deletions
@ -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(); |
||||
} |
||||
}; |
||||
}; |
||||
|
Loading…
Reference in new issue