var busboy = require('connect-busboy');
var fs = require('fs-extra');
var streamifier = require('streamifier');

module.exports = function(options) {
  options = options || {};

  return function(req, res, next) {
    return busboy(options)(req, res, function() {

      // If no busboy req obj, then no uploads are taking place
      if (!req.busboy)
        return next();

      req.files = null;

      req.busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) {
        req.body = req.body || {};
        req.body[fieldname] = val;
      });

      req.busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
        var buf = new Buffer(0);
        var safeFileNameRegex = /[^\w-]/g;

        file.on('data', function(data) {
          buf = Buffer.concat([buf, data]);

          if (options.debug) {
            return console.log('Uploading %s -> %s', fieldname, filename);
          }
        });

        file.on('end', function() {
          if (!req.files)
            req.files = {};
          
          // see: https://github.com/richardgirges/express-fileupload/issues/14
          // firefox uploads empty file in case of cache miss when f5ing page.
          // resulting in unexpected behavior. if there is no file data, the file is invalid.
          if(!buf.length) 
            return;

          if (options.safeFileNames) {
            if (typeof options.safeFileNames === 'object')
              safeFileNameRegex = options.safeFileNames;

            filename = filename.replace(safeFileNameRegex, '');

            console.log('filename yo', filename);
          }

          return req.files[fieldname] = {
            name: filename,
            data: buf,
            encoding: encoding,
            mimetype: mimetype,
            mv: function(path, callback) {
              var fstream;
              fstream = fs.createWriteStream(path);
              streamifier.createReadStream(buf).pipe(fstream);
              fstream.on('error', function(error) {
                callback(error);
              });
              fstream.on('close', function() {
                callback(null);
              });
            }
          };
        });
      });

      req.busboy.on('finish', next);

      req.pipe(req.busboy);
    });
  };
};

@@ -1665,35 +1665,19 @@
     
-return req.files%5Bfieldname%5D
+var newFile
  = %7B
@@ -2203,16 +2203,342 @@
       %7D;
+%0A%0A          if (!req.files.hasOwnProperty(fieldname)) %7B%0A            req.files%5Bfieldname%5D = newFile;%0A          %7D else %7B%0A            if (req.files%5Bfieldname%5D instanceof Array)%0A                req.files%5Bfieldname%5D.push(newFile);%0A            else%0A                req.files%5Bfieldname%5D = %5Breq.files%5Bfieldname%5D, newFile%5D;%0A          %7D
 %0A       

Fix input fields with multiple enabled: - req.files[fieldname] is an Object when no multiple files are sent in the same field. - req.files[fieldname] is an Array of Objects when multiple files are sent for fieldname.