-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathindex.js
196 lines (174 loc) · 6.05 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
'use strict';
/*
* Copyright (c) 2014, Yahoo Inc. All rights reserved.
* Copyrights licensed under the New BSD License.
* See the accompanying LICENSE file for terms.
*/
const busboy = require('connect-busboy');
const key = '@express-busboy';
const path = require('path');
const uuid = require('uuid');
const fs = require('fs');
const mkdirp = require('mkdirp');
const qs = require('qs');
const os = require('os');
const jsonBody = require('body/json');
const crypto = require('crypto');
const fixDups = (item) => {
Object.keys(item).forEach((field) => {
if (Array.isArray(item[field])) {
item[`__${field}__`] = item[field];
item[field] = item[field][0];
}
});
return item;
};
const md5 = (value) => {
return crypto.createHash('md5').update(value).digest('hex');
};
const strip = (value) => {
return md5(value);
};
var convertParams = (item, name, data) => {
if (Array.isArray(item[name])) {
item[name].push(data);
} else if (item[name]) {
item[name] = [item[name], data];
} else {
item[name] = data;
}
};
exports.extend = function(app, options) {
if (app[key]) {
return app;
}
Object.defineProperty(app, key, { value: exports });
options = options || {};
options.immediate = false; //Remove if the user sets it
options.parseBody = typeof options.parseBody === 'boolean' ? options.parseBody : true;
options.path = options.path || path.join(os.tmpdir(), 'express-busboy');
options.strip = options.strip && options.strip instanceof Function ? options.strip : strip;
const restrictMultiple = options.restrictMultiple;
const mimeTypeLimit = options.mimeTypeLimit
? !Array.isArray(options.mimeTypeLimit)
? [options.mimeTypeLimit]
: options.mimeTypeLimit
: null;
delete options.restrictMultiple;
delete options.mimeTypeLimit;
app.use(busboy(options));
app.use((req, res, next) => {
var allowUpload = true;
req.body = req.body || {};
req.files = req.files || {};
if (req.is('json') && req.readable) {
if (!options.parseBody) {
// skip parsing body if parseBody is false
next();
return;
}
jsonBody(req, res, options, (err, body) => {
req.body = body || {};
next();
});
return;
}
if (!req.busboy) {
//Nothing to parse..
return next();
}
if (options.allowedPath) {
allowUpload = false;
/*istanbul ignore else - else case is the default case*/
if (options.allowedPath === req.url) {
allowUpload = true;
} else if (typeof options.allowedPath === 'function') {
allowUpload = !!options.allowedPath(req.url);
} else if (typeof options.allowedPath.test === 'function') {
allowUpload = !!options.allowedPath.test(req.url);
}
}
let bbDone = false;
const finish = () => {
/*istanbul ignore next*/
if (!bbDone) {
return; //bail busboy is still running
}
//Busboy is done, check the files
let complete = true;
Object.keys(req.files).forEach((name) => {
let file = req.files[name];
if (!Array.isArray(file)) {
file = [file];
}
file.forEach((f) => {
if (!f.done) {
//file is not done writing
complete = false;
}
});
});
if (complete) {
//all files are done writing..
next();
}
};
if (options.upload && allowUpload) {
req.busboy.on('file', (name, file, info) => {
const { filename, encoding, mimeType } = info;
const fileUuid = uuid.v4();
const nameStripped = options.strip(name, 'name');
const filenameStripped = options.strip(filename || /*istanbul ignore next*/ '', 'filename');
const out = path.join(options.path, '/', fileUuid, '/', nameStripped, filenameStripped);
if (
mimeTypeLimit &&
!mimeTypeLimit.some((type) => {
return type === mimeType;
})
) {
return file.resume();
}
/*istanbul ignore next*/
if (!filename || filenameStripped === '') {
return file.on('data', () => {});
}
const data = {
uuid: fileUuid,
field: name,
file: out,
filename: filename,
encoding: encoding,
mimetype: mimeType,
truncated: false,
done: false
};
mkdirp.sync(path.dirname(out));
const writer = fs.createWriteStream(out);
writer.on('finish', () => {
data.done = true;
finish();
});
file.pipe(writer);
// Indicate whether the file was truncated
/*istanbul ignore next*/
file.on('limit', () => {
data.truncated = true;
});
convertParams(req.files, name, data);
});
}
req.busboy.on('field', (name, data) => {
convertParams(req.body, name, data);
});
req.busboy.on('close', () => {
req.body = qs.parse(qs.stringify(req.body), { arrayLimit: options.arrayLimit });
if (restrictMultiple) {
[req.body, req.files].forEach(fixDups);
}
bbDone = true;
finish();
});
req.pipe(req.busboy);
});
return app;
};