diff --git a/lib/index.js b/lib/index.js index e803de7..7c844ed 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,71 +1,71 @@ var busboy = require('connect-busboy'), - fs = require('fs-extra'), - streamifier = require('streamifier'); + fs = require('fs-extra'), + streamifier = require('streamifier'); module.exports = function(options) { - options = options || {}; + options = options || {}; - return function(req, res, next) { - return busboy(options)(req, res, function() { + 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(); + // If no busboy req obj, then no uploads are taking place + if (!req.busboy) + return next(); - req.files = null; + 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('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); + req.busboy.on('file', function(fieldname, file, filename, encoding, mimetype) { + var buf = new Buffer(0); - file.on('data', function(data) { - buf = Buffer.concat([buf, data]); - if (options.debug) { - return console.log('Uploading %s -> %s', fieldname, filename); - } - }); + 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; + 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; - 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); - }); - } - }; - - - }); - }); + 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.busboy.on('finish', next); - req.pipe(req.busboy); - }); - }; + req.pipe(req.busboy); + }); + }; }; diff --git a/test/upload.test.js b/test/upload.test.js index ff31204..52e41a1 100644 --- a/test/upload.test.js +++ b/test/upload.test.js @@ -1,6 +1,6 @@ var express = require('express'), - fileUpload = require('../lib/index.js'), - app = express(); + fileUpload = require('../lib/index.js'), + app = express(); app.use('/form', express.static(__dirname + '/upload.test.html')); @@ -8,31 +8,31 @@ app.use('/form', express.static(__dirname + '/upload.test.html')); app.use(fileUpload()); app.get('/ping', function(req, res) { - res.send('pong'); + res.send('pong'); }); app.post('/upload', function(req, res) { - var sampleFile, uploadPath; + var sampleFile, uploadPath; - if (!req.files) { - res.status(400).send('No files were uploaded.'); - return; - } + if (!req.files) { + res.status(400).send('No files were uploaded.'); + return; + } - sampleFile = req.files.sampleFile; + sampleFile = req.files.sampleFile; - uploadPath = __dirname + '/uploadedfiles/' + sampleFile.name; + uploadPath = __dirname + '/uploadedfiles/' + sampleFile.name; - sampleFile.mv(uploadPath, function(err) { - if (err) { - res.status(500).send(err); - } - else { - res.send('File uploaded to ' + uploadPath); - } - }); + sampleFile.mv(uploadPath, function(err) { + if (err) { + res.status(500).send(err); + } + else { + res.send('File uploaded to ' + uploadPath); + } + }); }); app.listen(8000, function() { - console.log('Express server listening on port 8000'); + console.log('Express server listening on port 8000'); }) \ No newline at end of file