Merge pull request #83 from Brammm-forks/master

Only open target if source can be opened
This commit is contained in:
Robin Appelman 2019-08-08 11:42:26 +02:00 committed by GitHub
commit e6c7a6763b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

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);
} }