@@ -14,8 +14,9 @@ module.exports = function(options) {
1414 options = options || { } ;
1515
1616 return function ( req , res , next ) {
17- if ( ! hasBody ( req ) || ! hasAcceptableMethod ( req ) || ! hasAcceptableMime ( req ) )
17+ if ( ! hasBody ( req ) || ! hasAcceptableMethod ( req ) || ! hasAcceptableMime ( req ) ) {
1818 return next ( ) ;
19+ }
1920
2021 processMultipart ( options , req , res , next ) ;
2122 } ;
@@ -56,11 +57,13 @@ function processMultipart(options, req, res, next) {
5657
5758 let prev = req . body [ fieldname ] ;
5859
59- if ( ! prev )
60+ if ( ! prev ) {
6061 return req . body [ fieldname ] = val ;
62+ }
6163
62- if ( Array . isArray ( prev ) )
64+ if ( Array . isArray ( prev ) ) {
6365 return prev . push ( val ) ;
66+ }
6467
6568 req . body [ fieldname ] = [ prev , val ] ;
6669 } ) ;
@@ -73,34 +76,39 @@ function processMultipart(options, req, res, next) {
7376 file . on ( 'data' , function ( data ) {
7477 buffers . push ( data ) ;
7578
76- if ( options . debug )
79+ if ( options . debug ) {
7780 return console . log ( 'Uploading %s -> %s' , fieldname , filename ) ;
81+ }
7882 } ) ;
7983
8084 file . on ( 'end' , function ( ) {
81- if ( ! req . files )
85+ if ( ! req . files ) {
8286 req . files = { } ;
87+ }
8388
8489 const buf = Buffer . concat ( buffers ) ;
8590 // see: https://github.com/richardgirges/express-fileupload/issues/14
8691 // firefox uploads empty file in case of cache miss when f5ing page.
8792 // resulting in unexpected behavior. if there is no file data, the file is invalid.
88- if ( ! buf . length )
93+ if ( ! buf . length ) {
8994 return ;
95+ }
9096
9197 if ( options . safeFileNames ) {
9298 let maxExtensionLength = 3 ;
9399 let extension = '' ;
94100
95- if ( typeof options . safeFileNames === 'object' )
101+ if ( typeof options . safeFileNames === 'object' ) {
96102 safeFileNameRegex = options . safeFileNames ;
103+ }
97104
98105 maxExtensionLength = parseInt ( options . preserveExtension ) ;
99106 if ( options . preserveExtension || maxExtensionLength === 0 ) {
100- if ( isNaN ( maxExtensionLength ) )
107+ if ( isNaN ( maxExtensionLength ) ) {
101108 maxExtensionLength = 3 ;
102- else
109+ } else {
103110 maxExtensionLength = Math . abs ( maxExtensionLength ) ;
111+ }
104112
105113 let filenameParts = filename . split ( '.' ) ;
106114 let filenamePartsLen = filenameParts . length ;
@@ -126,20 +134,44 @@ function processMultipart(options, req, res, next) {
126134 data : buf ,
127135 encoding : encoding ,
128136 mimetype : mime ,
129- mv : function ( path , callback ) {
130- let fstream = fs . createWriteStream ( path ) ;
137+ mv : function ( path , callback = null ) {
138+ // Callback is passed in, use the callback API
139+ if ( callback ) {
140+ doMove (
141+ ( ) => {
142+ callback ( null ) ;
143+ } ,
144+ ( error ) => {
145+ callback ( error ) ;
146+ }
147+ ) ;
148+
149+ // Otherwise, return a promise
150+ } else {
151+ return new Promise ( ( resolve , reject ) => {
152+ doMove ( resolve , reject ) ;
153+ } ) ;
154+ }
131155
132- streamifier . createReadStream ( buf ) . pipe ( fstream ) ;
156+ /**
157+ * Local function that moves the file to a different location on the filesystem
158+ * Takes two function arguments to make it compatible w/ Promise or Callback APIs
159+ * @param {Function } successFunc
160+ * @param {Function } errorFunc
161+ */
162+ function doMove ( successFunc , errorFunc ) {
163+ const fstream = fs . createWriteStream ( path ) ;
133164
134- fstream . on ( 'error' , function ( error ) {
135- if ( callback )
136- callback ( error ) ;
137- } ) ;
165+ streamifier . createReadStream ( buf ) . pipe ( fstream ) ;
138166
139- fstream . on ( 'close' , function ( ) {
140- if ( callback )
141- callback ( null ) ;
142- } ) ;
167+ fstream . on ( 'error' , function ( error ) {
168+ errorFunc ( error ) ;
169+ } ) ;
170+
171+ fstream . on ( 'close' , function ( ) {
172+ successFunc ( ) ;
173+ } ) ;
174+ }
143175 }
144176 } ;
145177
@@ -148,10 +180,11 @@ function processMultipart(options, req, res, next) {
148180 req . files [ fieldname ] = newFile ;
149181 } else {
150182 // Array fields
151- if ( req . files [ fieldname ] instanceof Array )
183+ if ( req . files [ fieldname ] instanceof Array ) {
152184 req . files [ fieldname ] . push ( newFile ) ;
153- else
185+ } else {
154186 req . files [ fieldname ] = [ req . files [ fieldname ] , newFile ] ;
187+ }
155188 }
156189 } ) ;
157190
0 commit comments