diff --git a/Binary/Loader/AbstractDoctrineLoader.php b/Binary/Loader/AbstractDoctrineLoader.php index f65189560..a5fd915f8 100644 --- a/Binary/Loader/AbstractDoctrineLoader.php +++ b/Binary/Loader/AbstractDoctrineLoader.php @@ -3,7 +3,7 @@ namespace Liip\ImagineBundle\Binary\Loader; use Doctrine\Common\Persistence\ObjectManager; -use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; +use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException; abstract class AbstractDoctrineLoader implements LoaderInterface { @@ -64,7 +64,7 @@ public function find($path) } if (!$image) { - throw new NotFoundHttpException(sprintf('Source image not found with id "%s"', $path)); + throw new NotLoadableException(sprintf('Source image was not found with id "%s"', $path)); } return stream_get_contents($this->getStreamFromImage($image)); diff --git a/Binary/Loader/FileSystemLoader.php b/Binary/Loader/FileSystemLoader.php index 959e7b975..aa7a7c41c 100644 --- a/Binary/Loader/FileSystemLoader.php +++ b/Binary/Loader/FileSystemLoader.php @@ -2,11 +2,12 @@ namespace Liip\ImagineBundle\Binary\Loader; + use Liip\ImagineBundle\Model\Binary; use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser; use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesserInterface; use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface; -use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; +use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException; class FileSystemLoader implements LoaderInterface { @@ -47,13 +48,13 @@ public function __construct( public function find($path) { if (false !== strpos($path, '../')) { - throw new NotFoundHttpException(sprintf("Source image was searched with '%s' out side of the defined root path", $path)); + throw new NotLoadableException(sprintf("Source image was searched with '%s' out side of the defined root path", $path)); } $absolutePath = $this->rootPath.'/'.ltrim($path, '/'); if (false == file_exists($absolutePath)) { - throw new NotFoundHttpException(sprintf('Source image not found in "%s"', $absolutePath)); + throw new NotLoadableException(sprintf('Source image not found in "%s"', $absolutePath)); } $mimeType = $this->mimeTypeGuesser->guess($absolutePath); diff --git a/Binary/Loader/GridFSLoader.php b/Binary/Loader/GridFSLoader.php index f1ee3b424..b6bb67e02 100644 --- a/Binary/Loader/GridFSLoader.php +++ b/Binary/Loader/GridFSLoader.php @@ -3,7 +3,7 @@ namespace Liip\ImagineBundle\Binary\Loader; use Doctrine\ODM\MongoDB\DocumentManager; -use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; +use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException; class GridFSLoader implements LoaderInterface { @@ -37,7 +37,7 @@ public function find($id) ->find(new \MongoId($id)); if (!$image) { - throw new NotFoundHttpException(sprintf('Source image not found with id "%s"', $id)); + throw new NotLoadableException(sprintf('Source image was not found with id "%s"', $id)); } return $image['file']->getBytes(); diff --git a/Binary/Loader/StreamLoader.php b/Binary/Loader/StreamLoader.php index ebe808a8a..d5d81d30b 100644 --- a/Binary/Loader/StreamLoader.php +++ b/Binary/Loader/StreamLoader.php @@ -2,7 +2,7 @@ namespace Liip\ImagineBundle\Binary\Loader; -use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; +use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException; class StreamLoader implements LoaderInterface { @@ -52,7 +52,7 @@ public function find($path) * file_exists() is not used as not all wrappers support stat() to actually check for existing resources. */ if (($this->context && !$resource = @fopen($name, 'r', null, $this->context)) || !$resource = @fopen($name, 'r')) { - throw new NotFoundHttpException('Source image not found.'); + throw new NotLoadableException('Source image not found.'); } // Closing the opened stream to avoid locking of the resource to find. diff --git a/Controller/ImagineController.php b/Controller/ImagineController.php index 348f2b5fa..6b3b38d24 100644 --- a/Controller/ImagineController.php +++ b/Controller/ImagineController.php @@ -6,10 +6,12 @@ use Liip\ImagineBundle\Imagine\Cache\CacheManager; use Liip\ImagineBundle\Imagine\Data\DataManager; use Liip\ImagineBundle\Imagine\Filter\FilterManager; +use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\UriSigner; class ImagineController @@ -79,7 +81,12 @@ public function filterAction(Request $request, $path, $filter) } if (!$this->cacheManager->isStored($path, $filter)) { - $binary = $this->dataManager->find($filter, $path); + try { + $binary = $this->dataManager->find($filter, $path); + } catch (NotLoadableException $e) { + + throw new NotFoundHttpException('Source image could not be found', $e); + } $this->cacheManager->store( $this->filterManager->applyFilter($binary, $filter, $runtimeConfig), diff --git a/Exception/Binary/Loader/NotLoadableException.php b/Exception/Binary/Loader/NotLoadableException.php new file mode 100644 index 000000000..3cc90d5f2 --- /dev/null +++ b/Exception/Binary/Loader/NotLoadableException.php @@ -0,0 +1,10 @@ +setExpectedException( - 'Symfony\Component\HttpKernel\Exception\NotFoundHttpException', + 'Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException', 'Source image was searched with' ); @@ -66,7 +66,7 @@ public function testThrowExceptionIfPathHasDoublePointSlashInTheMiddle() ); $this->setExpectedException( - 'Symfony\Component\HttpKernel\Exception\NotFoundHttpException', + 'Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException', 'Source image was searched with' ); @@ -82,7 +82,7 @@ public function testThrowExceptionIfFileNotExist() ); $this->setExpectedException( - 'Symfony\Component\HttpKernel\Exception\NotFoundHttpException', + 'Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException', 'Source image not found' ); diff --git a/Tests/Binary/Loader/StreamLoaderTest.php b/Tests/Binary/Loader/StreamLoaderTest.php index 09e640198..d44d13c07 100644 --- a/Tests/Binary/Loader/StreamLoaderTest.php +++ b/Tests/Binary/Loader/StreamLoaderTest.php @@ -14,7 +14,9 @@ public function testThrowsIfInvalidPathGivenOnFind() { $loader = new StreamLoader('file://'); - $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException'); + $this->setExpectedException( + 'Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException' + ); $loader->find($this->tempDir.'/invalid.jpeg'); } diff --git a/Tests/Functional/Controller/ImagineControllerTest.php b/Tests/Functional/Controller/ImagineControllerTest.php index f81bb68c4..980bce5de 100644 --- a/Tests/Functional/Controller/ImagineControllerTest.php +++ b/Tests/Functional/Controller/ImagineControllerTest.php @@ -94,6 +94,15 @@ public function testThrowBadRequestIfSignInvalidWhileUsingCustomFilters() ))); } + /** + * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException + * @expectedExceptionMessage Source image could not be found + */ + public function testShouldThrowNotFoundHttpExceptionIfFileNotExists() + { + $this->client->request('GET', '/media/cache/thumbnail_web_path/images/shrodinger_cats_which_not_exist.jpeg'); + } + public function testShouldResolveWithCustomFiltersPopulatingCacheFirst() { /** @var UriSigner $uriSigner */