arnaucube
arnaucube/comunicationLeapPublic Self-hosted repository
comunicationLeap/node_modules/connect/lib/middleware/multipart.js
node_modules/connect/lib/middleware/multipart.jsRaw
  1/*!
  2 * Connect - multipart
  3 * Copyright(c) 2010 Sencha Inc.
  4 * Copyright(c) 2011 TJ Holowaychuk
  5 * MIT Licensed
  6 */
  7
  8/**
  9 * Module dependencies.
 10 */
 11
 12var formidable = require('formidable')
 13  , _limit = require('./limit')
 14  , utils = require('../utils')
 15  , qs = require('qs');
 16
 17/**
 18 * noop middleware.
 19 */
 20
 21function noop(req, res, next) {
 22  next();
 23}
 24
 25/**
 26 * Multipart:
 27 * 
 28 * Parse multipart/form-data request bodies,
 29 * providing the parsed object as `req.body`
 30 * and `req.files`.
 31 *
 32 * Configuration:
 33 *
 34 *  The options passed are merged with [formidable](https://github.com/felixge/node-formidable)'s
 35 *  `IncomingForm` object, allowing you to configure the upload directory,
 36 *  size limits, etc. For example if you wish to change the upload dir do the following.
 37 *
 38 *     app.use(connect.multipart({ uploadDir: path }));
 39 *
 40 * Options:
 41 *
 42 *   - `limit`  byte limit defaulting to none
 43 *   - `defer`  defers processing and exposes the Formidable form object as `req.form`.
 44 *              `next()` is called without waiting for the form's "end" event.
 45 *              This option is useful if you need to bind to the "progress" event, for example.
 46 *
 47 * @param {Object} options
 48 * @return {Function}
 49 * @api public
 50 */
 51
 52exports = module.exports = function(options){
 53  options = options || {};
 54
 55  var limit = options.limit
 56    ? _limit(options.limit)
 57    : noop;
 58
 59  return function multipart(req, res, next) {
 60    if (req._body) return next();
 61    req.body = req.body || {};
 62    req.files = req.files || {};
 63
 64    if (!utils.hasBody(req)) return next();
 65
 66    // ignore GET
 67    if ('GET' == req.method || 'HEAD' == req.method) return next();
 68
 69    // check Content-Type
 70    if ('multipart/form-data' != utils.mime(req)) return next();
 71
 72    // flag as parsed
 73    req._body = true;
 74
 75    // parse
 76    limit(req, res, function(err){
 77      if (err) return next(err);
 78
 79      var form = new formidable.IncomingForm
 80        , data = {}
 81        , files = {}
 82        , done;
 83
 84      Object.keys(options).forEach(function(key){
 85        form[key] = options[key];
 86      });
 87
 88      function ondata(name, val, data){
 89        if (Array.isArray(data[name])) {
 90          data[name].push(val);
 91        } else if (data[name]) {
 92          data[name] = [data[name], val];
 93        } else {
 94          data[name] = val;
 95        }
 96      }
 97
 98      form.on('field', function(name, val){
 99        ondata(name, val, data);
100      });
101
102      form.on('file', function(name, val){
103        ondata(name, val, files);
104      });
105
106      form.on('error', function(err){
107        if (!options.defer) {
108          err.status = 400;
109          next(err);
110        }
111        done = true;
112      });
113
114      form.on('end', function(){
115        if (done) return;
116        try {
117          req.body = qs.parse(data);
118          req.files = qs.parse(files);
119          if (!options.defer) next();
120        } catch (err) {
121          form.emit('error', err);
122        }
123      });
124
125      form.parse(req);
126
127      if (options.defer) {
128        req.form = form;
129        next();
130      }
131    });
132  }
133};