-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmultibyr.js
226 lines (186 loc) · 6.34 KB
/
multibyr.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/**
* Copyright (C) 2018 Pieter E S Smith <https://github.com/smipi1>
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; version 2.1 or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along
* with this library; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
var os = require('os');
var fs = require('fs');
var path = require('path');
var crypto = require('crypto');
var Busboy = require('busboy');
function getDestFilename(fieldname, filename) {
var randomString = fieldname + filename + Date.now() + Math.random();
return crypto.createHash("md5").update(randomString).digest("hex");
};
function Multibyr(opts) {
if (!(this instanceof Multibyr)) {
return new Multibyr(opts);
}
this.opts = opts || {};
this.opts.dest = opts.dest || os.tmpdir();
this.opts.hooks = {
newData: (opts.hooks && opts.hooks.newData) ? opts.hooks.newData : function(file, data) {},
endData: (opts.hooks && opts.hooks.endData) ? opts.hooks.endData : function(err, file) {},
};
this.opts.getDestFilename = opts.getDestFilename || getDestFilename;
};
Multibyr.prototype.parse = function(req, res, cb) {
var callBack = cb;
function callBackOnce(err, files) {
if(callBack) {
var cb = callBack;
callBack = null;
return cb(err, files);
}
}
if(!req.hasOwnProperty('method')) {
return callBackOnce({ req: { method : "missing" } }, null);
}
if((req.method !== "POST") && (req.method !== "PUT")) {
return callBackOnce({ req: { method : req.method + "not supported" } }, null);
}
if(!req.hasOwnProperty('headers')) {
return callBackOnce({ req: { "headers" : "missing" } }, null);
}
if(!req.headers.hasOwnProperty("content-type")) {
return callBackOnce({ req: { headers: { "content-type" : "missing" } } }, null);
}
if(req.headers["content-type"].indexOf("multipart/form-data") !== 0) {
return callBackOnce({ req: { headers: { "content-type" : { "multipart/form-data" : "missing" } } } }, null);
}
var opts = this.opts;
return fs.stat(opts.dest, function(err, stats) {
if(err) {
return callBackOnce({ opts: { dest: err } }, null);
}
if(!stats.isDirectory()) {
return callBackOnce({ opts: { dest: opts.dest + " isn't a directory" } }, null);
}
var files = {};
req.body = req.body || {};
// Hand the opts to busboy with headers
opts.headers = req.headers;
var busboy = new Busboy(opts);
// handle text field data
busboy.on('field', function(fieldname, val, valTruncated, keyTruncated) {
// don't attach to the body object, if there is no value
if (!val) return;
if (req.body.hasOwnProperty(fieldname) && val) {
if (Array.isArray(req.body[fieldname])) {
req.body[fieldname].push(val);
} else {
req.body[fieldname] = [req.body[fieldname], val];
}
} else {
req.body[fieldname] = val;
}
});
// handle files
busboy.on('file', function(fieldname, readStream, filename, encoding, mimetype) {
var ext, newFilename, newFilePath;
if (!filename) {
// if the filename isn't specified, drop the file
return readStream.resume();
}
ext = '.' + filename.split('.').slice(-1)[0];
newFilename = opts.getDestFilename(fieldname, filename.replace(ext, '')) + ext;
newFilePath = path.join(opts.dest, newFilename);
var file = {
fieldname: fieldname,
originalname: filename,
name: newFilename,
encoding: encoding,
mimetype: mimetype,
path: newFilePath,
extension: (ext === null) ? null : ext.replace('.', ''),
size: 0,
truncated: true
};
files[fieldname] = file;
var writeStream = fs.createWriteStream(newFilePath);
readStream.pipe(writeStream);
readStream.on('data', function(data) {
if (data) {
file.size += data.length;
}
opts.hooks.newData(file, data);
});
function finish(err) {
var nullStream = fs.createWriteStream("/dev/null");
readStream.pipe(nullStream);
readStream.unpipe(writeStream);
writeStream.end();
opts.hooks.endData(err);
if(err) {
callBackOnce(err, files);
}
}
readStream.on('end', function() {
file.truncated = readStream.truncated;
finish(null);
});
readStream.on('error', function(error) {
file.truncated = true;
return finish({ readStream: error });
});
readStream.on('limit', function(error) {
file.truncated = true;
return finish({ limits: "fileSize" });
});
writeStream.on('error', function(error) {
file.truncated = true;
return finish({ writeStream: { filename: newFilePath, error: error} });
});
});
busboy.on('partsLimit', function() {
return callBackOnce({ limits: "parts" }, files);
});
busboy.on('filesLimit', function() {
return callBackOnce({ limits: "files" }, files);
});
busboy.on('fieldsLimit', function() {
return callBackOnce({ limits: "fields" }, files);
});
busboy.on('finish', function() {
for (var field in files) {
if (files[field].length === 1) {
files[field] = files[field][0];
}
}
return callBackOnce(null, files);
});
req.on('close', function() {
for (var field in files) {
if (files[field].length === 1) {
files[field] = files[field][0];
}
}
return callBackOnce(null, files);
});
req.pipe(busboy);
});
};
Multibyr.prototype.discard = function(files) {
if(!files) {
return;
}
for(var i in files) {
if(files.hasOwnProperty(i)) {
try {
fs.unlinkSync(files[i].path);
} catch(e) {
// Don't care if the file doesn't exist
}
}
}
};
module.exports = Multibyr;