|
2 | 2 |
|
3 | 3 | namespace Frosh\ThumbnailProcessorImgProxy\Service; |
4 | 4 |
|
| 5 | +use Frosh\ThumbnailProcessor\Service\SalesChannelIdDetector; |
5 | 6 | use Frosh\ThumbnailProcessor\Service\ThumbnailUrlTemplateInterface; |
6 | 7 | use Shopware\Core\System\SystemConfig\SystemConfigService; |
7 | 8 |
|
8 | 9 | class ThumbnailUrlTemplate implements ThumbnailUrlTemplateInterface |
9 | 10 | { |
10 | | - /** @var string */ |
11 | | - private $domain; |
| 11 | + private ?array $config = null; |
12 | 12 |
|
13 | | - /** @var string */ |
14 | | - private $key; |
| 13 | + public function __construct( |
| 14 | + private readonly SystemConfigService $systemConfigService, |
| 15 | + private readonly ThumbnailUrlTemplateInterface $parent, |
| 16 | + private readonly SalesChannelIdDetector $salesChannelIdDetector |
| 17 | + ) { |
| 18 | + } |
15 | 19 |
|
16 | | - /** @var string */ |
17 | | - private $salt; |
| 20 | + public function getUrl(string $mediaUrl, string $mediaPath, string $width): string |
| 21 | + { |
| 22 | + $config = $this->getConfig(); |
18 | 23 |
|
19 | | - /** @var string */ |
20 | | - private $resizingType; |
| 24 | + if (empty($config)) { |
| 25 | + return $this->parent->getUrl($mediaUrl, $mediaPath, $width); |
| 26 | + } |
21 | 27 |
|
22 | | - /** @var string */ |
23 | | - private $gravity; |
| 28 | + $extension = pathinfo($mediaPath, \PATHINFO_EXTENSION); |
| 29 | + $encodedUrl = rtrim(strtr(base64_encode($mediaUrl . '/' . $mediaPath), '+/', '-_'), '='); |
24 | 30 |
|
25 | | - /** @var int */ |
26 | | - private $enlarge; |
| 31 | + $path = "/rs:{$config['resizingType']}:{$width}:0:{$config['enlarge']}/g:{$config['gravity']}/{$encodedUrl}.{$extension}"; |
| 32 | + $signature = hash_hmac('sha256', $config['saltBin'] . $path, $config['keyBin'], true); |
27 | 33 |
|
28 | | - /** @var int */ |
29 | | - private $signatureSize; |
| 34 | + if ($config['signatureSize'] !== 32) { |
| 35 | + $signature = pack('A' . $config['signatureSize'], $signature); |
| 36 | + } |
30 | 37 |
|
31 | | - /** |
32 | | - * @var ThumbnailUrlTemplateInterface |
33 | | - */ |
34 | | - private $parent; |
| 38 | + $signature = rtrim(strtr(base64_encode($signature), '+/', '-_'), '='); |
35 | 39 |
|
36 | | - public function __construct(SystemConfigService $systemConfigService, ThumbnailUrlTemplateInterface $parent) |
37 | | - { |
38 | | - $this->domain = $systemConfigService->get('FroshPlatformThumbnailProcessorImgProxy.config.Domain'); |
39 | | - $this->key = $systemConfigService->get('FroshPlatformThumbnailProcessorImgProxy.config.imgproxykey'); |
40 | | - $this->salt = $systemConfigService->get('FroshPlatformThumbnailProcessorImgProxy.config.imgproxysalt'); |
41 | | - $this->resizingType = $systemConfigService->get('FroshPlatformThumbnailProcessorImgProxy.config.resizingType') ?: 'fit'; |
42 | | - $this->gravity = $systemConfigService->get('FroshPlatformThumbnailProcessorImgProxy.config.gravity') ?: 'sm'; |
43 | | - $this->enlarge = $systemConfigService->get('FroshPlatformThumbnailProcessorImgProxy.config.enlarge') ?: 0; |
44 | | - $this->signatureSize = $systemConfigService->get('FroshPlatformThumbnailProcessorImgProxy.config.signatureSize') ?: 32; |
45 | | - $this->parent = $parent; |
| 40 | + return \rtrim($config['Domain'], '/') . '/' . $signature . $path; |
46 | 41 | } |
47 | 42 |
|
48 | 43 | /** |
49 | | - * @param string $mediaUrl |
50 | | - * @param string $mediaPath |
51 | | - * @param string $width |
52 | | - * @param string $height |
| 44 | + * @return array{Domain: string, imgproxykey: string, imgproxysalt: string, keyBin: string, saltBin: string, resizingType: string, gravity: string, enlarge: int, signatureSize: int} |
53 | 45 | */ |
54 | | - public function getUrl($mediaUrl, $mediaPath, $width, $height = ''): string |
| 46 | + private function getConfig(): array |
55 | 47 | { |
56 | | - $keyBin = pack('H*', $this->key); |
57 | | - $saltBin = pack('H*', $this->salt); |
| 48 | + if (!is_array($this->config)) { |
| 49 | + $salesChannelId = $this->salesChannelIdDetector->getSalesChannelId(); |
| 50 | + $config = $this->systemConfigService->get('FroshPlatformThumbnailProcessorImgProxy.config', $salesChannelId); |
58 | 51 |
|
59 | | - if (empty($keyBin) || empty($saltBin)) { |
60 | | - return $this->parent->getUrl($mediaUrl, $mediaPath, $width, $height); |
61 | | - } |
| 52 | + if (!\is_array($config)) { |
| 53 | + return []; |
| 54 | + } |
62 | 55 |
|
63 | | - $extension = pathinfo($mediaPath, PATHINFO_EXTENSION); |
64 | | - $encodedUrl = rtrim(strtr(base64_encode($mediaUrl . '/' . $mediaPath), '+/', '-_'), '='); |
| 56 | + if (empty($config['Domain']) || empty($config['imgproxykey']) || empty($config['imgproxysalt'])) { |
| 57 | + return []; |
| 58 | + } |
65 | 59 |
|
66 | | - $path = "/rs:{$this->resizingType}:{$width}:{$height}/g:{$this->gravity}/{$encodedUrl}.{$extension}"; |
67 | | - $signature = hash_hmac('sha256', $saltBin . $path, $keyBin, true); |
| 60 | + $config['keyBin'] = pack('H*', $config['imgproxykey']); |
| 61 | + $config['saltBin'] = pack('H*', $config['imgproxysalt']); |
68 | 62 |
|
69 | | - if ($this->signatureSize !== 32) { |
70 | | - $signature = pack('A' . $this->signatureSize, $signature); |
71 | | - } |
| 63 | + if (empty($config['resizingType'])) { |
| 64 | + $config['resizingType'] = 'fit'; |
| 65 | + } |
72 | 66 |
|
73 | | - $signature = rtrim(strtr(base64_encode($signature), '+/', '-_'), '='); |
| 67 | + if (empty($config['gravity'])) { |
| 68 | + $config['gravity'] = 'sm'; |
| 69 | + } |
| 70 | + |
| 71 | + if (!isset($config['enlarge'])) { |
| 72 | + $config['enlarge'] = 0; |
| 73 | + } |
| 74 | + |
| 75 | + if (empty($config['signatureSize'])) { |
| 76 | + $config['signatureSize'] = 32; |
| 77 | + } |
| 78 | + |
| 79 | + if (!\is_int($config['signatureSize'])) { |
| 80 | + $config['signatureSize'] = (int) $config['signatureSize']; |
| 81 | + } |
| 82 | + |
| 83 | + $this->config = $config; |
| 84 | + } |
74 | 85 |
|
75 | | - return $this->domain . '/' . $signature . $path; |
| 86 | + return $this->config; |
76 | 87 | } |
77 | 88 | } |
0 commit comments