From 39944f1fb18a30fbf5d514c4c664b2a7f64b51cf Mon Sep 17 00:00:00 2001 From: Daniel Melchior Date: Thu, 6 Jun 2024 21:21:40 +0200 Subject: [PATCH] feature: allow passing media timestamp to image service (#132) * feature: allow passing media timestamp to image service * feat: change string "null" to "0" * chore: release 5.1.0 --------- Co-authored-by: tinect --- .github/workflows/test.yml | 3 +- CHANGELOG_en-GB.md | 3 ++ README.md | 1 + composer.json | 2 +- src/Core/Media/MediaUrlGenerator.php | 3 +- ...h-thumbnail-processor-info-texts.html.twig | 1 + src/Resources/config/config.xml | 3 +- src/Service/ThumbnailUrlTemplate.php | 6 +-- src/Service/ThumbnailUrlTemplateInterface.php | 2 +- .../unit/Service/ThumbnailUrlTemplateTest.php | 41 ++++++++++--------- 10 files changed, 36 insertions(+), 29 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index fbd6d46..bc12ed7 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -32,7 +32,6 @@ jobs: mysql -V php -v composer -V - echo ${{ github.workspace }} - name: Checkout uses: actions/checkout@v3 @@ -73,6 +72,7 @@ jobs: with: shopware-version: ${{ matrix.version }} php-version: ${{ matrix.php-version }} + php-extensions: pcov - name: Info run: | @@ -80,7 +80,6 @@ jobs: mysql -V php -v composer -V - echo ${{ github.workspace }} - name: Checkout uses: actions/checkout@v3 diff --git a/CHANGELOG_en-GB.md b/CHANGELOG_en-GB.md index 2739baa..b9b79cd 100644 --- a/CHANGELOG_en-GB.md +++ b/CHANGELOG_en-GB.md @@ -1,3 +1,6 @@ +# 5.1.0 +* Add variable mediaUpdatedAt for paths to prevent caching + # 5.0.1 * Fix support for cloning product entity diff --git a/README.md b/README.md index 333ff4b..9e2374e 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,7 @@ Available variables with examples: * {mediaUrl}: https://www.example.com/ * {mediaPath}: media/01/82/69/sasse.png * {width}: 800 +* {mediaUpdatedAt}: 1716882050 (unix timestamp) or 0 Feel free to decorate `ThumbnailUrlTemplateInterface` to add more individual functions like [signed imgproxy](https://github.com/FriendsOfShopware/FroshPlatformThumbnailProcessorImgProxy) diff --git a/composer.json b/composer.json index 042e3da..67eb1f0 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ "thumbnail" ], "description": "This plugins allows you to use variable thumbnails, without having them on storage.", - "version": "5.0.1", + "version": "5.1.0", "type": "shopware-platform-plugin", "license": "mit", "authors": [ diff --git a/src/Core/Media/MediaUrlGenerator.php b/src/Core/Media/MediaUrlGenerator.php index 30b4ea0..21a493c 100644 --- a/src/Core/Media/MediaUrlGenerator.php +++ b/src/Core/Media/MediaUrlGenerator.php @@ -51,7 +51,8 @@ public function generate(array $paths): array $urls[$key] = $this->thumbnailUrlTemplate->getUrl( $baseUrl, $value->path, - $this->getWidth($maxWidth, $value) + $this->getWidth($maxWidth, $value), + $value->updatedAt ); } diff --git a/src/Resources/app/administration/src/module/frosh-thumbnail-processor/frosh-thumbnail-processor-info-texts/frosh-thumbnail-processor-info-texts.html.twig b/src/Resources/app/administration/src/module/frosh-thumbnail-processor/frosh-thumbnail-processor-info-texts/frosh-thumbnail-processor-info-texts.html.twig index 3c67efe..976b4e0 100644 --- a/src/Resources/app/administration/src/module/frosh-thumbnail-processor/frosh-thumbnail-processor-info-texts/frosh-thumbnail-processor-info-texts.html.twig +++ b/src/Resources/app/administration/src/module/frosh-thumbnail-processor/frosh-thumbnail-processor-info-texts/frosh-thumbnail-processor-info-texts.html.twig @@ -9,6 +9,7 @@ {mediaUrl}: e.g. https://cdn.test.de/
{mediaPath}: e.g. media/image/5b/6d/16/tea.png
{width}: e.g. 800 + {mediaUpdatedAt}: 1716882050 (unix timestamp) or 0

diff --git a/src/Resources/config/config.xml b/src/Resources/config/config.xml index 07879eb..2105463 100644 --- a/src/Resources/config/config.xml +++ b/src/Resources/config/config.xml @@ -20,7 +20,8 @@ {mediaUrl}: https://cdn.test.de/
{mediaPath}: media/image/5b/6d/16/tea.png
- {width}: 800 + {width}: 800
+ {mediaUpdatedAt}: 1716882050 (unix timestamp) or 0 ]]>
diff --git a/src/Service/ThumbnailUrlTemplate.php b/src/Service/ThumbnailUrlTemplate.php index 6530bfc..3329f0c 100644 --- a/src/Service/ThumbnailUrlTemplate.php +++ b/src/Service/ThumbnailUrlTemplate.php @@ -11,11 +11,11 @@ public function __construct( ) { } - public function getUrl(string $mediaUrl, string $mediaPath, string $width): string + public function getUrl(string $mediaUrl, string $mediaPath, string $width, ?\DateTimeInterface $mediaUpdatedAt): string { return str_replace( - ['{mediaUrl}', '{mediaPath}', '{width}'], - [$mediaUrl, $mediaPath, $width], + ['{mediaUrl}', '{mediaPath}', '{width}', '{mediaUpdatedAt}'], + [$mediaUrl, $mediaPath, $width, $mediaUpdatedAt?->getTimestamp() ?: '0'], $this->getPattern() ); } diff --git a/src/Service/ThumbnailUrlTemplateInterface.php b/src/Service/ThumbnailUrlTemplateInterface.php index 25d4f54..6eba88c 100644 --- a/src/Service/ThumbnailUrlTemplateInterface.php +++ b/src/Service/ThumbnailUrlTemplateInterface.php @@ -4,5 +4,5 @@ interface ThumbnailUrlTemplateInterface { - public function getUrl(string $mediaUrl, string $mediaPath, string $width): string; + public function getUrl(string $mediaUrl, string $mediaPath, string $width, ?\DateTimeInterface $mediaUpdatedAt): string; } diff --git a/tests/unit/Service/ThumbnailUrlTemplateTest.php b/tests/unit/Service/ThumbnailUrlTemplateTest.php index 3f9a528..c1cb29d 100644 --- a/tests/unit/Service/ThumbnailUrlTemplateTest.php +++ b/tests/unit/Service/ThumbnailUrlTemplateTest.php @@ -1,6 +1,6 @@ createMock(ConfigReader::class); $configReader->expects(static::once()) - ->method('getConfig')->willReturn('{mediaUrl}/{mediaPath}?width={width}&uff'); + ->method('getConfig')->willReturn('{mediaUrl}/{mediaPath}?width={width}&updatedAt={mediaUpdatedAt}&uff'); $class = new ThumbnailUrlTemplate($configReader); - $url = $class->getUrl($mediaUrl, $mediaPath, $width); + $url = $class->getUrl($mediaUrl, $mediaPath, $width, $date); - static::assertSame(\sprintf('%s/%s?width=%s&uff', $mediaUrl, $mediaPath, $width), $url); + static::assertSame(\sprintf('%s/%s?width=%s&updatedAt=%s&uff', $mediaUrl, $mediaPath, $width, $date?->getTimestamp() ?: '0'), $url); } /** * @dataProvider getSalesChannelIds */ - public function testGetUrlWithoutSetConfig(?string $salesChannelId, string $mediaUrl, string $mediaPath, string $width): void + public function testGetUrlWithoutSetConfig(?string $salesChannelId, string $mediaUrl, string $mediaPath, string $width, ?\DateTimeInterface $date): void { $configReader = $this->createMock(ConfigReader::class); $configReader->expects(static::once()) - ->method('getConfig')->willReturn('{mediaUrl}/{mediaPath}?width={width}'); + ->method('getConfig')->willReturn('{mediaUrl}/{mediaPath}?width={width}&updatedAt={mediaUpdatedAt}'); $class = new ThumbnailUrlTemplate($configReader); - $url = $class->getUrl($mediaUrl, $mediaPath, $width); + $url = $class->getUrl($mediaUrl, $mediaPath, $width, $date); - static::assertSame(\sprintf('%s/%s?width=%s', $mediaUrl, $mediaPath, $width), $url); + static::assertSame(\sprintf('%s/%s?width=%s&updatedAt=%s', $mediaUrl, $mediaPath, $width, $date?->getTimestamp() ?: '0'), $url); } /** * @dataProvider getSalesChannelIds */ - public function testGetUrlGetPatternOnce(?string $salesChannelId, string $mediaUrl, string $mediaPath, string $width): void + public function testGetUrlGetPatternOnce(?string $salesChannelId, string $mediaUrl, string $mediaPath, string $width, ?\DateTimeInterface $date): void { $configReader = $this->createMock(ConfigReader::class); $configReader->expects(static::once()) - ->method('getConfig')->willReturn('{mediaUrl}/{mediaPath}?width={width}'); + ->method('getConfig')->willReturn('{mediaUrl}/{mediaPath}?width={width}&updatedAt={mediaUpdatedAt}'); $class = new ThumbnailUrlTemplate($configReader); - $class->getUrl($mediaUrl, $mediaPath, $width); + $class->getUrl($mediaUrl, $mediaPath, $width, $date); - $url = $class->getUrl($mediaUrl, $mediaPath, $width); + $url = $class->getUrl($mediaUrl, $mediaPath, $width, $date); - static::assertSame(\sprintf('%s/%s?width=%s', $mediaUrl, $mediaPath, $width), $url); + static::assertSame(\sprintf('%s/%s?width=%s&updatedAt=%s', $mediaUrl, $mediaPath, $width, $date?->getTimestamp() ?: '0'), $url); } /** @@ -64,10 +64,11 @@ public function testGetUrlGetPatternOnce(?string $salesChannelId, string $mediaU */ public static function getSalesChannelIds(): iterable { - yield [null, 'https://www.anywebpage.test', 'media/78/a1/myimage.jpg', '200']; - yield [Uuid::randomHex(), 'https://www.anyotherwebpage.test', 'media/aa/a1/myimage.jpg', '300']; - yield [Uuid::randomHex(), 'https://www.anyother2webpage.test', 'media/aa/bb/myimage.jpg', '700']; - yield [Uuid::randomHex(), 'https://www.anyother3webpage.test', 'media/aa/cc/myimage.jpg', '900']; - yield [Uuid::randomHex(), 'https://www.anyother4webpage.test', 'media/aa/dd/myimage.jpg', '1000']; + yield [null, 'https://www.anywebpage.test', 'media/78/a1/myimage.jpg', '200', null]; + yield [null, 'https://www.anyotherwebpage.test', 'media/78/a1/myimage.jpg', '200', new \DateTimeImmutable()]; + yield [Uuid::randomHex(), 'https://www.anyother2webpage.test', 'media/aa/a1/myimage.jpg', '300', new \DateTimeImmutable()]; + yield [Uuid::randomHex(), 'https://www.anyother3webpage.test', 'media/aa/bb/myimage.jpg', '700', null]; + yield [Uuid::randomHex(), 'https://www.anyother4webpage.test', 'media/aa/cc/myimage.jpg', '900', new \DateTimeImmutable()]; + yield [Uuid::randomHex(), 'https://www.anyother5webpage.test', 'media/aa/dd/myimage.jpg', '1000', null]; } }