Skip to content

Commit 83668f5

Browse files
author
Guntur Sarwohadi
committed
Collect formData on 'field' event from form property in UploadHandler.prototype.post method to req.fields object (created new object if undefined). Extend fileInfo with req.fields on 'end' event.
1 parent eb6e518 commit 83668f5

File tree

1 file changed

+182
-182
lines changed

1 file changed

+182
-182
lines changed

lib/uploadhandler.js

Lines changed: 182 additions & 182 deletions
Original file line numberDiff line numberDiff line change
@@ -1,188 +1,188 @@
11
var EventEmitter = require('events').EventEmitter,
2-
path = require('path'),
3-
fs = require('fs'),
4-
formidable = require('formidable'),
5-
imageMagick = require('imagemagick'),
6-
mkdirp = require('mkdirp'),
7-
_ = require('lodash');
2+
path = require('path'),
3+
fs = require('fs'),
4+
formidable = require('formidable'),
5+
imageMagick = require('imagemagick'),
6+
mkdirp = require('mkdirp'),
7+
_ = require('lodash');
88

99
module.exports = function (options) {
1010

11-
var FileInfo = require('./fileinfo')(
12-
_.extend({
13-
baseDir: options.uploadDir
14-
}, _.pick(options, 'minFileSize', 'maxFileSize', 'acceptFileTypes'))
15-
);
16-
17-
var UploadHandler = function (req, res, callback) {
18-
EventEmitter.call(this);
19-
this.req = req;
20-
this.res = res;
21-
this.callback = callback;
22-
};
23-
require('util').inherits(UploadHandler, EventEmitter);
24-
25-
UploadHandler.prototype.noCache = function () {
26-
this.res.set({
27-
'Pragma': 'no-cache',
28-
'Cache-Control': 'no-store, no-cache, must-revalidate',
29-
'Content-Disposition': 'inline; filename="files.json"'
30-
});
31-
};
32-
33-
UploadHandler.prototype.get = function () {
34-
this.noCache();
35-
var files = [];
36-
fs.readdir(options.uploadDir(), _.bind(function (err, list) {
37-
_.each(list, function (name) {
38-
var stats = fs.statSync(options.uploadDir() + '/' + name),
39-
fileInfo;
40-
if (stats.isFile()) {
41-
fileInfo = new FileInfo({
42-
name: name,
43-
size: stats.size
44-
});
45-
this.initUrls(fileInfo);
46-
files.push(fileInfo);
47-
}
48-
}, this);
49-
this.callback({files: files});
50-
}, this));
51-
};
52-
53-
UploadHandler.prototype.post = function () {
54-
var self = this,
55-
form = new formidable.IncomingForm(),
56-
tmpFiles = [],
57-
files = [],
58-
map = {},
59-
counter = 1,
60-
redirect,
61-
finish = _.bind(function () {
62-
if (!--counter) {
63-
_.each(files, function (fileInfo) {
64-
this.initUrls(fileInfo);
65-
fileInfo = _.extend(fileInfo, self.req.fields);
66-
this.emit('end', fileInfo);
67-
}, this);
68-
this.callback({files: files}, redirect);
69-
}
70-
}, this);
71-
72-
this.noCache();
73-
74-
form.uploadDir = options.tmpDir;
75-
form
76-
.on('fileBegin', function (name, file) {
77-
tmpFiles.push(file.path);
78-
var fileInfo = new FileInfo(file);
79-
fileInfo.safeName();
80-
map[path.basename(file.path)] = fileInfo;
81-
files.push(fileInfo);
82-
self.emit('begin', fileInfo);
83-
})
84-
.on('field', function (name, value) {
85-
if (name === 'redirect') {
86-
redirect = value;
87-
}
88-
if ( !self.req.fields )
89-
self.req.fields = {};
90-
self.req.fields[name] = value;
91-
})
92-
.on('file', function (name, file) {
93-
var fileInfo = map[path.basename(file.path)];
94-
if (fs.existsSync(file.path)) {
95-
fileInfo.size = file.size;
96-
if (!fileInfo.validate()) {
97-
fs.unlink(file.path);
98-
return;
99-
}
100-
101-
var generatePreviews = function () {
102-
if (options.imageTypes.test(fileInfo.name)) {
103-
_.each(options.imageVersions, function (value, version) {
104-
// creating directory recursive
105-
if (!fs.existsSync(options.uploadDir() + '/' + version + '/'))
106-
mkdirp.sync(options.uploadDir() + '/' + version + '/');
107-
108-
counter++;
109-
var opts = options.imageVersions[version];
110-
imageMagick.resize({
111-
width: opts.width,
112-
height: opts.height,
113-
srcPath: options.uploadDir() + '/' + fileInfo.name,
114-
dstPath: options.uploadDir() + '/' + version + '/' + fileInfo.name,
115-
customArgs: opts.imageArgs || ['-auto-orient']
116-
}, finish);
117-
});
118-
}
119-
}
120-
121-
if (!fs.existsSync(options.uploadDir() + '/'))
122-
mkdirp.sync(options.uploadDir() + '/');
123-
124-
counter++;
125-
fs.rename(file.path, options.uploadDir() + '/' + fileInfo.name, function (err) {
126-
if (!err) {
127-
generatePreviews();
128-
finish();
129-
} else {
130-
var is = fs.createReadStream(file.path);
131-
var os = fs.createWriteStream(options.uploadDir() + '/' + fileInfo.name);
132-
is.on('end', function (err) {
133-
if (!err) {
134-
fs.unlinkSync(file.path);
135-
generatePreviews();
136-
}
137-
finish();
138-
});
139-
is.pipe(os);
140-
}
141-
});
142-
}
143-
})
144-
.on('aborted', function () {
145-
_.each(tmpFiles, function (file) {
146-
var fileInfo = map[path.basename(file)];
147-
self.emit('abort', fileInfo);
148-
fs.unlink(file);
149-
});
150-
})
151-
.on('error', function (e) {
152-
self.emit('error', e);
153-
})
154-
.on('progress', function (bytesReceived, bytesExpected) {
155-
if (bytesReceived > options.maxPostSize)
156-
self.req.connection.destroy();
157-
})
158-
.on('end', finish)
159-
.parse(self.req);
160-
};
161-
162-
UploadHandler.prototype.destroy = function () {
163-
var self = this,
164-
fileName = path.basename(decodeURIComponent(this.req.url));
165-
166-
fs.unlink(options.uploadDir() + '/' + fileName, function (ex) {
167-
_.each(options.imageVersions, function (value, version) {
168-
fs.unlink(options.uploadDir() + '/' + version + '/' + fileName);
169-
});
170-
self.emit('delete', fileName);
171-
self.callback({success: !ex});
172-
});
173-
};
174-
175-
UploadHandler.prototype.initUrls = function (fileInfo) {
176-
var baseUrl = (options.ssl ? 'https:' : 'http:') + '//' + (options.hostname || this.req.get('Host'));
177-
fileInfo.setUrl(null, baseUrl + options.uploadUrl());
178-
fileInfo.setUrl('delete', baseUrl + this.req.originalUrl);
179-
_.each(options.imageVersions, function (value, version) {
180-
if (fs.existsSync(options.uploadDir() + '/' + version + '/' + fileInfo.name)) {
181-
fileInfo.setUrl(version, baseUrl + options.uploadUrl() + '/' + version);
182-
}
183-
}, this);
184-
};
185-
186-
return UploadHandler;
11+
var FileInfo = require('./fileinfo')(
12+
_.extend({
13+
baseDir: options.uploadDir
14+
}, _.pick(options, 'minFileSize', 'maxFileSize', 'acceptFileTypes'))
15+
);
16+
17+
var UploadHandler = function (req, res, callback) {
18+
EventEmitter.call(this);
19+
this.req = req;
20+
this.res = res;
21+
this.callback = callback;
22+
};
23+
require('util').inherits(UploadHandler, EventEmitter);
24+
25+
UploadHandler.prototype.noCache = function () {
26+
this.res.set({
27+
'Pragma': 'no-cache',
28+
'Cache-Control': 'no-store, no-cache, must-revalidate',
29+
'Content-Disposition': 'inline; filename="files.json"'
30+
});
31+
};
32+
33+
UploadHandler.prototype.get = function () {
34+
this.noCache();
35+
var files = [];
36+
fs.readdir(options.uploadDir(), _.bind(function (err, list) {
37+
_.each(list, function (name) {
38+
var stats = fs.statSync(options.uploadDir() + '/' + name),
39+
fileInfo;
40+
if (stats.isFile()) {
41+
fileInfo = new FileInfo({
42+
name: name,
43+
size: stats.size
44+
});
45+
this.initUrls(fileInfo);
46+
files.push(fileInfo);
47+
}
48+
}, this);
49+
this.callback({files: files});
50+
}, this));
51+
};
52+
53+
UploadHandler.prototype.post = function () {
54+
var self = this,
55+
form = new formidable.IncomingForm(),
56+
tmpFiles = [],
57+
files = [],
58+
map = {},
59+
counter = 1,
60+
redirect,
61+
finish = _.bind(function () {
62+
if (!--counter) {
63+
_.each(files, function (fileInfo) {
64+
this.initUrls(fileInfo);
65+
fileInfo = _.extend(fileInfo, self.req.fields);
66+
this.emit('end', fileInfo);
67+
}, this);
68+
this.callback({files: files}, redirect);
69+
}
70+
}, this);
71+
72+
this.noCache();
73+
74+
form.uploadDir = options.tmpDir;
75+
form
76+
.on('fileBegin', function (name, file) {
77+
tmpFiles.push(file.path);
78+
var fileInfo = new FileInfo(file);
79+
fileInfo.safeName();
80+
map[path.basename(file.path)] = fileInfo;
81+
files.push(fileInfo);
82+
self.emit('begin', fileInfo);
83+
})
84+
.on('field', function (name, value) {
85+
if (name === 'redirect') {
86+
redirect = value;
87+
}
88+
if ( !self.req.fields )
89+
self.req.fields = {};
90+
self.req.fields[name] = value;
91+
})
92+
.on('file', function (name, file) {
93+
var fileInfo = map[path.basename(file.path)];
94+
if (fs.existsSync(file.path)) {
95+
fileInfo.size = file.size;
96+
if (!fileInfo.validate()) {
97+
fs.unlink(file.path);
98+
return;
99+
}
100+
101+
var generatePreviews = function () {
102+
if (options.imageTypes.test(fileInfo.name)) {
103+
_.each(options.imageVersions, function (value, version) {
104+
// creating directory recursive
105+
if (!fs.existsSync(options.uploadDir() + '/' + version + '/'))
106+
mkdirp.sync(options.uploadDir() + '/' + version + '/');
107+
108+
counter++;
109+
var opts = options.imageVersions[version];
110+
imageMagick.resize({
111+
width: opts.width,
112+
height: opts.height,
113+
srcPath: options.uploadDir() + '/' + fileInfo.name,
114+
dstPath: options.uploadDir() + '/' + version + '/' + fileInfo.name,
115+
customArgs: opts.imageArgs || ['-auto-orient']
116+
}, finish);
117+
});
118+
}
119+
}
120+
121+
if (!fs.existsSync(options.uploadDir() + '/'))
122+
mkdirp.sync(options.uploadDir() + '/');
123+
124+
counter++;
125+
fs.rename(file.path, options.uploadDir() + '/' + fileInfo.name, function (err) {
126+
if (!err) {
127+
generatePreviews();
128+
finish();
129+
} else {
130+
var is = fs.createReadStream(file.path);
131+
var os = fs.createWriteStream(options.uploadDir() + '/' + fileInfo.name);
132+
is.on('end', function (err) {
133+
if (!err) {
134+
fs.unlinkSync(file.path);
135+
generatePreviews();
136+
}
137+
finish();
138+
});
139+
is.pipe(os);
140+
}
141+
});
142+
}
143+
})
144+
.on('aborted', function () {
145+
_.each(tmpFiles, function (file) {
146+
var fileInfo = map[path.basename(file)];
147+
self.emit('abort', fileInfo);
148+
fs.unlink(file);
149+
});
150+
})
151+
.on('error', function (e) {
152+
self.emit('error', e);
153+
})
154+
.on('progress', function (bytesReceived, bytesExpected) {
155+
if (bytesReceived > options.maxPostSize)
156+
self.req.connection.destroy();
157+
})
158+
.on('end', finish)
159+
.parse(self.req);
160+
};
161+
162+
UploadHandler.prototype.destroy = function () {
163+
var self = this,
164+
fileName = path.basename(decodeURIComponent(this.req.url));
165+
166+
fs.unlink(options.uploadDir() + '/' + fileName, function (ex) {
167+
_.each(options.imageVersions, function (value, version) {
168+
fs.unlink(options.uploadDir() + '/' + version + '/' + fileName);
169+
});
170+
self.emit('delete', fileName);
171+
self.callback({success: !ex});
172+
});
173+
};
174+
175+
UploadHandler.prototype.initUrls = function (fileInfo) {
176+
var baseUrl = (options.ssl ? 'https:' : 'http:') + '//' + (options.hostname || this.req.get('Host'));
177+
fileInfo.setUrl(null, baseUrl + options.uploadUrl());
178+
fileInfo.setUrl('delete', baseUrl + this.req.originalUrl);
179+
_.each(options.imageVersions, function (value, version) {
180+
if (fs.existsSync(options.uploadDir() + '/' + version + '/' + fileInfo.name)) {
181+
fileInfo.setUrl(version, baseUrl + options.uploadUrl() + '/' + version);
182+
}
183+
}, this);
184+
};
185+
186+
return UploadHandler;
187187
}
188188

0 commit comments

Comments
 (0)