vendor/liip/imagine-bundle/Controller/ImagineController.php line 90

  1. <?php
  2. /*
  3.  * This file is part of the `liip/LiipImagineBundle` project.
  4.  *
  5.  * (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE.md
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Liip\ImagineBundle\Controller;
  11. use Imagine\Exception\RuntimeException;
  12. use Liip\ImagineBundle\Config\Controller\ControllerConfig;
  13. use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException;
  14. use Liip\ImagineBundle\Exception\Imagine\Filter\NonExistingFilterException;
  15. use Liip\ImagineBundle\Imagine\Cache\Helper\PathHelper;
  16. use Liip\ImagineBundle\Imagine\Cache\SignerInterface;
  17. use Liip\ImagineBundle\Imagine\Data\DataManager;
  18. use Liip\ImagineBundle\Service\FilterService;
  19. use Symfony\Component\HttpFoundation\Exception\BadRequestException;
  20. use Symfony\Component\HttpFoundation\RedirectResponse;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  23. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  24. use Symfony\Component\HttpKernel\Kernel;
  25. class ImagineController
  26. {
  27.     /**
  28.      * @var FilterService
  29.      */
  30.     private $filterService;
  31.     /**
  32.      * @var DataManager
  33.      */
  34.     private $dataManager;
  35.     /**
  36.      * @var SignerInterface
  37.      */
  38.     private $signer;
  39.     /**
  40.      * @var ControllerConfig
  41.      */
  42.     private $controllerConfig;
  43.     public function __construct(
  44.         FilterService $filterService,
  45.         DataManager $dataManager,
  46.         SignerInterface $signer,
  47.         ?ControllerConfig $controllerConfig null
  48.     ) {
  49.         $this->filterService $filterService;
  50.         $this->dataManager $dataManager;
  51.         $this->signer $signer;
  52.         if (null === $controllerConfig) {
  53.             @trigger_error(sprintf(
  54.                 'Instantiating "%s" without a forth argument of type "%s" is deprecated since 2.2.0 and will be required in 3.0.'self::class, ControllerConfig::class
  55.             ), E_USER_DEPRECATED);
  56.         }
  57.         $this->controllerConfig $controllerConfig ?? new ControllerConfig(301);
  58.     }
  59.     /**
  60.      * This action applies a given filter to a given image, saves the image and redirects the browser to the stored
  61.      * image.
  62.      *
  63.      * The resulting image is cached so subsequent requests will redirect to the cached image instead applying the
  64.      * filter and storing the image again.
  65.      *
  66.      * @param string $path
  67.      * @param string $filter
  68.      *
  69.      * @throws RuntimeException
  70.      * @throws NotFoundHttpException
  71.      *
  72.      * @return RedirectResponse
  73.      */
  74.     public function filterAction(Request $request$path$filter)
  75.     {
  76.         $path PathHelper::urlPathToFilePath($path);
  77.         $resolver $request->get('resolver');
  78.         return $this->createRedirectResponse(function () use ($path$filter$resolver$request) {
  79.             return $this->filterService->getUrlOfFilteredImage(
  80.                 $path,
  81.                 $filter,
  82.                 $resolver,
  83.                 $this->isWebpSupported($request)
  84.             );
  85.         }, $path$filter);
  86.     }
  87.     /**
  88.      * This action applies a given filter -merged with additional runtime filters- to a given image, saves the image and
  89.      * redirects the browser to the stored image.
  90.      *
  91.      * The resulting image is cached so subsequent requests will redirect to the cached image instead applying the
  92.      * filter and storing the image again.
  93.      *
  94.      * @param string $hash
  95.      * @param string $path
  96.      * @param string $filter
  97.      *
  98.      * @throws RuntimeException
  99.      * @throws BadRequestHttpException
  100.      * @throws NotFoundHttpException
  101.      *
  102.      * @return RedirectResponse
  103.      */
  104.     public function filterRuntimeAction(Request $request$hash$path$filter)
  105.     {
  106.         $resolver $request->get('resolver');
  107.         $path PathHelper::urlPathToFilePath($path);
  108.         $runtimeConfig $this->getFiltersBc($request);
  109.         if (true !== $this->signer->check($hash$path$runtimeConfig)) {
  110.             throw new BadRequestHttpException(sprintf('Signed url does not pass the sign check for path "%s" and filter "%s" and runtime config %s'$path$filterjson_encode($runtimeConfig)));
  111.         }
  112.         return $this->createRedirectResponse(function () use ($path$filter$runtimeConfig$resolver$request) {
  113.             return $this->filterService->getUrlOfFilteredImageWithRuntimeFilters(
  114.                 $path,
  115.                 $filter,
  116.                 $runtimeConfig,
  117.                 $resolver,
  118.                 $this->isWebpSupported($request)
  119.             );
  120.         }, $path$filter$hash);
  121.     }
  122.     private function getFiltersBc(Request $request): array
  123.     {
  124.         if (version_compare(Kernel::VERSION'5.1''>=')) {
  125.             try {
  126.                 return $request->query->all('filters');
  127.             } catch (BadRequestException $e) {
  128.                 // for strict BC - BadRequestException seems more suited to this situation.
  129.                 // remove the try-catch in version 3
  130.                 throw new NotFoundHttpException(sprintf('Filters must be an array. Value was "%s"'$request->query->get('filters')));
  131.             }
  132.         }
  133.         $runtimeConfig $request->query->get('filters', []);
  134.         if (!\is_array($runtimeConfig)) {
  135.             throw new NotFoundHttpException(sprintf('Filters must be an array. Value was "%s"'$runtimeConfig));
  136.         }
  137.         return $runtimeConfig;
  138.     }
  139.     private function createRedirectResponse(\Closure $urlstring $pathstring $filter, ?string $hash null): RedirectResponse
  140.     {
  141.         try {
  142.             return new RedirectResponse($url(), $this->controllerConfig->getRedirectResponseCode());
  143.         } catch (NotLoadableException $exception) {
  144.             if (null !== $this->dataManager->getDefaultImageUrl($filter)) {
  145.                 return new RedirectResponse($this->dataManager->getDefaultImageUrl($filter));
  146.             }
  147.             throw new NotFoundHttpException(sprintf('Source image for path "%s" could not be found'$path), $exception);
  148.         } catch (NonExistingFilterException $exception) {
  149.             throw new NotFoundHttpException(sprintf('Requested non-existing filter "%s"'$filter), $exception);
  150.         } catch (RuntimeException $exception) {
  151.             throw new \RuntimeException(vsprintf('Unable to create image for path "%s" and filter "%s". Message was "%s"', [$hash sprintf('%s/%s'$hash$path) : $path$filter$exception->getMessage()]), 0$exception);
  152.         }
  153.     }
  154.     private function isWebpSupported(Request $request): bool
  155.     {
  156.         return false !== mb_stripos($request->headers->get('accept'''), 'image/webp');
  157.     }
  158. }