thephpleague / flysystem-replicate-adapter

Flysystem Adapter Decorator for Replicating Filesystems.
24 stars 11 forks source link

Stream replicate incorrect #2

Closed Leechael closed 9 years ago

Leechael commented 9 years ago

For case like:


$fp = fopen('/path/to/file', 'rb+');
$source = new \League\Flysystem\Adapter\Local('/path/to/source/directory');
$replica = new \League\Flysystem\Adapter\Local('/path/to/replica/directory');
$filesystem = new \League\Flysystem\Filesystem(new \League\Flysystem\Replicate\ReplicateAdapter($source, $replica));
$filesystem->wirteStream('foo', $fp);

But the file on replica will be zero bytes. The possible solution are do fseek before write to replica; but some streams aren't seek-able.

Any comment?

Leechael commented 9 years ago

The dirty and quick solution works for me:


    public function writeStream($path, $resource, Config $config)
    {
        if (! $this->source->writeStream($path, $resource, $config)) {
            return false;
        }

        if (fseek($resource, 0) === 0) {
            return $this->replica->writeStream($path, $resource, $config);
        } else {
            $src = $this->source->readStream($path);
            return $this->replica->writeStream($path, $src, $config);
        }
    }
twistor commented 9 years ago

@frankdejonge This can be closed as well.