From e664a13f53e2e9e1fb254fe0e6d37f4096081e87 Mon Sep 17 00:00:00 2001 From: tomaszmadeyski Date: Tue, 4 Jul 2017 11:54:49 +0200 Subject: [PATCH] EZP-27551 Proper handling of missing images (#2036) * EZP-27551 Proper handling of missing images * EZP-27551 removing double method call * EZP-27551 adding missing arguments (cherry picked from commit 924b630ed068f90f1498d9635e3bffcbd247dbf4) --- eZ/Publish/Core/FieldType/Image/IO/Legacy.php | 16 +++++++++++----- eZ/Publish/Core/IO/TolerantIOService.php | 8 ++++++-- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/eZ/Publish/Core/FieldType/Image/IO/Legacy.php b/eZ/Publish/Core/FieldType/Image/IO/Legacy.php index eddd56e21..84366919b 100644 --- a/eZ/Publish/Core/FieldType/Image/IO/Legacy.php +++ b/eZ/Publish/Core/FieldType/Image/IO/Legacy.php @@ -8,10 +8,11 @@ */ namespace eZ\Publish\Core\FieldType\Image\IO; +use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException; use eZ\Publish\Core\IO\IOServiceInterface; use eZ\Publish\Core\IO\Values\BinaryFile; use eZ\Publish\Core\IO\Values\BinaryFileCreateStruct; -use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException; +use eZ\Publish\Core\IO\Values\MissingBinaryFile; /** * Legacy Image IOService. @@ -145,10 +146,15 @@ public function loadBinaryFile($binaryFileId) public function loadBinaryFileByUri($binaryFileUri) { try { - return $this->publishedIOService->loadBinaryFileByUri($binaryFileUri); - } - // InvalidArgumentException means that the prefix didn't match, NotFound can pass through - catch (InvalidArgumentException $prefixException) { + $image = $this->publishedIOService->loadBinaryFileByUri($binaryFileUri); + + if ($image instanceof MissingBinaryFile) { + throw new InvalidArgumentException('binaryFileUri', sprintf("Can't find file with url {0}", $binaryFileUri)); + } + + return $image; + } catch (InvalidArgumentException $prefixException) { + // InvalidArgumentException means that the prefix didn't match, NotFound can pass through try { return $this->draftIOService->loadBinaryFileByUri($binaryFileUri); } catch (InvalidArgumentException $e) { diff --git a/eZ/Publish/Core/IO/TolerantIOService.php b/eZ/Publish/Core/IO/TolerantIOService.php index 5c4997c8d..89ff1430b 100644 --- a/eZ/Publish/Core/IO/TolerantIOService.php +++ b/eZ/Publish/Core/IO/TolerantIOService.php @@ -99,12 +99,16 @@ public function loadBinaryFile($binaryFileId) public function loadBinaryFileByUri($binaryFileUri) { + $binaryFileId = $this->binarydataHandler->getIdFromUri($binaryFileUri); try { - $binaryFileId = $this->removeUriPrefix($this->binarydataHandler->getIdFromUri($binaryFileUri)); + $binaryFileId = $this->removeUriPrefix($binaryFileId); } catch (InvalidArgumentException $e) { $this->logMissingFile($binaryFileUri); - return new MissingBinaryFile(['uri' => $binaryFileUri]); + return new MissingBinaryFile([ + 'id' => $binaryFileId, + 'uri' => $binaryFileUri, + ]); } try {