forked from arvindr21/blueimp-file-upload-expressjs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
150 lines (129 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
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
/*jslint node: true */
'use strict';
var fs = require('fs');
var path = require('path');
var formidable = require('formidable');
var FileInfo = require('./lib/fileinfo');
var configs = require('./lib/configs');
function getTransporters(opts, storages){
var transporters = [];
storages.forEach(function(storage){
switch(storage.type){
case 'local':
transporters.push(require('./lib/transport/local')(opts, storage));
break;
case 's3':
transporters.push(require('./lib/transport/aws')(opts, storage));
break;
}
});
return transporters;
}
function calls(transporters, method, params, callback){
var count = transporters.length;
function cb(){
if (0 === --count) return callback.apply(null, arguments);
}
transporters.forEach(function(transporter){
transporter[method].apply(transporter, params.concat(cb));
});
}
function getFileKey(filePath) {
return path.basename(filePath);
}
function setNoCacheHeaders(res) {
res.setHeader('Pragma', 'no-cache');
res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate');
res.setHeader('Content-Disposition', 'inline; filename="files.json"');
}
function validate(fileInfo, opts) {
if (opts.minFileSize && opts.minFileSize > fileInfo.size) {
fileInfo.error = 'File is too small';
} else if (opts.maxFileSize && opts.maxFileSize < fileInfo.size) {
fileInfo.error = 'File is too big';
} else if (!opts.acceptFileTypes.test(fileInfo.name)) {
fileInfo.error = 'Filetype not allowed';
}
return !fileInfo.error;
}
module.exports = function uploadService(opts) {
var options = configs(opts);
var storages = options.storages;
var transporters = getTransporters(options, storages);
return {
get: function(req, res, callback) {
options.host = req.headers.host;
transporters[0].get(callback);
},
post: function(req, res, callback) {
setNoCacheHeaders(res);
var form = new formidable.IncomingForm();
var tmpFiles = [];
var files = [];
var fields = {};
var redirect;
options.host = req.headers.host;
req.body = req.body || {};
function finish(error, fileInfo) {
if (error) return callback(error, {
files: files
}, redirect);
if (!fileInfo) return callback(null, {
files: files
}, redirect);
var allFilesProccessed = true;
files.forEach(function(file, idx) {
allFilesProccessed = allFilesProccessed && file.proccessed;
});
if (allFilesProccessed) {
callback(null, {
files: files
}, redirect);
}
}
form.uploadDir = options.tmpDir;
form.maxFieldsSize = options.maxPostSize;
form.maxFileSize = options.maxFileSize;
form.on('fileBegin', function(name, file) {
}).on('field', function(name, value) {
fields[name] = value;
if (name === 'redirect') {
redirect = value;
}
}).on('file', function(name, file) {
tmpFiles.push(file.path);
options.saveFile = true;
transporters.forEach(function(transporter, i){
var fileInfo = transporter.createFileInfo(file, fields);
if (!validate(fileInfo, options)) {
finish(fileInfo.error);
fs.unlink(file.path);
return;
}
if (!i) {
files.push(fileInfo);
transporter.post(fileInfo, file, finish);
} else {
transporter.post(fileInfo, file, function(){});
}
});
}).on('aborted', function() {
// error will be triggered after this
tmpFiles.forEach(function(file) {
fs.unlink(file);
});
}).on('error', function(e) {
console.log('form.error', e);
finish(e);
}).on('progress', function(bytesReceived) {
if (bytesReceived > options.maxPostSize) {
req.connection.destroy();
}
}).on('end', function() {
}).parse(req);
},
delete: function(req, res, callback) {
calls(transporters, 'delete', [req, res], callback);
}
};
}