flysystem-stream-wrapper
flysystem-stream-wrapper copied to clipboard
Twistor\FlysystemStreamWrapper::stream_stat() sets size to 0 for at least some remote files, can result in empty files being written
I encountered this issue with https://github.com/thephpleague/flysystem-azure-blob-storage/issues/22 but I believe it could occur for other remote filesystems.
When I attempt copy with a remote file backed by flysystem-azure-blob-storage as the source it always writes 0 byte files at the destination. The culprit seems to be Twistor\FlysystemStreamWrapper::stream_stat() which sets the size of the returned $stat array to zero. This is because it calls Twistor\StreamUtil::getSize() which uses https://www.php.net/manual/en/function.fstat.php. There's a note on the documentation page:
Note: This function will not work on remote files as the file to be examined must be accessible via the server's filesystem.
I'm guessing that's why, though it fails in a much less obvious way than I'd expect from the note.
I'll attach a pull request shortly with a crude fix which ignores the fstat() filesize if it is zero and just uses the original file size from url_stat. I'm not sure if this is the best solution but it seems to work for me.
I faced similar issue, what i did was just to put default stat
// Use the size of our handle, since it could have been written to or
// truncated.
$stream_util_size = StreamUtil::getSize($this->handle);
if (!empty($stream_util_size)) {
// Only set size if stream_util_size is available or
// just send original file metadata.
$stat['size'] = $stat[7] = $stream_util_size;
}