Giter Club home page Giter Club logo

koa-busboy's Introduction

koa-busboy

A koa's middleware for handling multipart form. Note that, this middleware supports Koa v2 only.

Installation

$ npm i -S koa-busboy

Usage

koa-busboy will adds all text fields to ctx.request.body object and uploaded files to ctx.request.files array.

To create middleware, simply call the module function with custom config options as following:

const busboy = require('koa-busboy')
const uploader = busboy(options)

While, options is same as original busboy module.

However, you can specify the following options also.

  • dest - string - The folder to save uploaded files (Default: system temp folder - os.tmpdir()).

  • fnDestFilename - function - The function that defines the final filename which saved on dest folder. The fnDestFilename(fieldname, filename) function will return the name of file which be saved on disk. While fieldname is your uploaded field's name and filename is your uploaded file's name. (Default: (fieldname, filename) => Date.now() + fieldname + filename)

  • acceptMimeTypes - Array - A list of mimetypes that are accepted, default is to accept all.

Example

const busboy = require('koa-busboy')
const koaRouter = require('koa-router')

const uploader = busboy({
  dest: './upload' // default is system temp folder (`os.tmpdir()`)
  fnDestFilename: (fieldname, filename) => uuid() + filename,
  acceptMimeTypes: ['image/gif', 'image/jpeg'] //only gif and jpeg files are uploded
})
const router = koaRouter()

router.post('/upload', uploader, async ctx => {
  // fields
  // text fields is add to ctx.request.body object
  let { name } = ctx.request.body
  // files
  // uploaded files is add to ctx.request.files array
  let fileReadStream = ctx.request.files[0]
})

koa-busboy's People

Contributors

christopherl91 avatar dominhhai avatar nervgh avatar ostec-mh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

koa-busboy's Issues

Add uploaded file size to extracted data

https://github.com/mscdex/busboy#examples show how to extract data:

busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
  console.log('File [' + fieldname + ']: filename: ' + filename + ', encoding: ' + encoding + ', mimetype: ' + mimetype);
  file.on('data', function(data) {
    console.log('File [' + fieldname + '] got ' + data.length + ' bytes'); // <-- HERE
  });
  file.on('end', function() {
    console.log('File [' + fieldname + '] Finished');
  });
});

Is it possible to implement this feature in this package?

Make saving file to disk optional

Would you make saving files to disk optional? I'd like to use just the readstreams returned by busboy for the various files and do something else with them rather than save them to disk.

Support dynamic dest

Add feature dynamic dest like depend on post data, code like

const uploader = async ctx => {
  console.log(ctx.request.body)
  const {path} = ctx.request.body
  return await busboy({
    dest: path,
    fnDestFilename: (fieldname, filename) => filename
  })
}

Unhandled promise rejection

I've encountered an unhandled promise rejection that I was able to track down and fix with the following patch.

diff --git a/extract.js b/extract.js
index 18e614e..e657bc3 100644
--- a/extract.js
+++ b/extract.js
@@ -27,7 +27,7 @@ module.exports = function (req, dest, fnDestFilename, opts = {}) {
 
             resolve(rs)
           })
-      }))
+      }).catch(reject))
     })
 
     busboy.on('field', (name, value) => {

I think it happened when there was a problem with GlusterFS. I was able to reproduce it using SSHFS, but it's not simple.

There is a Promise.all(files).catch(reject) further down, but it's too late.

Make final filename on disk configurable

Thanks for the lib @dominhhai! I have had to fork it just to work around the fact that the actual filename that gets written to disk on the server is opaquely modified:

https://github.com/dominhhai/koa-busboy/blob/master/extract.js#L16

It would be ideal if this was configurable - in my particular use case my filenames are already guaranteed to be unique and I don't really want to have to parse the fileReadStream.path to work out what the final filename is on disk. I also find it unintuitive that fileReadStream.filename shows the original filename and not the final, modified one.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.