-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #116 from RomanBurunkov/master
Fix multiple files upload in the same time with tempfile handler.
- Loading branch information
Showing
3 changed files
with
35 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}; | ||
}; |