Skip to content

Commit

Permalink
simplify multipart example, add some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanong committed Dec 6, 2013
1 parent b1e3d56 commit 67d9aed
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .jshintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
*/test.js
10 changes: 10 additions & 0 deletions body-parsing/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
/**
* Example for handling JSON and urlencoded request bodies.
* All the throws at the beginning of the middleware are all app-dependent and may not be suited for your use-case.
* For the actual body parsing (yield rawBody and down),
* you most may be interested in modules that already do this for you:
*
* - [body](https://github.com/raynos/body)
* - [co-body](https://github.com/visionmedia/co-body)
*/

var koa = require('koa');
var qs = require('querystring');
var rawBody = require('raw-body');
Expand Down
5 changes: 5 additions & 0 deletions flash-messages/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* A very simple flash example.
* Only uses JSON for simplicity.
*/

var koa = require('koa');
var rawBody = require('raw-body');
var session = require('koa-session');
Expand Down
37 changes: 22 additions & 15 deletions multipart/app.js
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);
}
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"koa-session": "koajs/session",
"co-busboy": "cojs/busboy",
"save-to": "~1.0.0",
"archan": "~0.4.0",
"raw-body": "~1.1.1"
},
"devDependencies": {
Expand Down

0 comments on commit 67d9aed

Please sign in to comment.