Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #373. Replace NotFoundHttpException with SourceNotFoundException #403

Merged
merged 3 commits into from
Apr 28, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Binary/Loader/AbstractDoctrineLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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));
Expand Down
7 changes: 4 additions & 3 deletions Binary/Loader/FileSystemLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions Binary/Loader/GridFSLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions Binary/Loader/StreamLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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.
Expand Down
9 changes: 8 additions & 1 deletion Controller/ImagineController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down
10 changes: 10 additions & 0 deletions Exception/Binary/Loader/NotLoadableException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Liip\ImagineBundle\Exception\Binary\Loader;

use Liip\ImagineBundle\Exception\ExceptionInterface;

class NotLoadableException extends \RuntimeException implements ExceptionInterface
{

}
6 changes: 3 additions & 3 deletions Tests/Binary/Loader/FileSystemLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function testThrowExceptionIfPathHasDoublePointSlashAtBegging()
);

$this->setExpectedException(
'Symfony\Component\HttpKernel\Exception\NotFoundHttpException',
'Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException',
'Source image was searched with'
);

Expand All @@ -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'
);

Expand All @@ -82,7 +82,7 @@ public function testThrowExceptionIfFileNotExist()
);

$this->setExpectedException(
'Symfony\Component\HttpKernel\Exception\NotFoundHttpException',
'Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException',
'Source image not found'
);

Expand Down
4 changes: 3 additions & 1 deletion Tests/Binary/Loader/StreamLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
9 changes: 9 additions & 0 deletions Tests/Functional/Controller/ImagineControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down