Skip to content

Commit 847f57f

Browse files
committed
add file width and height to fileInfo by @jake09
1 parent 2e7975c commit 847f57f

File tree

1 file changed

+58
-43
lines changed

1 file changed

+58
-43
lines changed

lib/uploadhandler.js

+58-43
Original file line numberDiff line numberDiff line change
@@ -108,55 +108,70 @@ module.exports = function (options) {
108108
.on('file', function (name, file) {
109109
counter++;
110110
var fileInfo = map[path.basename(file.path)];
111-
fs.exists(file.path, function(exists) {
112-
if (exists) {
113-
fileInfo.size = file.size;
114-
if (!fileInfo.validate()) {
115-
fs.unlink(file.path);
116-
finish();
117-
return;
118-
}
119111

120-
var generatePreviews = function () {
121-
if (options.imageTypes.test(fileInfo.name)) {
122-
_.each(options.imageVersions, function (value, version) {
123-
counter++;
124-
// creating directory recursive
125-
mkdirp(options.uploadDir() + '/' + version + '/', function (err, made) {
126-
var opts = options.imageVersions[version];
127-
imageMagick.resize({
128-
width: opts.width,
129-
height: opts.height,
130-
srcPath: options.uploadDir() + '/' + fileInfo.name,
131-
dstPath: options.uploadDir() + '/' + version + '/' + fileInfo.name,
132-
customArgs: opts.imageArgs || ['-auto-orient']
133-
}, finish);
134-
});
135-
});
112+
(function (_next) {
113+
if (options.imageTypes.test(fileInfo.name)) {
114+
imageMagick.identify(file.path, function (err, features) {
115+
if (!err) {
116+
fileInfo.width = features.width;
117+
fileInfo.height = features.height;
118+
}
119+
_next();
120+
});
121+
} else {
122+
_next();
123+
}
124+
})(function () {
125+
fs.exists(file.path, function(exists) {
126+
if (exists) {
127+
fileInfo.size = file.size;
128+
if (!fileInfo.validate()) {
129+
fs.unlink(file.path);
130+
finish();
131+
return;
136132
}
137-
}
138133

139-
mkdirp(options.uploadDir() + '/', function(err, made) {
140-
fs.rename(file.path, options.uploadDir() + '/' + fileInfo.name, function (err) {
141-
if (!err) {
142-
generatePreviews();
143-
finish();
144-
} else {
145-
var is = fs.createReadStream(file.path);
146-
var os = fs.createWriteStream(options.uploadDir() + '/' + fileInfo.name);
147-
is.on('end', function (err) {
148-
if (!err) {
149-
fs.unlink(file.path);
150-
generatePreviews();
151-
}
152-
finish();
134+
var generatePreviews = function () {
135+
if (options.imageTypes.test(fileInfo.name)) {
136+
_.each(options.imageVersions, function (value, version) {
137+
counter++;
138+
// creating directory recursive
139+
mkdirp(options.uploadDir() + '/' + version + '/', function (err, made) {
140+
var opts = options.imageVersions[version];
141+
imageMagick.resize({
142+
width: opts.width,
143+
height: opts.height,
144+
srcPath: options.uploadDir() + '/' + fileInfo.name,
145+
dstPath: options.uploadDir() + '/' + version + '/' + fileInfo.name,
146+
customArgs: opts.imageArgs || ['-auto-orient']
147+
}, finish);
148+
});
153149
});
154-
is.pipe(os);
155150
}
151+
}
152+
153+
mkdirp(options.uploadDir() + '/', function(err, made) {
154+
fs.rename(file.path, options.uploadDir() + '/' + fileInfo.name, function (err) {
155+
if (!err) {
156+
generatePreviews();
157+
finish();
158+
} else {
159+
var is = fs.createReadStream(file.path);
160+
var os = fs.createWriteStream(options.uploadDir() + '/' + fileInfo.name);
161+
is.on('end', function (err) {
162+
if (!err) {
163+
fs.unlink(file.path);
164+
generatePreviews();
165+
}
166+
finish();
167+
});
168+
is.pipe(os);
169+
}
170+
});
156171
});
157-
});
158-
}
159-
else finish();
172+
}
173+
else finish();
174+
});
160175
});
161176
})
162177
.on('aborted', function () {

0 commit comments

Comments
 (0)