|
1 | 1 | var busboy = require('connect-busboy'), |
2 | | - fs = require('fs-extra'), |
3 | | - streamifier = require('streamifier'); |
| 2 | + fs = require('fs-extra'), |
| 3 | + streamifier = require('streamifier'); |
4 | 4 |
|
5 | 5 | module.exports = function(options) { |
6 | | - options = options || {}; |
| 6 | + options = options || {}; |
7 | 7 |
|
8 | | - return function(req, res, next) { |
9 | | - return busboy(options)(req, res, function() { |
| 8 | + return function(req, res, next) { |
| 9 | + return busboy(options)(req, res, function() { |
10 | 10 |
|
11 | | - // If no busboy req obj, then no uploads are taking place |
12 | | - if (!req.busboy) |
13 | | - return next(); |
| 11 | + // If no busboy req obj, then no uploads are taking place |
| 12 | + if (!req.busboy) |
| 13 | + return next(); |
14 | 14 |
|
15 | | - req.files = null; |
| 15 | + req.files = null; |
16 | 16 |
|
17 | | - req.busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) { |
18 | | - req.body = req.body || {}; |
19 | | - req.body[fieldname] = val; |
20 | | - }); |
| 17 | + req.busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) { |
| 18 | + req.body = req.body || {}; |
| 19 | + req.body[fieldname] = val; |
| 20 | + }); |
21 | 21 |
|
22 | | - req.busboy.on('file', function(fieldname, file, filename, encoding, mimetype) { |
23 | | - var buf = new Buffer(0); |
| 22 | + req.busboy.on('file', function(fieldname, file, filename, encoding, mimetype) { |
| 23 | + var buf = new Buffer(0); |
24 | 24 |
|
25 | | - file.on('data', function(data) { |
26 | | - buf = Buffer.concat([buf, data]); |
27 | | - if (options.debug) { |
28 | | - return console.log('Uploading %s -> %s', fieldname, filename); |
29 | | - } |
30 | | - }); |
| 25 | + file.on('data', function(data) { |
| 26 | + buf = Buffer.concat([buf, data]); |
| 27 | + if (options.debug) { |
| 28 | + return console.log('Uploading %s -> %s', fieldname, filename); |
| 29 | + } |
| 30 | + }); |
31 | 31 |
|
32 | | - file.on('end', function() { |
33 | | - if (!req.files) |
34 | | - req.files = {}; |
35 | | - |
36 | | - |
37 | | - // see: https://github.com/richardgirges/express-fileupload/issues/14 |
38 | | - // firefox uploads empty file in case of cache miss when f5ing page. |
39 | | - // resulting in unexpected behavior. if there is no file data, the file is invalid. |
40 | | - |
41 | | - if(!buf.length) |
42 | | - return; |
| 32 | + file.on('end', function() { |
| 33 | + if (!req.files) |
| 34 | + req.files = {}; |
| 35 | + |
| 36 | + |
| 37 | + // see: https://github.com/richardgirges/express-fileupload/issues/14 |
| 38 | + // firefox uploads empty file in case of cache miss when f5ing page. |
| 39 | + // resulting in unexpected behavior. if there is no file data, the file is invalid. |
| 40 | + |
| 41 | + if(!buf.length) |
| 42 | + return; |
43 | 43 |
|
44 | | - return req.files[fieldname] = { |
45 | | - name: filename, |
46 | | - data: buf, |
47 | | - encoding: encoding, |
48 | | - mimetype: mimetype, |
49 | | - mv: function(path, callback) { |
50 | | - var fstream; |
51 | | - fstream = fs.createWriteStream(path); |
52 | | - streamifier.createReadStream(buf).pipe(fstream); |
53 | | - fstream.on('error', function(error) { |
54 | | - callback(error); |
55 | | - }); |
56 | | - fstream.on('close', function() { |
57 | | - callback(null); |
58 | | - }); |
59 | | - } |
60 | | - }; |
61 | | - |
62 | | - |
63 | | - }); |
64 | | - }); |
| 44 | + return req.files[fieldname] = { |
| 45 | + name: filename, |
| 46 | + data: buf, |
| 47 | + encoding: encoding, |
| 48 | + mimetype: mimetype, |
| 49 | + mv: function(path, callback) { |
| 50 | + var fstream; |
| 51 | + fstream = fs.createWriteStream(path); |
| 52 | + streamifier.createReadStream(buf).pipe(fstream); |
| 53 | + fstream.on('error', function(error) { |
| 54 | + callback(error); |
| 55 | + }); |
| 56 | + fstream.on('close', function() { |
| 57 | + callback(null); |
| 58 | + }); |
| 59 | + } |
| 60 | + }; |
| 61 | + |
| 62 | + |
| 63 | + }); |
| 64 | + }); |
65 | 65 |
|
66 | | - req.busboy.on('finish', next); |
| 66 | + req.busboy.on('finish', next); |
67 | 67 |
|
68 | | - req.pipe(req.busboy); |
69 | | - }); |
70 | | - }; |
| 68 | + req.pipe(req.busboy); |
| 69 | + }); |
| 70 | + }; |
71 | 71 | }; |
0 commit comments