Skip to content
This repository was archived by the owner on Jan 30, 2024. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

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

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

## Install

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

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

## License

Expand Down
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"storage",
"thumbnail"
],
"description": "This plugins allows you to use secured imgproxy in thumbnail processor",
"version": "1.0.0",
"description": "This plugin allows you to use secured imgproxy in thumbnail processor",
"version": "2.0.0",
"type": "shopware-platform-plugin",
"license": "MIT",
"authors": [
Expand All @@ -21,8 +21,8 @@
}
],
"require": {
"frosh/platform-thumbnail-processor": "^1.0.20",
"shopware/core": "~6.4.0"
"frosh/platform-thumbnail-processor": "^3.0 || ^4.0",
"shopware/core": "~6.5.0"
},
"extra": {
"shopware-plugin-class": "Frosh\\ThumbnailProcessorImgProxy\\FroshPlatformThumbnailProcessorImgProxy",
Expand All @@ -31,8 +31,8 @@
"en-GB": "ThumbnailProcessorImgProxy Addon"
},
"description": {
"de-DE": "This plugins allows you to use secured imgproxy in thumbnail processor",
"en-GB": "This plugins allows you to use secured imgproxy in thumbnail processor"
"de-DE": "Mit diesem Plugin können Sie Ihren selbst gehosteten, gesicherten imgproxy verwenden.",
"en-GB": "This plugin allows you to use your self-hosted secured imgproxy."
}
},
"autoload": {
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/config.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/shopware/platform/master/src/Core/System/SystemConfig/Schema/config.xsd">
xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/shopware/platform/trunk/src/Core/System/SystemConfig/Schema/config.xsd">

<card>
<title>Configuration</title>
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<service id="Frosh\ThumbnailProcessorImgProxy\Service\ThumbnailUrlTemplate" decorates="Frosh\ThumbnailProcessor\Service\ThumbnailUrlTemplateInterface">
<argument type="service" id="Shopware\Core\System\SystemConfig\SystemConfigService"/>
<argument type="service" id="Frosh\ThumbnailProcessorImgProxy\Service\ThumbnailUrlTemplate.inner"/>
<argument type="service" id="Frosh\ThumbnailProcessor\Service\SalesChannelIdDetector"/>
</service>
</services>
</container>
105 changes: 58 additions & 47 deletions src/Service/ThumbnailUrlTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,76 +2,87 @@

namespace Frosh\ThumbnailProcessorImgProxy\Service;

use Frosh\ThumbnailProcessor\Service\SalesChannelIdDetector;
use Frosh\ThumbnailProcessor\Service\ThumbnailUrlTemplateInterface;
use Shopware\Core\System\SystemConfig\SystemConfigService;

class ThumbnailUrlTemplate implements ThumbnailUrlTemplateInterface
{
/** @var string */
private $domain;
private ?array $config = null;

/** @var string */
private $key;
public function __construct(
private readonly SystemConfigService $systemConfigService,
private readonly ThumbnailUrlTemplateInterface $parent,
private readonly SalesChannelIdDetector $salesChannelIdDetector
) {
}

/** @var string */
private $salt;
public function getUrl(string $mediaUrl, string $mediaPath, string $width): string
{
$config = $this->getConfig();

/** @var string */
private $resizingType;
if (empty($config)) {
return $this->parent->getUrl($mediaUrl, $mediaPath, $width);
}

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

/** @var int */
private $enlarge;
$path = "/rs:{$config['resizingType']}:{$width}:0:{$config['enlarge']}/g:{$config['gravity']}/{$encodedUrl}.{$extension}";
$signature = hash_hmac('sha256', $config['saltBin'] . $path, $config['keyBin'], true);

/** @var int */
private $signatureSize;
if ($config['signatureSize'] !== 32) {
$signature = pack('A' . $config['signatureSize'], $signature);
}

/**
* @var ThumbnailUrlTemplateInterface
*/
private $parent;
$signature = rtrim(strtr(base64_encode($signature), '+/', '-_'), '=');

public function __construct(SystemConfigService $systemConfigService, ThumbnailUrlTemplateInterface $parent)
{
$this->domain = $systemConfigService->get('FroshPlatformThumbnailProcessorImgProxy.config.Domain');
$this->key = $systemConfigService->get('FroshPlatformThumbnailProcessorImgProxy.config.imgproxykey');
$this->salt = $systemConfigService->get('FroshPlatformThumbnailProcessorImgProxy.config.imgproxysalt');
$this->resizingType = $systemConfigService->get('FroshPlatformThumbnailProcessorImgProxy.config.resizingType') ?: 'fit';
$this->gravity = $systemConfigService->get('FroshPlatformThumbnailProcessorImgProxy.config.gravity') ?: 'sm';
$this->enlarge = $systemConfigService->get('FroshPlatformThumbnailProcessorImgProxy.config.enlarge') ?: 0;
$this->signatureSize = $systemConfigService->get('FroshPlatformThumbnailProcessorImgProxy.config.signatureSize') ?: 32;
$this->parent = $parent;
return \rtrim($config['Domain'], '/') . '/' . $signature . $path;
}

/**
* @param string $mediaUrl
* @param string $mediaPath
* @param string $width
* @param string $height
* @return array{Domain: string, imgproxykey: string, imgproxysalt: string, keyBin: string, saltBin: string, resizingType: string, gravity: string, enlarge: int, signatureSize: int}
*/
public function getUrl($mediaUrl, $mediaPath, $width, $height = ''): string
private function getConfig(): array
{
$keyBin = pack('H*', $this->key);
$saltBin = pack('H*', $this->salt);
if (!is_array($this->config)) {
$salesChannelId = $this->salesChannelIdDetector->getSalesChannelId();
$config = $this->systemConfigService->get('FroshPlatformThumbnailProcessorImgProxy.config', $salesChannelId);

if (empty($keyBin) || empty($saltBin)) {
return $this->parent->getUrl($mediaUrl, $mediaPath, $width, $height);
}
if (!\is_array($config)) {
return [];
}

$extension = pathinfo($mediaPath, PATHINFO_EXTENSION);
$encodedUrl = rtrim(strtr(base64_encode($mediaUrl . '/' . $mediaPath), '+/', '-_'), '=');
if (empty($config['Domain']) || empty($config['imgproxykey']) || empty($config['imgproxysalt'])) {
return [];
}

$path = "/rs:{$this->resizingType}:{$width}:{$height}/g:{$this->gravity}/{$encodedUrl}.{$extension}";
$signature = hash_hmac('sha256', $saltBin . $path, $keyBin, true);
$config['keyBin'] = pack('H*', $config['imgproxykey']);
$config['saltBin'] = pack('H*', $config['imgproxysalt']);

if ($this->signatureSize !== 32) {
$signature = pack('A' . $this->signatureSize, $signature);
}
if (empty($config['resizingType'])) {
$config['resizingType'] = 'fit';
}

$signature = rtrim(strtr(base64_encode($signature), '+/', '-_'), '=');
if (empty($config['gravity'])) {
$config['gravity'] = 'sm';
}

if (!isset($config['enlarge'])) {
$config['enlarge'] = 0;
}

if (empty($config['signatureSize'])) {
$config['signatureSize'] = 32;
}

if (!\is_int($config['signatureSize'])) {
$config['signatureSize'] = (int) $config['signatureSize'];
}

$this->config = $config;
}

return $this->domain . '/' . $signature . $path;
return $this->config;
}
}