forked from jpfluger/multer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
155 lines (118 loc) · 4.55 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
var os = require('os');
var fs = require('fs');
var path = require('path');
var crypto = require('crypto');
var Busboy = require('busboy');
var mkdirp = require('mkdirp');
module.exports = function(options) {
options = options || {};
// specify the destination directory, else, the uploads will be moved to the temporary dir of the system
var dest;
if (options.dest) {
dest = options.dest;
} else {
dest = os.tmpdir();
}
// make sure the dest dir exists
mkdirp(dest, function(err) { if (err) throw err; });
// 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) {
var random_string = fieldname + filename + Date.now() + Math.random();
return crypto.createHash('md5').update(random_string).digest('hex');
};
return function(req, res, next) {
req.body = req.body || {};
req.files = req.files || {};
if (req.headers['content-type'] &&
req.headers['content-type'].indexOf('multipart/form-data') === 0 &&
(req.method === 'POST' || req.method === 'PUT')
) {
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) {
// 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, fileStream, filename, encoding, mimetype) {
var ext, newFilename, newFilePath;
// don't attach to the files object, if there is file
if (!filename) return fileStream.resume();
ext = '.' + filename.split('.').slice(-1)[0];
newFilename = rename(fieldname, filename.replace(ext, '')) + ext;
newFilePath = path.join(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: null
};
// trigger "file upload start" event
if (options.onFileUploadStart) { options.onFileUploadStart(file); }
var ws = fs.createWriteStream(newFilePath);
fileStream.pipe(ws);
fileStream.on('data', function(data) {
if (data) { file.size += data.length; }
// trigger "file data" event
if (options.onFileUploadData) { options.onFileUploadData(file, data); }
});
fileStream.on('end', function() {
file.truncated = fileStream.truncated;
if (!req.files[fieldname]) { req.files[fieldname] = []; }
req.files[fieldname].push(file);
// trigger "file end" event
if (options.onFileUploadComplete) { options.onFileUploadComplete(file); }
});
fileStream.on('error', function(error) {
// trigger "file error" event
if (options.onError) { options.onError(error, next); }
else next(error);
});
ws.on('error', function(error) {
// trigger "file error" event
if (options.onError) { options.onError(error, next); }
else next(error);
});
});
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() {
for (var field in req.files) {
if (req.files[field].length === 1) {
req.files[field] = req.files[field][0];
}
}
// when done parsing the form, pass the control to the next middleware in stack
if (options.onParseEnd) { options.onParseEnd(); }
next();
});
req.pipe(busboy);
}
else { return next(); }
}
}