5

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!

Bill
  • 25,119
  • 8
  • 94
  • 125

1 Answers1

7

You're on the right track by rewriting form.onPart. Formidable writes to disk by default, so you want to act before it does.

Parts themselves are Streams, so you can pipe them to whatever you want, including gm. I haven't tested it, but this makes sense based on the documentation:

var form = new formidable.IncomingForm;
form.onPart = function (part) {
  if (!part.filename) return this.handlePart(part);

  gm(part).resize(200, 200).stream(function (err, stdout, stderr) {
    stdout.pipe(fs.createWriteStream('my/new/path/to/img.png'));
  });
};

As for the middleware, I'd copypaste the multipart middleware from Connect/Express and add the onPart function to it: http://www.senchalabs.org/connect/multipart.html

It'd be a lot nicer if formidable didn't write to disk by default or if it took a flag, wouldn't it? You could send them an issue.

juandopazo
  • 6,283
  • 2
  • 26
  • 29
  • Thank you! This got me 95% of the way there. My last problem is that `gm` is async and so the form parsing returns before the resize is actually complete (meaning the url I had back to the client isn't any good yet). Is there a way to let `formidable` know when the `part` handling is actually complete? – Bill Aug 03 '12 at 23:49
  • Actually, I just ended up handling this on the client instead - works out better that way. Thanks again! – Bill Aug 04 '12 at 01:13
  • 1
    stdout is a Stream. That's why you can pipe() it to a file WriteStream. And that means it has an "end" event you can listen to. Just do stdout.on('end', fn) and that's when you can tell the client the job is done. – juandopazo Aug 04 '12 at 16:17