Skip to content

Commit

Permalink
feature: allow passing media timestamp to image service (#132)
Browse files Browse the repository at this point in the history
* feature: allow passing media timestamp to image service

* feat: change string "null" to "0"

* chore: release 5.1.0

---------

Co-authored-by: tinect <[email protected]>
  • Loading branch information
xyng and tinect authored Jun 6, 2024
1 parent 9ce705d commit 39944f1
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 29 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ jobs:
mysql -V
php -v
composer -V
echo ${{ github.workspace }}
- name: Checkout
uses: actions/checkout@v3
Expand Down Expand Up @@ -73,14 +72,14 @@ jobs:
with:
shopware-version: ${{ matrix.version }}
php-version: ${{ matrix.php-version }}
php-extensions: pcov

- name: Info
run: |
php bin/console -V
mysql -V
php -v
composer -V
echo ${{ github.workspace }}
- name: Checkout
uses: actions/checkout@v3
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG_en-GB.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 5.1.0
* Add variable mediaUpdatedAt for paths to prevent caching

# 5.0.1
* Fix support for cloning product entity

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
3 changes: 2 additions & 1 deletion src/Core/Media/MediaUrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<b>{mediaUrl}</b>: e.g. https://cdn.test.de/<br>
<b>{mediaPath}</b>: e.g. media/image/5b/6d/16/tea.png<br>
<b>{width}</b>: e.g. 800
<b>{mediaUpdatedAt}</b>: 1716882050 (unix timestamp) or 0
</p>

<p>
Expand Down
3 changes: 2 additions & 1 deletion src/Resources/config/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
<helpText><![CDATA[available variables:<br>
{mediaUrl}: https://cdn.test.de/<br>
{mediaPath}: media/image/5b/6d/16/tea.png<br>
{width}: 800
{width}: 800<br>
{mediaUpdatedAt}: 1716882050 (unix timestamp) or 0
]]></helpText>
</input-field>

Expand Down
6 changes: 3 additions & 3 deletions src/Service/ThumbnailUrlTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Service/ThumbnailUrlTemplateInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
41 changes: 21 additions & 20 deletions tests/unit/Service/ThumbnailUrlTemplateTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare(strict_types=1);

namespace Frosh\ThumbnailProcessor\TestsUnit\Service;
namespace Frosh\ThumbnailProcessor\Tests\Unit\Service;

use Frosh\ThumbnailProcessor\Service\ConfigReader;
use Frosh\ThumbnailProcessor\Service\ThumbnailUrlTemplate;
Expand All @@ -9,65 +9,66 @@

class ThumbnailUrlTemplateTest extends TestCase
{
/**
/**
* @dataProvider getSalesChannelIds
*/
public function testGetUrl(?string $salesChannelId, string $mediaUrl, string $mediaPath, string $width): void
public function testGetUrl(?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}&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);
}

/**
* @return iterable<array{string|null, string, string, string}>
*/
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];
}
}

0 comments on commit 39944f1

Please sign in to comment.