-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
156 lines (123 loc) · 4.39 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
'use strict';
const path = require('path');
const fs = require('fs');
const uniqueFilename = require('unique-filename');
const splitBuffer = require('buffer-split');
const grab = (key, header) => {
if (!header) {
return;
}
const regex = RegExp('\\b' + key + '\\s*=\\s*("[^"]+"|\'[^\']+\'|[^;,]+)', 'i');
const cap = regex.exec(header);
if (cap) {
return cap[1].trim().replace(/^['"]|['"]$/g, '');
}
};
const parseHeaderInfos = headerBuffer => {
const headers = splitBuffer(headerBuffer, Buffer.from('\r\n'));
return headers.reduce((parsed, header) => {
header = header.toString();
const headerPair = header.split(':');
const headerKey = headerPair.length >= 2 ? headerPair[0].trim().toLowerCase() : false;
switch (headerKey) {
case 'content-disposition':
const name = grab('name', header);
const filename = grab('filename', header);
if (name) {
parsed.name = name;
}
if (filename) {
parsed.filename = filename;
parsed.isFile = true;
}
break;
case 'content-type':
parsed.type = headerPair[1].trim();
break;
case 'content-transfer-encoding':
parsed.encoding = headerPair[1].trim();
break;
}
return parsed;
}, {});
};
const parse = (body, boundary) => {
const buffers = splitBuffer(body, Buffer.from('--' + boundary));
// Throw away start (mostly empty string) and end (end delimiter data) buffers
// Remove trailing newline
const parts = buffers.slice(1, -1).map(part => part.slice(0, -2));
const parsedRequest = {
files: {},
fields: {}
};
return parts.map(part => {
// Parse headers and body of each part
const doubleNewlineBuffer = Buffer.from('\r\n\r\n');
const headersAndBody = splitBuffer(part, doubleNewlineBuffer);
if (headersAndBody.length < 2) {
return;
}
const infos = parseHeaderInfos(headersAndBody.shift());
// Rejoin multiple body parts, if separated by splitBuffer
let data = headersAndBody.reduce((body, bodyPart) => {
if (!body) {
return bodyPart;
}
return Buffer.concat([body, doubleNewlineBuffer, bodyPart]);
});
return Object.assign(infos, {
data
});
}).reduce((parsed, part) => {
// Map parts to files or fields
if (part && part.name) {
const partSet = parsed[part.isFile ? 'files' : 'fields'];
if (partSet[part.name]) {
if (Array.isArray(partSet[part.name])) {
partSet[part.name].push(part);
} else {
partSet[part.name] = [partSet[part.name], part];
}
} else {
partSet[part.name] = part;
}
}
return parsed;
}, parsedRequest);
};
module.exports = opts => {
const options = Object.assign({
prefix: 'multipart',
dest: false,
mapFiles: files => files,
mapFields: fields => fields
}, opts);
return (req, res, next) => {
const boundary = grab('boundary', req.headers['content-type']);
const form = parse(req.body, boundary);
if (!form || !form.files || !form.fields) {
return next();
}
if (options.dest) {
// Write files to disk
Object.keys(form.files).forEach(fileKey => {
let files = form.files[fileKey];
if (!Array.isArray(files)) {
files = [files];
}
files.forEach(file => {
const filepath = uniqueFilename(options.dest, options.prefix);
file.path = path.resolve(filepath);
fs.writeFileSync(file.path, file.data);
});
});
}
// User defined map functions
form.files = options.mapFiles(form.files);
form.fields = options.mapFields(form.fields);
// Map files to req.files and data to req.fields
req.files = Object.assign(form.files, req.files);
req.fields = Object.assign(form.fields, req.fields);
return next();
};
};