diff --git a/Controller/ImagineController.php b/Controller/ImagineController.php index 817267b20..ebe365887 100644 --- a/Controller/ImagineController.php +++ b/Controller/ImagineController.php @@ -122,17 +122,17 @@ public function filterRuntimeAction(Request $request, $hash, $path, $filter) throw new NotFoundHttpException(sprintf('Source image could not be found for path "%s" and filter "%s"', $path, $filter), $e); } - $cachePrefix = 'rc/'.$hash; + $rcPath = $this->cacheManager->getRuntimePath($path, $filters); $this->cacheManager->store( $this->filterManager->applyFilter($binary, $filter, array( 'filters' => $filters, )), - $cachePrefix.'/'.$path, + $rcPath, $filter ); - return new RedirectResponse($this->cacheManager->resolve($cachePrefix.'/'.$path, $filter), 301); + return new RedirectResponse($this->cacheManager->resolve($rcPath, $filter), 301); } catch (RuntimeException $e) { throw new \RuntimeException(sprintf('Unable to create image for path "%s" and filter "%s". Message was "%s"', $hash.'/'.$path, $filter, $e->getMessage()), 0, $e); } diff --git a/Imagine/Cache/CacheManager.php b/Imagine/Cache/CacheManager.php index 93e55be2e..8ecc1f750 100644 --- a/Imagine/Cache/CacheManager.php +++ b/Imagine/Cache/CacheManager.php @@ -125,7 +125,12 @@ protected function getResolver($filter) public function getBrowserPath($path, $filter, array $runtimeConfig = array()) { if (!empty($runtimeConfig)) { - return $this->generateUrl($path, $filter, $runtimeConfig); + $rcPath = $this->getRuntimePath($path, $runtimeConfig); + + return $this->isStored($rcPath, $filter) ? + $this->resolve($rcPath, $filter) : + $this->generateUrl($path, $filter, $runtimeConfig) + ; } return $this->isStored($path, $filter) ? @@ -134,6 +139,17 @@ public function getBrowserPath($path, $filter, array $runtimeConfig = array()) ; } + /** + * Get path to runtime config image + * + * @param string $path + * @param array $runtimeConfig + */ + public function getRuntimePath($path, array $runtimeConfig) + { + return 'rc/'.$this->signer->sign($path, $runtimeConfig).'/'.$path; + } + /** * Returns a web accessible URL. * diff --git a/Tests/Imagine/Cache/CacheManagerTest.php b/Tests/Imagine/Cache/CacheManagerTest.php index dcd88e760..95fc44bb3 100644 --- a/Tests/Imagine/Cache/CacheManagerTest.php +++ b/Tests/Imagine/Cache/CacheManagerTest.php @@ -51,6 +51,20 @@ public function testGetBrowserPathWithoutResolver() $cacheManager->getBrowserPath('cats.jpeg', 'thumbnail'); } + public function testGetRuntimePath() + { + $config = $this->createFilterConfigurationMock(); + $cacheManager = new CacheManager($config, $this->createRouterMock(), new Signer('secret'), $this->createEventDispatcherMock()); + + $rcPath = $cacheManager->getRuntimePath('image.jpg', array( + 'thumbnail' => array( + 'size' => array(180, 180) + ) + )); + + $this->assertEquals('rc/ILfTutxX/image.jpg', $rcPath); + } + public function testDefaultResolverUsedIfNoneSetOnGetBrowserPath() { $resolver = $this->createResolverMock(); @@ -134,6 +148,53 @@ public function testFilterActionUrlGeneratedAndReturnIfResolverReturnNullOnGetBr $this->assertEquals('/media/cache/thumbnail/cats.jpeg', $actualBrowserPath); } + public function testFilterActionUrlGeneratedAndReturnIfResolverReturnNullOnGetBrowserPathWithRuntimeConfig() + { + $runtimeConfig = array( + 'thumbnail' => array( + 'size' => array(100, 100), + ) + ); + + $resolver = $this->createResolverMock(); + $resolver + ->expects($this->once()) + ->method('isStored') + ->with('rc/VhOzTGRB/cats.jpeg', 'thumbnail') + ->will($this->returnValue(false)) + ; + $resolver + ->expects($this->never()) + ->method('resolve') + ; + + $config = $this->createFilterConfigurationMock(); + $config + ->expects($this->atLeastOnce()) + ->method('get') + ->with('thumbnail') + ->will($this->returnValue(array( + 'size' => array(180, 180), + 'mode' => 'outbound', + 'cache' => null, + ))) + ; + + $router = $this->createRouterMock(); + $router + ->expects($this->once()) + ->method('generate') + ->will($this->returnValue('/media/cache/thumbnail/rc/VhOzTGRB/cats.jpeg')) + ; + + $cacheManager = new CacheManager($config, $router, new Signer('secret'), $this->createEventDispatcherMock()); + $cacheManager->addResolver('default', $resolver); + + $actualBrowserPath = $cacheManager->getBrowserPath('cats.jpeg', 'thumbnail', $runtimeConfig); + + $this->assertEquals('/media/cache/thumbnail/rc/VhOzTGRB/cats.jpeg', $actualBrowserPath); + } + /** * @dataProvider invalidPathProvider */