I'm using Express.js
and have a route to upload images that I then need to resize. Currently I just let Express
write the file to disk (which I think uses node-formidable
under the covers) and then resize using gm
(http://aheckmann.github.com/gm/) which writes a second version to disk.
gm(path)
.resize(540,404)
.write(dest, function (err) { ... });
I've read that you can get a hold of the node-formidable
file stream before it writes it to disk, and since gm
can accept a stream instead of just a path, I should be able to pass this right through eliminating the double write to disk.
I think I need to override form.onPart
but I'm not sure where (should it be done as Express
middleware?) and I'm not sure how to get a hold of form
or what exactly to do with the part
. This is the code skeleton that I've seen in a few places:
form.onPart = function(part) {
if (!part.filename) { form.handlePart(part); return; }
part.on('data', function(buffer) {
});
part.on('end', function() {
}
}
Can somebody help me put these two pieces together? Thanks!