2013-12-04 06:47:39 +04:00
|
|
|
var BusBoy = require('busboy'),
|
|
|
|
fs = require('fs-extra'),
|
|
|
|
path = require('path'),
|
2014-01-05 07:11:05 +04:00
|
|
|
os = require('os'),
|
|
|
|
crypto = require('crypto');
|
2013-12-04 06:47:39 +04:00
|
|
|
|
|
|
|
// ### ghostBusboy
|
2014-01-07 02:39:03 +04:00
|
|
|
// Process multipart file streams
|
2013-12-04 06:47:39 +04:00
|
|
|
function ghostBusBoy(req, res, next) {
|
|
|
|
var busboy,
|
2014-01-07 02:39:03 +04:00
|
|
|
stream,
|
2014-03-02 05:37:15 +04:00
|
|
|
tmpDir;
|
2013-12-04 06:47:39 +04:00
|
|
|
|
2013-12-25 04:05:20 +04:00
|
|
|
// busboy is only used for POST requests
|
2014-03-02 05:37:15 +04:00
|
|
|
if (req.method && !/post/i.test(req.method)) {
|
2013-12-04 06:47:39 +04:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2014-09-10 08:06:24 +04:00
|
|
|
busboy = new BusBoy({headers: req.headers});
|
2013-12-04 06:47:39 +04:00
|
|
|
tmpDir = os.tmpdir();
|
|
|
|
|
|
|
|
req.files = req.files || {};
|
|
|
|
req.body = req.body || {};
|
|
|
|
|
|
|
|
busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
|
2014-01-05 07:11:05 +04:00
|
|
|
var filePath,
|
|
|
|
tmpFileName,
|
|
|
|
md5 = crypto.createHash('md5');
|
2013-12-04 06:47:39 +04:00
|
|
|
|
2014-03-02 05:37:15 +04:00
|
|
|
// If the filename is invalid, skip the stream
|
2013-12-04 06:47:39 +04:00
|
|
|
if (!filename) {
|
2014-03-02 05:37:15 +04:00
|
|
|
return file.resume();
|
2013-12-04 06:47:39 +04:00
|
|
|
}
|
|
|
|
|
2014-01-07 02:39:03 +04:00
|
|
|
// Create an MD5 hash of original filename
|
2014-01-05 07:11:05 +04:00
|
|
|
md5.update(filename, 'utf8');
|
|
|
|
|
2014-01-07 02:39:03 +04:00
|
|
|
tmpFileName = (new Date()).getTime() + md5.digest('hex');
|
2014-01-05 07:11:05 +04:00
|
|
|
|
|
|
|
filePath = path.join(tmpDir, tmpFileName || 'temp.tmp');
|
2013-12-04 06:47:39 +04:00
|
|
|
|
|
|
|
file.on('end', function () {
|
|
|
|
req.files[fieldname] = {
|
|
|
|
type: mimetype,
|
|
|
|
encoding: encoding,
|
|
|
|
name: filename,
|
|
|
|
path: filePath
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2014-03-02 05:37:15 +04:00
|
|
|
file.on('error', function (error) {
|
2014-01-07 02:39:03 +04:00
|
|
|
console.log('Error', 'Something went wrong uploading the file', error);
|
|
|
|
});
|
|
|
|
|
|
|
|
stream = fs.createWriteStream(filePath);
|
|
|
|
|
|
|
|
stream.on('error', function (error) {
|
|
|
|
console.log('Error', 'Something went wrong uploading the file', error);
|
|
|
|
});
|
|
|
|
|
|
|
|
file.pipe(stream);
|
2013-12-04 06:47:39 +04:00
|
|
|
});
|
|
|
|
|
2014-03-02 05:37:15 +04:00
|
|
|
busboy.on('error', function (error) {
|
|
|
|
console.log('Error', 'Something went wrong parsing the form', error);
|
2014-08-09 17:48:58 +04:00
|
|
|
res.status(500).send({code: 500, message: 'Could not parse upload completely.'});
|
2014-03-02 05:37:15 +04:00
|
|
|
});
|
|
|
|
|
2013-12-04 06:47:39 +04:00
|
|
|
busboy.on('field', function (fieldname, val) {
|
|
|
|
req.body[fieldname] = val;
|
|
|
|
});
|
|
|
|
|
2014-03-02 05:37:15 +04:00
|
|
|
busboy.on('finish', function () {
|
2013-12-04 06:47:39 +04:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
req.pipe(busboy);
|
|
|
|
}
|
|
|
|
|
2014-09-10 08:06:24 +04:00
|
|
|
module.exports = ghostBusBoy;
|