forked from lennym/busboy-body-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
55 lines (48 loc) · 1.71 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
var Busboy = require('busboy'),
bytes = require('bytes'),
concat = require('concat-stream'),
debug = require('debug')('busboy-body-parser');
module.exports = function (settings) {
settings = settings || {};
settings.limit = settings.limit || Math.Infinity;
if (typeof settings.limit === 'string') {
settings.limit = bytes(settings.limit);
}
return function multipartBodyParser(req, res, next) {
if (req.is('multipart/form-data')) {
var busboy = new Busboy({
headers: req.headers,
limits: {
fileSize: settings.limit
}
});
busboy.on('field', function (key, value) {
debug('Received field %s: %s', key, value);
req.body[key] = value;
});
busboy.on('file', function (key, file, name, enc, mimetype) {
file.pipe(concat(function (d) {
debug('Received file %s', file);
req.files[key] = {
data: file.truncated ? null : d,
name: name,
encoding: enc,
mimetype: mimetype,
truncated: file.truncated,
size: Buffer.byteLength(d.toString('binary'), 'binary')
};
}));
});
busboy.on('finish', function () {
debug('Finished form parsing');
debug(req.body);
next();
});
req.files = req.files || {};
req.body = req.body || {};
req.pipe(busboy);
} else {
next();
}
};
};