forked from expressjs/multer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
213 lines (168 loc) · 6.25 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
var os = require('os');
var fs = require('fs');
var path = require('path');
var crypto = require('crypto');
var Busboy = require('busboy');
var mkdirp = require('mkdirp');
var is = require('type-is');
var qs = require('qs');
function multer(req, res, options){
options = options || {};
options.includeEmptyFields = options.includeEmptyFields || false;
options.inMemory = options.inMemory || false;
options.putSingleFilesInArray = options.putSingleFilesInArray || false;
var readFinished = false;
var fileCount = 0;
var dest;
if (options.dest) {
dest = options.dest;
} else {
dest = os.tmpdir();
}
// renaming function for the destination directory
var changeDest = options.changeDest || function(dest, req, res) {
return dest;
};
// renaming function for the uploaded file - need not worry about the extension
// ! if you want to keep the original filename, write a renamer function which does that
var rename = options.rename || function(fieldname, filename, req, res) {
var random_string = fieldname + filename + Date.now() + Math.random();
return crypto.createHash('md5').update(random_string).digest('hex');
};
mkdirp(dest, function(err) { if (err) throw err; });
req.body = req.body || {};
req.files = req.files || {};
if (is(req, ['multipart'])) {
if (options.onParseStart) { options.onParseStart(); }
// add the request headers to the options
options.headers = req.headers;
var busboy = new Busboy(options);
// handle text field data
busboy.on('field', function(fieldname, val, valTruncated, keyTruncated) {
// if includeEmptyFields is false and there is no value then don't
// attach the fields to req.body
if (!options.includeEmptyFields && !val) return;
if (req.body.hasOwnProperty(fieldname)) {
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, fileStream, filename, encoding, mimetype) {
var ext, newFilename, newFilePath;
// don't attach to the files object, if there is no file
if (!filename) return fileStream.resume();
// defines is processing a new file
fileCount++;
if (filename.indexOf('.') > 0) { ext = '.' + filename.split('.').slice(-1)[0]; }
else { ext = ''; }
newFilename = rename(fieldname, filename.replace(ext, ''), req, res) + ext;
newFilePath = path.join(changeDest(dest, req, res), newFilename);
var file = {
fieldname: fieldname,
originalname: filename,
name: newFilename,
encoding: encoding,
mimetype: mimetype,
path: newFilePath,
extension: (ext === '') ? '' : ext.replace('.', ''),
size: 0,
truncated: null,
buffer: null
};
// trigger "file upload start" event
if (options.onFileUploadStart) {
var proceed = options.onFileUploadStart(file, req, res);
// if the onFileUploadStart handler returned null, it means we should proceed further, discard the file!
if (proceed === false) {
fileCount--;
return fileStream.resume();
}
}
var bufs = [];
var ws;
if (!options.inMemory) {
ws = fs.createWriteStream(newFilePath);
fileStream.pipe(ws);
}
fileStream.on('data', function(data) {
if (data) {
if (options.inMemory) bufs.push(data);
file.size += data.length;
}
// trigger "file data" event
if (options.onFileUploadData) { options.onFileUploadData(file, data, req, res); }
});
function onFileStreamEnd() {
file.truncated = fileStream.truncated;
if (!req.files[fieldname]) { req.files[fieldname] = []; }
if (options.inMemory) file.buffer = Buffer.concat(bufs, file.size);
req.files[fieldname].push(file);
// trigger "file end" event
if (options.onFileUploadComplete) { options.onFileUploadComplete(file, req, res); }
// defines has completed processing one more file
fileCount--;
onFinish();
}
if (options.inMemory)
fileStream.on('end', onFileStreamEnd);
else
ws.on('finish', onFileStreamEnd);
fileStream.on('error', function(error) {
// trigger "file error" event
if (options.onError) { options.onError(error, next); }
else next(error);
});
fileStream.on('limit', function () {
if (options.onFileSizeLimit) { options.onFileSizeLimit(file); }
});
function onFileStreamError(error) {
// trigger "file error" event
if (options.onError) { options.onError(error, next); }
else next(error);
}
if (options.inMemory)
fileStream.on('error', onFileStreamError );
else
ws.on('error', onFileStreamError );
});
busboy.on('partsLimit', function() {
if (options.onPartsLimit) { options.onPartsLimit(); }
});
busboy.on('filesLimit', function() {
if (options.onFilesLimit) { options.onFilesLimit(); }
});
busboy.on('fieldsLimit', function() {
if (options.onFieldsLimit) { options.onFieldsLimit(); }
});
busboy.on('finish', function() {
readFinished = true;
onFinish();
});
/**
* Pass the control to the next middleware in stack
* only if the read and write stream are finished
*/
var onFinish = function () {
if (!readFinished || fileCount > 0) return;
if (!options.putSingleFilesInArray) {
for (var field in req.files) {
if (req.files[field].length === 1) {
req.files[field] = req.files[field][0];
}
}
}
// Parse the body and create a best structure
req.body = qs.parse(req.body);
// when done parsing the form, pass the control to the next middleware in stack
if (options.onParseEnd) { options.onParseEnd(req, next); }
};
req.pipe(busboy);
}
}
module.exports = multer;