Skip to content

Commit f36bb8a

Browse files
committed
reduce more sync operations
1 parent 46f5e37 commit f36bb8a

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

lib/uploadhandler.js

+22-13
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@ module.exports = function (options) {
4343
name: name,
4444
size: stats.size
4545
});
46-
this.initUrls(fileInfo);
47-
files.push(fileInfo);
46+
this.initUrls(fileInfo, function(err) {
47+
files.push(fileInfo);
48+
cb(err);
49+
return;
50+
});
4851
}
4952
}
5053
cb(err);
@@ -67,11 +70,15 @@ module.exports = function (options) {
6770
redirect,
6871
finish = _.bind(function () {
6972
if (!--counter) {
70-
_.each(files, function (fileInfo) {
71-
this.initUrls(fileInfo);
72-
this.emit('end', fileInfo);
73-
}, this);
74-
this.callback({files: files}, redirect);
73+
async.each(files, function(fileInfo, cb) {
74+
this.initUrls(fileInfo, function(err) {
75+
this.emit('end', fileInfo);
76+
cb(err);
77+
});
78+
},
79+
function(err) {
80+
this.callback({files: files}, redirect);
81+
});
7582
}
7683
}, this);
7784

@@ -175,15 +182,17 @@ module.exports = function (options) {
175182
});
176183
};
177184

178-
UploadHandler.prototype.initUrls = function (fileInfo) {
185+
UploadHandler.prototype.initUrls = function (fileInfo, cb) {
179186
var baseUrl = (options.ssl ? 'https:' : 'http:') + '//' + (options.hostname || this.req.get('Host'));
180187
fileInfo.setUrl(null, baseUrl + options.uploadUrl());
181188
fileInfo.setUrl('delete', baseUrl + this.req.originalUrl);
182-
_.each(options.imageVersions, function (value, version) {
183-
if (fs.existsSync(options.uploadDir() + '/' + version + '/' + fileInfo.name)) {
184-
fileInfo.setUrl(version, baseUrl + options.uploadUrl() + '/' + version);
185-
}
186-
}, this);
189+
async.each(Object.keys(options.imageVersions), function(version, cb) {
190+
fs.exists(options.uploadDir() + '/' + version + '/' + fileInfo.name, function(exists) {
191+
if (exists) fileInfo.setUrl(version, baseUrl + options.uploadUrl() + '/' + version);
192+
cb(null);
193+
})
194+
},
195+
cb);
187196
};
188197

189198
return UploadHandler;

0 commit comments

Comments
 (0)