middleware/bodyReader.js
function multipartEncoded(gw, callback){
  var upload = new formidable.IncomingForm();
  var files = {};
  var fields = {};    

  gw.eventium.on('end',cleanTemp);

  upload.uploadDir = tempDirectory;
  upload.keepExtensions = true;
  upload.onPart = function(part) {
    if (part.filename!="") {
      upload.handlePart(part);
    }
  }
  upload
    .on('progress', function(bytesReceived, bytesExpected) {
      var percent_complete = (bytesReceived / bytesExpected) * 100;
    })
    .on('error', function (error) {gw.error(500,error);})
    .on('field', function (field, value) {
      if (fields[field]) {
        if (!Array.isArray(fields[field])) {fields[field]=[fields[field]];}
        fields[field].push(value);
      } else {
        fields[field]=value;
      }
    })
    .on('file', function(field, file) {
      file.ext =  file.name.replace(/^.*\./,'');
      if (fields[field]) {
        if(!Array.isArray(fields[field])){fields[field]=[fields[field]];}
        fields[field].push(file);
      } else {
        fields[field]=file;
      }
      if (files[field]) {
        if (!Array.isArray(files[field])) {files[field]=[files[field]];}
        files[field].push(file);
      } else {
        files[field]=file;
      }
    })
    .on('end', function() {
      fields = gw.constructor.queryHeap(fields);
      gw.content.params = fields;
      for(var i=0,k=Object.keys(fields),l=k.length;i<l;i++){
        gw.params[k[i]]=fields[k[i]];
      }
      gw.files = files;
      callback();
    })
  ;
  upload.parse(gw.req);
}
function multipartEncoded(gw, callback){
  var upload = new formidable.IncomingForm();
  var files = {};
  var fields = {};    

  gw.eventium.on('end',cleanTemp);

  upload.uploadDir = tempDirectory;
  upload.keepExtensions = true;
  upload.onPart = function(part) {
    if (part.filename!="") {
      upload.handlePart(part);
    }
  }
  upload
    .on('progress', function(bytesReceived, bytesExpected) {
      var percent_complete = (bytesReceived / bytesExpected) * 100;
    })
    .on('error', function (error) {gw.error(500,error);})
    .on('field', function (field, value) {
      if (fields[field]) {
        if (!Array.isArray(fields[field])) {fields[field]=[fields[field]];}
        fields[field].push(value);
      } else {
        fields[field]=value;
      }
    })
    .on('file', function(field, file) {
      file.ext =  file.name.replace(/^.*\./,'');
      if (fields[field]) {
        if(!Array.isArray(fields[field])){fields[field]=[fields[field]];}
        fields[field].push(file);
      } else {
        fields[field]=file;
      }
      if (files[field]) {
        if (!Array.isArray(files[field])) {files[field]=[files[field]];}
        files[field].push(file);
      } else {
        files[field]=file;
      }
    })
    .on('end', function() {
      fields = gw.constructor.queryHeap(fields);
      gw.content.params = fields;
      for(var i=0,k=Object.keys(fields),l=k.length;i<l;i++){
        gw.params[k[i]]=fields[k[i]];
      }
      gw.files = files;
      callback();
    })
  ;
  upload.parse(gw.req);
}
function multipartEncoded ( gw , callback ) { var upload = new formidable . IncomingForm ( ) ; var files = { } ; var fields = { } ; gw . eventium . on ( 'end' , cleanTemp ) ; upload . uploadDir = tempDirectory ; upload . keepExtensions = true ; upload . onPart = function ( part ) { if ( part . filename != "" ) { upload . handlePart ( part ) ; } } upload . on ( 'progress' , function ( bytesReceived , bytesExpected ) { var percent_complete = ( bytesReceived / bytesExpected ) * 100 ; } ) . on ( 'error' , function ( error ) { gw . error ( 500 , error ) ; } ) . on ( 'field' , function ( field , value ) { if ( fields [ field ] ) { if ( ! Array . isArray ( fields [ field ] ) ) { fields [ field ] = [ fields [ field ] ] ; } fields [ field ] . push ( value ) ; } else { fields [ field ] = value ; } } ) . on ( 'file' , function ( field , file ) { file . ext = file . name . replace ( / ^.*\. / , '' ) ; if ( fields [ field ] ) { if ( ! Array . isArray ( fields [ field ] ) ) { fields [ field ] = [ fields [ field ] ] ; } fields [ field ] . push ( file ) ; } else { fields [ field ] = file ; } if ( files [ field ] ) { if ( ! Array . isArray ( files [ field ] ) ) { files [ field ] = [ files [ field ] ] ; } files [ field ] . push ( file ) ; } else { files [ field ] = file ; } } ) . on ( 'end' , function ( ) { fields = gw . constructor . queryHeap ( fields ) ; gw . content . params = fields ; for ( var i = 0 , k = Object . keys ( fields ) , l = k . length ; i < l ; i ++ ) { gw . params [ k [ i ] ] = fields [ k [ i ] ] ; } gw . files = files ; callback ( ) ; } ) ; upload . parse ( gw . req ) ; }
Multipart parser.
Multipart parser .