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