@ -1,7 +1,8 @@
'use strict' ;
const Busboy = require ( 'busboy' ) ;
const fs = require ( 'fs-extra' ) ;
const fs = require ( 'fs' ) ;
const path = require ( 'path' ) ;
const streamifier = require ( 'streamifier' ) ;
const md5 = require ( 'md5' ) ;
@ -53,7 +54,7 @@ function processMultipart(options, req, res, next) {
busboy = new Busboy ( busboyOptions ) ;
// Build multipart req.body fields
busboy . on ( 'field' , function ( fieldname , val , fieldnameTruncated , valTruncated , encoding , mime ) {
busboy . on ( 'field' , function ( fieldname , val ) {
req . body = req . body || { } ;
let prev = req . body [ fieldname ] ;
@ -85,7 +86,7 @@ function processMultipart(options, req, res, next) {
buffers . push ( data ) ;
if ( options . debug ) {
return console . log ( 'Uploading %s -> %s' , fieldname , filename ) ;
return console . log ( 'Uploading %s -> %s' , fieldname , filename ) ; // eslint-disable-line
}
} ) ;
@ -144,7 +145,7 @@ function processMultipart(options, req, res, next) {
truncated : file . truncated ,
mimetype : mime ,
md5 : md5 ( buf ) ,
mv : function ( p ath, callback ) {
mv : function ( fileP ath, callback ) {
// Callback is passed in, use the callback API
if ( callback ) {
doMove (
@ -170,7 +171,14 @@ function processMultipart(options, req, res, next) {
* @ param { Function } errorFunc
* /
function doMove ( successFunc , errorFunc ) {
const fstream = fs . createWriteStream ( path ) ;
if ( options . createParentPath ) {
const parentPath = path . dirname ( filePath ) ;
if ( ! fs . existsSync ( parentPath ) ) {
fs . mkdirSync ( parentPath ) ;
}
}
const fstream = fs . createWriteStream ( filePath ) ;
streamifier . createReadStream ( buf ) . pipe ( fstream ) ;