Skip to content

Instantly share code, notes, and snippets.

@kilianc
Created February 13, 2015 10:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kilianc/bfe0620f4638e63f709f to your computer and use it in GitHub Desktop.
Save kilianc/bfe0620f4638e63f709f to your computer and use it in GitHub Desktop.
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())
})
}
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