Stream文件通过gm上传到Express.js,以消除双重写入 [英] Stream file uploaded with Express.js through gm to eliminate double write

查看:90
本文介绍了Stream文件通过gm上传到Express.js,以消除双重写入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Express.js ,并有路由上传图像,然后我需要调整大小。目前我只是让 Express 将文件写入磁盘(我认为使用 node-formidable 在封面下),然后使用向磁盘写入第二个版本的 gm (http://aheckmann.github.com/gm/)调整大小。



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$

我已经读过,可以扣住$ code节点强大文件流,然后将其写入磁盘,由于 gm 可以接受流,而不仅仅是一个路径,我应该能够传递这个权限通过消除对磁盘的双重写入。



我想我需要覆盖 form.onPart 但我不是确定在哪里(应该如 Express 中间件那样完成?),我不知道如何获得表单或者与部分完全相关。这是我在几个地方看到的代码框架:

  form.onPart = function(part){
if(!part.filename){form.handlePart(part);返回; }

part.on('data',function(buffer){

});
part.on('end',function(){

}
}

有人可以帮我把这两件事放在一起吗?谢谢!

解决方案

你在通过重写 form.onPart 正确的轨道。默认情况下,强制写入磁盘,所以您希望在它之前采取行动。



零件本身就是Streams,所以你可以把它们管理到任何你想要的东西,包括 gm 。我还没有测试过,但是这个文档是有道理的:

  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'));
});
};

对于中间件,我将从Connect / E中复制 multipart 中间件xpress并添加 onPart 函数: http:// www。 senchalabs.org/connect/multipart.html



如果强大的默认情况下没有写入磁盘或者如果它标记了,不是吗?您可以向他们发送问题。


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!

解决方案

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.

这篇关于Stream文件通过gm上传到Express.js,以消除双重写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆