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

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. 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. // TODO once we limit `symfony/http-foundation` to 6.4 or newer, use `$request->query->getString()`
  78. $resolver = $request->query->has('resolver') ? (string) $request->query->get('resolver') : null;
  79. return $this->createRedirectResponse(function () use ($path, $filter, $resolver, $request) {
  80. return $this->filterService->getUrlOfFilteredImage(
  81. $path,
  82. $filter,
  83. $resolver,
  84. $this->isWebpSupported($request)
  85. );
  86. }, $path, $filter);
  87. }
  88. /**
  89. * This action applies a given filter -merged with additional runtime filters- to a given image, saves the image and
  90. * redirects the browser to the stored image.
  91. *
  92. * The resulting image is cached so subsequent requests will redirect to the cached image instead applying the
  93. * filter and storing the image again.
  94. *
  95. * @param string $hash
  96. * @param string $path
  97. * @param string $filter
  98. *
  99. * @throws RuntimeException
  100. * @throws BadRequestHttpException
  101. * @throws NotFoundHttpException
  102. *
  103. * @return RedirectResponse
  104. */
  105. public function filterRuntimeAction(Request $request, $hash, $path, $filter)
  106. {
  107. $resolver = $request->query->has('resolver') ? (string) $request->query->get('resolver') : null;
  108. $path = PathHelper::urlPathToFilePath($path);
  109. $runtimeConfig = $this->getFiltersBc($request);
  110. if (true !== $this->signer->check($hash, $path, $runtimeConfig)) {
  111. 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)));
  112. }
  113. return $this->createRedirectResponse(function () use ($path, $filter, $runtimeConfig, $resolver, $request) {
  114. return $this->filterService->getUrlOfFilteredImageWithRuntimeFilters(
  115. $path,
  116. $filter,
  117. $runtimeConfig,
  118. $resolver,
  119. $this->isWebpSupported($request)
  120. );
  121. }, $path, $filter, $hash);
  122. }
  123. private function getFiltersBc(Request $request): array
  124. {
  125. if (version_compare(Kernel::VERSION, '5.1', '>=')) {
  126. try {
  127. return $request->query->all('filters');
  128. } catch (BadRequestException $e) {
  129. // for strict BC - BadRequestException seems more suited to this situation.
  130. // remove the try-catch in version 3
  131. throw new NotFoundHttpException(\sprintf('Filters must be an array. Value was "%s"', $request->query->get('filters')));
  132. }
  133. }
  134. $runtimeConfig = $request->query->get('filters', []);
  135. if (!\is_array($runtimeConfig)) {
  136. throw new NotFoundHttpException(\sprintf('Filters must be an array. Value was "%s"', $runtimeConfig));
  137. }
  138. return $runtimeConfig;
  139. }
  140. private function createRedirectResponse(\Closure $url, string $path, string $filter, ?string $hash = null): RedirectResponse
  141. {
  142. try {
  143. return new RedirectResponse($url(), $this->controllerConfig->getRedirectResponseCode());
  144. } catch (NotLoadableException $exception) {
  145. if (null !== $this->dataManager->getDefaultImageUrl($filter)) {
  146. return new RedirectResponse($this->dataManager->getDefaultImageUrl($filter));
  147. }
  148. throw new NotFoundHttpException(\sprintf('Source image for path "%s" could not be found', $path), $exception);
  149. } catch (NonExistingFilterException $exception) {
  150. throw new NotFoundHttpException(\sprintf('Requested non-existing filter "%s"', $filter), $exception);
  151. } catch (RuntimeException $exception) {
  152. 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);
  153. }
  154. }
  155. private function isWebpSupported(Request $request): bool
  156. {
  157. return false !== mb_stripos($request->headers->get('accept', ''), 'image/webp');
  158. }
  159. }