Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
Merge pull request #2091 from fauvel/issue-2091
Browse files Browse the repository at this point in the history
Throw if S3 upload fails
  • Loading branch information
fauvel committed Feb 25, 2016
2 parents 9fe6a70 + 1d45989 commit af2c75f
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions library/CM/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,7 @@ public function copyToFile(CM_File $file) {
} elseif ($adapterSource instanceof CM_File_Filesystem_Adapter_StreamInterface &&
$adapterDestination instanceof CM_File_Filesystem_Adapter_StreamInterface
) {
$streamSource = $adapterSource->getStreamRead($this->getPath());
$streamDestination = $adapterDestination->getStreamWrite($file->getPath());
stream_copy_to_stream($streamSource, $streamDestination);
@fclose($streamDestination);
@fclose($streamSource);
$this->_copyToFileStreaming($file, $adapterSource, $adapterDestination);
} else {
$file->write($this->read());
}
Expand Down Expand Up @@ -398,4 +394,31 @@ public function equals(CM_Comparable $other = null) {
$sameFilesystemAdapter = $this->_filesystem->equals($other->_filesystem);
return ($samePath && $sameFilesystemAdapter);
}

/**
* @param CM_File $file
* @param CM_File_Filesystem_Adapter $adapterSource
* @param CM_File_Filesystem_Adapter $adapterDestination
* @throws CM_Exception
*/
protected function _copyToFileStreaming(CM_File $file, CM_File_Filesystem_Adapter $adapterSource, CM_File_Filesystem_Adapter $adapterDestination) {
/** @var CM_File_Filesystem_Adapter_StreamInterface $adapterSource */
/** @var CM_File_Filesystem_Adapter_StreamInterface $adapterDestination */
$streamSource = $adapterSource->getStreamRead($this->getPath());
$streamDestination = $adapterDestination->getStreamWrite($file->getPath());
$sizeDestination = stream_copy_to_stream($streamSource, $streamDestination);
$sizeSource = $this->getSize();
if ($sizeSource !== $sizeDestination) {
throw new CM_Exception('Copy of `' . $this->getPath() . '` to `' . $file->getPath() . '` failed', null, [
'Source size' => $sizeSource,
'Bytes copied' => $sizeDestination,
]);
}
if (!@fclose($streamDestination)) {
throw new CM_Exception('Could not close stream for `' . $file->getPath() . '`');
}
if (!@fclose($streamSource)) {
throw new CM_Exception('Could not close stream for `' . $this->getPath() . '`');
}
}
}

0 comments on commit af2c75f

Please sign in to comment.