-
Notifications
You must be signed in to change notification settings - Fork 69
/
index.js
121 lines (97 loc) · 3.46 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
/*jslint node: true */
'use strict';
var FileInfo = require('./lib/fileinfo.js');
var configs = require('./lib/configs.js');
var formidable = require('formidable');
var fs = require('fs');
var path = require('path');
module.exports = uploadService;
function uploadService(opts) {
var options = configs.apply(opts);
var transporter = options.storage.type === 'local' ? require('./lib/transport/local.js') : require('./lib/transport/aws.js');
transporter = transporter(options);
var fileUploader = {};
fileUploader.config = options;
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"');
}
fileUploader.get = function(req, res, callback) {
this.config.host = req.headers.host;
setNoCacheHeaders(res);
transporter.get(callback);
};
fileUploader.post = function(req, res, callback) {
setNoCacheHeaders(res);
var form = new formidable.IncomingForm();
var tmpFiles = [];
var files = [];
var map = {};
var fields = {};
var redirect;
this.config.host = req.headers.host;
var configs = this.config;
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 = configs.tmpDir;
form.on('fileBegin', function(name, file) {
tmpFiles.push(file.path);
// fix #41
configs.saveFile = true;
var fileInfo = new FileInfo(file, configs, fields);
map[fileInfo.key] = fileInfo;
files.push(fileInfo);
}).on('field', function(name, value) {
fields[name] = value;
if (name === 'redirect') {
redirect = value;
}
}).on('file', function(name, file) {
var fileInfo = map[FileInfo.getFileKey(file.path)];
fileInfo.update(file);
if (!fileInfo.validate()) {
finish(fileInfo.error);
fs.unlink(file.path);
return;
}
transporter.post(fileInfo, file, finish);
}).on('aborted', function() {
finish('aborted');
tmpFiles.forEach(function(file) {
fs.unlink(file);
});
}).on('error', function(e) {
console.log('form.error', e);
finish(e);
}).on('progress', function(bytesReceived) {
if (bytesReceived > configs.maxPostSize) {
req.connection.destroy();
}
}).on('end', function() {
//if (configs.storage.type == 'local') {
// finish();
//}
}).parse(req);
};
fileUploader.delete = function(req, res, callback) {
transporter.delete(req, res, callback);
};
return fileUploader;
}