Skip to content
This repository was archived by the owner on Jan 30, 2024. It is now read-only.

Commit 8d1799b

Browse files
committed
feat: add support for ThumbnailProcessor v3 and v4
1 parent f27ebba commit 8d1799b

File tree

5 files changed

+67
-55
lines changed

5 files changed

+67
-55
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
44

5-
This plugins allows you to use your self-hosted secured ImgProxy. You will need Plugin `Thumbnail Processor`.
5+
This plugin allows you to use your self-hosted secured imgproxy. You will need Plugin `Thumbnail Processor`.
66

77
## Install
88

99
Download the plugin from the release page and enable it in shopware.
1010

1111
## Usage
12-
Adjust the config. [See docs of ImgProxy.](https://docs.imgproxy.net/#/configuration?id=url-signature)
12+
Adjust the config. [See docs of imgproxy.](https://docs.imgproxy.net/signing_the_url)
1313

1414
## License
1515

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
"storage",
1111
"thumbnail"
1212
],
13-
"description": "This plugins allows you to use secured imgproxy in thumbnail processor",
14-
"version": "1.0.0",
13+
"description": "This plugin allows you to use secured imgproxy in thumbnail processor",
14+
"version": "2.0.0",
1515
"type": "shopware-platform-plugin",
1616
"license": "MIT",
1717
"authors": [
@@ -21,7 +21,7 @@
2121
}
2222
],
2323
"require": {
24-
"frosh/platform-thumbnail-processor": "^1.0.20",
24+
"frosh/platform-thumbnail-processor": "^3.0 || ^4.0",
2525
"shopware/core": "~6.4.0"
2626
},
2727
"extra": {
@@ -31,8 +31,8 @@
3131
"en-GB": "ThumbnailProcessorImgProxy Addon"
3232
},
3333
"description": {
34-
"de-DE": "This plugins allows you to use secured imgproxy in thumbnail processor",
35-
"en-GB": "This plugins allows you to use secured imgproxy in thumbnail processor"
34+
"de-DE": "Mit diesem Plugin können Sie Ihren selbst gehosteten, gesicherten imgproxy verwenden.",
35+
"en-GB": "This plugin allows you to use your self-hosted secured imgproxy."
3636
}
3737
},
3838
"autoload": {

src/Resources/config/config.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/shopware/platform/master/src/Core/System/SystemConfig/Schema/config.xsd">
3+
xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/shopware/platform/trunk/src/Core/System/SystemConfig/Schema/config.xsd">
44

55
<card>
66
<title>Configuration</title>

src/Resources/config/services.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<service id="Frosh\ThumbnailProcessorImgProxy\Service\ThumbnailUrlTemplate" decorates="Frosh\ThumbnailProcessor\Service\ThumbnailUrlTemplateInterface">
88
<argument type="service" id="Shopware\Core\System\SystemConfig\SystemConfigService"/>
99
<argument type="service" id="Frosh\ThumbnailProcessorImgProxy\Service\ThumbnailUrlTemplate.inner"/>
10+
<argument type="service" id="Frosh\ThumbnailProcessor\Service\SalesChannelIdDetector"/>
1011
</service>
1112
</services>
1213
</container>

src/Service/ThumbnailUrlTemplate.php

Lines changed: 58 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,76 +2,87 @@
22

33
namespace Frosh\ThumbnailProcessorImgProxy\Service;
44

5+
use Frosh\ThumbnailProcessor\Service\SalesChannelIdDetector;
56
use Frosh\ThumbnailProcessor\Service\ThumbnailUrlTemplateInterface;
67
use Shopware\Core\System\SystemConfig\SystemConfigService;
78

89
class ThumbnailUrlTemplate implements ThumbnailUrlTemplateInterface
910
{
10-
/** @var string */
11-
private $domain;
11+
private ?array $config = null;
1212

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+
}
1519

16-
/** @var string */
17-
private $salt;
20+
public function getUrl(string $mediaUrl, string $mediaPath, string $width): string
21+
{
22+
$config = $this->getConfig();
1823

19-
/** @var string */
20-
private $resizingType;
24+
if (empty($config)) {
25+
return $this->parent->getUrl($mediaUrl, $mediaPath, $width);
26+
}
2127

22-
/** @var string */
23-
private $gravity;
28+
$extension = pathinfo($mediaPath, \PATHINFO_EXTENSION);
29+
$encodedUrl = rtrim(strtr(base64_encode($mediaUrl . '/' . $mediaPath), '+/', '-_'), '=');
2430

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);
2733

28-
/** @var int */
29-
private $signatureSize;
34+
if ($config['signatureSize'] !== 32) {
35+
$signature = pack('A' . $config['signatureSize'], $signature);
36+
}
3037

31-
/**
32-
* @var ThumbnailUrlTemplateInterface
33-
*/
34-
private $parent;
38+
$signature = rtrim(strtr(base64_encode($signature), '+/', '-_'), '=');
3539

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;
4641
}
4742

4843
/**
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}
5345
*/
54-
public function getUrl($mediaUrl, $mediaPath, $width, $height = ''): string
46+
private function getConfig(): array
5547
{
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);
5851

59-
if (empty($keyBin) || empty($saltBin)) {
60-
return $this->parent->getUrl($mediaUrl, $mediaPath, $width, $height);
61-
}
52+
if (!\is_array($config)) {
53+
return [];
54+
}
6255

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+
}
6559

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']);
6862

69-
if ($this->signatureSize !== 32) {
70-
$signature = pack('A' . $this->signatureSize, $signature);
71-
}
63+
if (empty($config['resizingType'])) {
64+
$config['resizingType'] = 'fit';
65+
}
7266

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+
}
7485

75-
return $this->domain . '/' . $signature . $path;
86+
return $this->config;
7687
}
7788
}

0 commit comments

Comments
 (0)