2015-11-22 28 views
1

我需要使用graphicsmagick来操作图像。使用graphicsmagick在collectionFS中进行相同的读取和写入流

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

无法像这样同时读取和写入同一文件;有效地,当你还在读取文件时,你将覆盖部分文件。最好的办法是写入一个单独的文件,然后在完成后重命名它。 –

+0

@ExplosionPills这是有道理的:-)我不熟悉使用流。我怎样才能使用临时文件? – user3848987

回答

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

这给了我'ReferenceError:fs没有定义'。我可以使用商店的“任何”和“某物”(如我的示例代码中所示)并将“某物”重命名为“任何物品”?我的意思是'anything-writeTo'应该是'something'... – user3848987

+0

@ user3848987改为'file'而不是'fs'。你可以使用任何你想要的名字,但想法是你不会尝试写入一个现有的文件。 –

+0

fs = Npm.require('fs'); – Tobi

相关问题