File tree 2 files changed +9
-5
lines changed
2 files changed +9
-5
lines changed Original file line number Diff line number Diff line change @@ -10,22 +10,26 @@ var app = module.exports = koa();
10
10
11
11
app . use ( function * ( ) {
12
12
// create a go-like channel for control flow
13
- var ch = archan ( ) ;
13
+ var ch = archan ( {
14
+ concurrency : 1 // save only 1 file at a time
15
+ } ) ;
14
16
15
17
// parse the multipart body
16
18
var parts = parse ( this , {
17
- autoFields : true
19
+ autoFields : true // saves the fields to parts.field(s)
18
20
} ) ;
19
21
20
22
// create a temporary folder to store files
21
23
var tmpdir = path . join ( os . tmpdir ( ) , Math . random ( ) . toString ( 36 ) . slice ( 2 ) ) ;
22
24
23
- // make the directory
25
+ // make the temporary directory
24
26
yield fs . mkdir . bind ( null , tmpdir ) ;
25
27
26
28
// yield each part as a stream
27
29
var part ;
28
30
while ( part = yield parts ) {
31
+ // wait if there are too many file descriptors opened
32
+ yield * ch . drain ( ) ;
29
33
// save each part to a file,
30
34
// but do it in a different channel
31
35
// so we don't block this particular while loop.
@@ -34,7 +38,7 @@ app.use(function *(){
34
38
35
39
// return all the filenames as an array
36
40
// after all the files have finished downloading
37
- this . body = yield * ch . end ( ) ;
41
+ this . body = yield * ch . flush ( ) ;
38
42
} )
39
43
40
44
if ( ! module . parent ) app . listen ( 3000 ) ;
Original file line number Diff line number Diff line change 9
9
"koa-session" : " koajs/session" ,
10
10
"co-busboy" : " cojs/busboy" ,
11
11
"save-to" : " ~1.0.0" ,
12
- "archan" : " ~0.3 .0" ,
12
+ "archan" : " ~0.4 .0" ,
13
13
"raw-body" : " ~1.1.1"
14
14
},
15
15
"devDependencies" : {
You can’t perform that action at this time.
0 commit comments