forked from expressjs/multer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
125 lines (95 loc) · 3.75 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
var os = require('os');
var fs = require('fs');
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;
// some users may ommit the trailing slash
if (options.dest) { dest = options.dest.slice(-1) == '/' ? options.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') {
//if (req.method === 'POST') {
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) {
req.body[fieldname] = val;
});
// handle files
busboy.on('file', function(fieldname, fileStream, filename, encoding, mimetype) {
if (!filename) {
filename = "";
}
var ext = '.' + filename.split('.').slice(-1)[0];
var newFilename = rename(fieldname, filename.replace(ext, '')) + ext;
var path = dest + newFilename;
var file = {
fieldname: fieldname,
originalname: filename,
name: newFilename,
encoding: encoding,
mimetype: mimetype,
path: path,
extension: ext.replace('.', '')
};
// trigger "file upload start" event
if (options.onFileUploadStart) { options.onFileUploadStart(file); }
var ws = fs.createWriteStream(path);
fileStream.pipe(ws);
fileStream.on('data', function(data) {
// trigger "file data" event
if (options.onFileUploadData) { options.onFileUploadData(file, data); }
});
fileStream.on('end', function() {
req.files[fieldname] = 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('end', function() {
// 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();
}
};
};