Created
February 13, 2015 10:57
-
-
Save kilianc/bfe0620f4638e63f709f to your computer and use it in GitHub Desktop.
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
var bufferjoiner = require('bufferjoiner') | |
/** | |
* Consumes and buffers a file stream | |
* @param {ReadStream} fileStream The readable file stream | |
* @param {Function} callback The done callback | |
*/ | |
module.exports = function consumeFileStream(fileStream, callback) { | |
if (undefined === callback) return consumeFileStream.bind(null, fileStream) | |
var buffer = bufferjoiner() | |
fileStream.on('data', function (data) { | |
buffer.add(data) | |
}) | |
fileStream.on('end', function () { | |
callback(null, buffer.join()) | |
}) | |
} |
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
var parse = require('co-busboy') | |
/** | |
* Retrieve a file stream from a koa request. | |
* Supports multipart and binary uploads | |
* @param {Context} ctx The koa context | |
* @yield {ReadableStream} The readable stream with incoming file | |
*/ | |
module.exports = function *getFileStream(ctx) { | |
var fileStream | |
if (/image/.test(ctx.get('content-type'))) { | |
fileStream = ctx.req | |
} else { | |
fileStream = yield parse(ctx, { autoFields: true }) | |
} | |
return fileStream | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment