【问题标题】:Same read- and write-stream in collectionFS using graphicsmagick使用 graphicsmagick 在 collectionFS 中相同的读取和写入流
【发布时间】:2015-11-22 04:01:28
【问题描述】:

我需要使用graphicsmagick 操作图像。

我的 FSCollection 如下所示:

Images = new FS.Collection("media", {
    stores: [
        new FS.Store.FileSystem("anything"),
        new FS.Store.FileSystem("something")
    ],
});

我的问题是,writeStream 应该和 readStream 一样。这不起作用,因为这会导致结果为空:

var read  = file.createReadStream('anything'),
    write = file.createWriteStream('anything');

gm(read)
    .crop(100,100,10,10)
.stream()
.on('end',function(){ console.log('done'); })
.on('error',function(err){ console.warn(err); })
.pipe(write, function (error) {
    if (error) console.log(error);
    else console.log('ok');
});

【问题讨论】:

  • 你不能像这样同时读取和写入同一个文件;实际上,当您仍在读取文件时,您将覆盖文件的某些部分。最好的办法是写入一个单独的文件,然后在完成后重命名它。
  • @ExplosionPills 这很有道理 :-) 我不太熟悉使用流。如何使用临时文件?

标签: javascript meteor graphicsmagick collectionfs


【解决方案1】:

同时读取和写入同一个文件是不可能的,因为您将在尝试读取内容的同时覆盖内容。写入不同的文件,然后将其重命名为原始文件。

var read  = file.createReadStream('anything'),
    write = file.createWriteStream('anything-writeTo');

gm(read)
    .crop(100,100,10,10)
.stream()
.on('error',function(err){ console.warn(err); })
.pipe(write, function (error) {
    if (error) console.log(error);
    else console.log('ok');
})
.on('end',function(){
    file.rename("anything-writeTo", "anything", function (err) {
        if (err) console.error(err);
        else console.log('rename complete');
    });
})

【讨论】:

  • 这给了我ReferenceError: fs is not defined。我可以使用商店'anything'和'something'(如我的示例代码中所示)并将'something'重命名为'anything'吗?我的意思是 'anything-writeTo' 应该是 'something'...
  • @user3848987 更改为 file 而不是 fs。您可以使用任何您想要的名称,但您不会尝试写入现有文件。
  • fs = Npm.require('fs');
猜你喜欢
  • 1970-01-01
  • 2014-07-02
  • 2016-02-11
  • 2019-01-24
  • 2015-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多