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

Open in your IDE?
  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. class ImagineController
  25. {
  26. /**
  27. * @var FilterService
  28. */
  29. private $filterService;
  30. /**
  31. * @var DataManager
  32. */
  33. private $dataManager;
  34. /**
  35. * @var SignerInterface
  36. */
  37. private $signer;
  38. /**
  39. * @var ControllerConfig
  40. */
  41. private $controllerConfig;
  42. public function __construct(
  43. FilterService $filterService,
  44. DataManager $dataManager,
  45. SignerInterface $signer,
  46. ?ControllerConfig $controllerConfig = null
  47. ) {
  48. $this->filterService = $filterService;
  49. $this->dataManager = $dataManager;
  50. $this->signer = $signer;
  51. if (null === $controllerConfig) {
  52. @trigger_error(\sprintf(
  53. '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
  54. ), E_USER_DEPRECATED);
  55. }
  56. $this->controllerConfig = $controllerConfig ?? new ControllerConfig(301);
  57. }
  58. /**
  59. * This action applies a given filter to a given image, saves the image and redirects the browser to the stored
  60. * image.
  61. *
  62. * The resulting image is cached so subsequent requests will redirect to the cached image instead applying the
  63. * filter and storing the image again.
  64. *
  65. * @param string $path
  66. * @param string $filter
  67. *
  68. * @throws RuntimeException
  69. * @throws NotFoundHttpException
  70. *
  71. * @return RedirectResponse
  72. */
  73. public function filterAction(Request $request, $path, $filter)
  74. {
  75. $path = PathHelper::urlPathToFilePath($path);
  76. // TODO once we limit `symfony/http-foundation` to 6.4 or newer, use `$request->query->getString()`
  77. $resolver = $request->query->has('resolver') ? (string) $request->query->get('resolver') : null;
  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->query->has('resolver') ? (string) $request->query->get('resolver') : null;
  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, $filter, json_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. try {
  125. return $request->query->all('filters');
  126. } catch (BadRequestException $e) {
  127. // for strict BC - BadRequestException seems more suited to this situation.
  128. // remove the try-catch in version 3
  129. throw new NotFoundHttpException(\sprintf('Filters must be an array. Value was "%s"', $request->query->get('filters')));
  130. }
  131. }
  132. private function createRedirectResponse(\Closure $url, string $path, string $filter, ?string $hash = null): RedirectResponse
  133. {
  134. try {
  135. return new RedirectResponse($url(), $this->controllerConfig->getRedirectResponseCode());
  136. } catch (NotLoadableException $exception) {
  137. if (null !== $this->dataManager->getDefaultImageUrl($filter)) {
  138. return new RedirectResponse($this->dataManager->getDefaultImageUrl($filter));
  139. }
  140. throw new NotFoundHttpException(\sprintf('Source image for path "%s" could not be found', $path), $exception);
  141. } catch (NonExistingFilterException $exception) {
  142. throw new NotFoundHttpException(\sprintf('Requested non-existing filter "%s"', $filter), $exception);
  143. } catch (RuntimeException $exception) {
  144. 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);
  145. }
  146. }
  147. private function isWebpSupported(Request $request): bool
  148. {
  149. return false !== mb_stripos($request->headers->get('accept', ''), 'image/webp');
  150. }
  151. }