I use Express.jsand have a route to upload images, which I then need to change. Currently, I just let you Expresswrite the file to disk (which I think uses node-formidableunder the covers) and then resize it using gm(http://aheckmann.github.com/gm/), which writes the second version to disk.
gm(path)
.resize(540,404)
.write(dest, function (err) { ... });
I read that you can capture a file stream node-formidablebefore writing it to disk, and since it gmcan receive a stream instead of a simple path, I should be able to transfer this right by eliminating double write to disk.
I think I need to override form.onPart, but I'm not sure where (should it be done as Expressmiddleware?), And I'm not sure how to get the trick formor what exactly to do with part. This is the code skeleton that I saw in several places:
form.onPart = function(part) {
if (!part.filename) { form.handlePart(part); return; }
part.on('data', function(buffer) {
});
part.on('end', function() {
}
}
Can someone help me assemble these two parts? Thank!
source
share