Only open target if source can be opened

This prevents empty files from being created if a remote file can't be opened.
This commit is contained in:
Bram Van der Sype 2019-08-08 11:12:37 +02:00
commit c67e9b9222

View file

@ -223,6 +223,12 @@ class NativeShare extends AbstractShare {
if (!$target) { if (!$target) {
throw new InvalidPathException('Invalid target path: Filename cannot be empty'); throw new InvalidPathException('Invalid target path: Filename cannot be empty');
} }
$sourceHandle = $this->getState()->open($this->buildUrl($source), 'r');
if (!$sourceHandle) {
throw new InvalidResourceException('Failed opening remote file "' . $source . '" for reading');
}
$targetHandle = @fopen($target, 'wb'); $targetHandle = @fopen($target, 'wb');
if (!$targetHandle) { if (!$targetHandle) {
$error = error_get_last(); $error = error_get_last();
@ -231,15 +237,10 @@ class NativeShare extends AbstractShare {
} else { } else {
$reason = 'Unknown error'; $reason = 'Unknown error';
} }
$this->getState()->close($sourceHandle);
throw new InvalidResourceException('Failed opening local file "' . $target . '" for writing: ' . $reason); throw new InvalidResourceException('Failed opening local file "' . $target . '" for writing: ' . $reason);
} }
$sourceHandle = $this->getState()->open($this->buildUrl($source), 'r');
if (!$sourceHandle) {
fclose($targetHandle);
throw new InvalidResourceException('Failed opening remote file "' . $source . '" for reading');
}
while ($data = $this->getState()->read($sourceHandle, NativeReadStream::CHUNK_SIZE)) { while ($data = $this->getState()->read($sourceHandle, NativeReadStream::CHUNK_SIZE)) {
fwrite($targetHandle, $data); fwrite($targetHandle, $data);
} }