@@ -24,11 +24,13 @@ const DEFAULT_OPTIONS = {
24
24
encoding : 'utf-8' ,
25
25
hash : false ,
26
26
uploadDir : os . tmpdir ( ) ,
27
+ storeFiles : true ,
27
28
multiples : false ,
28
29
enabledPlugins : [ 'octetstream' , 'querystring' , 'multipart' , 'json' ] ,
29
30
} ;
30
31
31
- const File = require ( './File' ) ;
32
+ const PersistentFile = require ( './PersistentFile' ) ;
33
+ const VolatileFile = require ( './VolatileFile' ) ;
32
34
const DummyParser = require ( './parsers/Dummy' ) ;
33
35
const MultipartParser = require ( './parsers/Multipart' ) ;
34
36
@@ -138,11 +140,11 @@ class IncomingForm extends EventEmitter {
138
140
const fields = { } ;
139
141
let mockFields = '' ;
140
142
const files = { } ;
141
-
143
+
142
144
this . on ( 'field' , ( name , value ) => {
143
145
if ( this . options . multiples ) {
144
- let mObj = { [ name ] : value } ;
145
- mockFields = mockFields + '&' + qs . stringify ( mObj ) ;
146
+ const mObj = { [ name ] : value } ;
147
+ mockFields = ` ${ mockFields } & ${ qs . stringify ( mObj ) } ` ;
146
148
} else {
147
149
fields [ name ] = value ;
148
150
}
@@ -295,11 +297,10 @@ class IncomingForm extends EventEmitter {
295
297
296
298
this . _flushing += 1 ;
297
299
298
- const file = new File ( {
300
+ const file = this . _newFile ( {
299
301
path : this . _rename ( part ) ,
300
- name : part . filename ,
301
- type : part . mime ,
302
- hash : this . options . hash ,
302
+ filename : part . filename ,
303
+ mime : part . mime ,
303
304
} ) ;
304
305
file . on ( 'error' , ( err ) => {
305
306
this . _error ( err ) ;
@@ -420,7 +421,7 @@ class IncomingForm extends EventEmitter {
420
421
421
422
if ( Array . isArray ( this . openedFiles ) ) {
422
423
this . openedFiles . forEach ( ( file ) => {
423
- file . _writeStream . destroy ( ) ;
424
+ file . destroy ( ) ;
424
425
setTimeout ( fs . unlink , 0 , file . path , ( ) => { } ) ;
425
426
} ) ;
426
427
}
@@ -443,6 +444,21 @@ class IncomingForm extends EventEmitter {
443
444
return new MultipartParser ( this . options ) ;
444
445
}
445
446
447
+ _newFile ( { path : filePath , filename : name , mime : type } ) {
448
+ return this . options . storeFiles
449
+ ? new PersistentFile ( {
450
+ path : filePath ,
451
+ name,
452
+ type,
453
+ hash : this . options . hash ,
454
+ } )
455
+ : new VolatileFile ( {
456
+ name,
457
+ type,
458
+ hash : this . options . hash ,
459
+ } ) ;
460
+ }
461
+
446
462
_getFileName ( headerValue ) {
447
463
// matches either a quoted-string or a token (RFC 2616 section 19.5.1)
448
464
const m = headerValue . match (
0 commit comments