-
Notifications
You must be signed in to change notification settings - Fork 740
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
simplify multipart example, add some comments
- Loading branch information
1 parent
b1e3d56
commit 67d9aed
Showing
5 changed files
with
39 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
*/test.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,51 @@ | ||
/** | ||
* Multipart example downloading all the files to disk using co-busboy. | ||
* If all you want is to download the files to a temporary folder, | ||
* just use https://github.com/cojs/multipart instead of copying this code | ||
* as it handles file descriptor limits whereas this does not. | ||
*/ | ||
|
||
var os = require('os'); | ||
var path = require('path'); | ||
var fs = require('fs'); | ||
var koa = require('koa'); | ||
var parse = require('co-busboy'); | ||
var saveTo = require('save-to'); | ||
var archan = require('archan'); | ||
|
||
var app = module.exports = koa(); | ||
|
||
app.use(function *(){ | ||
// create a go-like channel for control flow | ||
var ch = archan({ | ||
concurrency: 1 // save only 1 file at a time | ||
}); | ||
|
||
// parse the multipart body | ||
var parts = parse(this, { | ||
autoFields: true // saves the fields to parts.field(s) | ||
}); | ||
|
||
// create a temporary folder to store files | ||
var tmpdir = path.join(os.tmpdir(), Math.random().toString(36).slice(2)); | ||
var tmpdir = path.join(os.tmpdir(), uid()); | ||
|
||
// make the temporary directory | ||
yield fs.mkdir.bind(null, tmpdir); | ||
|
||
// list of all the files | ||
var files = []; | ||
var file; | ||
|
||
// yield each part as a stream | ||
var part; | ||
while (part = yield parts) { | ||
// wait if there are too many file descriptors opened | ||
yield* ch.drain(); | ||
// save each part to a file, | ||
// but do it in a different channel | ||
// so we don't block this particular while loop. | ||
saveTo(part, path.join(tmpdir, part.filename), ch.push()); | ||
// filename for this part | ||
files.push(file = path.join(tmpdir, part.filename)) | ||
// save the file | ||
yield saveTo(part, file); | ||
} | ||
|
||
// return all the filenames as an array | ||
// after all the files have finished downloading | ||
this.body = yield* ch.flush(); | ||
this.body = files; | ||
}) | ||
|
||
if (!module.parent) app.listen(3000); | ||
if (!module.parent) app.listen(3000); | ||
|
||
function uid() { | ||
return Math.random().toString(36).slice(2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters