Skip to content

Commit

Permalink
Add extension placeholder for serialized medias (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
Prokyonn committed Feb 21, 2023
1 parent e8a1004 commit 9ec2faa
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 29 deletions.
22 changes: 14 additions & 8 deletions Content/Serializer/MediaSerializer.php
Expand Up @@ -94,15 +94,21 @@ public function serialize(MediaInterface $media, string $locale, ?SerializationC
$preferredExtension = $this->imageConverter->getSupportedOutputImageFormats($formatMediaApi->getMimeType())[0] ?? null;
if ($preferredExtension) {
$fileName = \pathinfo($fileName)['filename'] . '.' . $preferredExtension;
}

$mediaData['formatUri'] = $this->formatCache->getMediaUrl(
$formatMediaApi->getId(),
$fileName,
'{format}',
$formatMediaApi->getVersion(),
$formatMediaApi->getSubVersion()
);
// extension brackets cannot be added here because of the urlencoding
$fileName = \pathinfo($fileName)['filename'] . '.' . 'extension';
$formatUri = $this->formatCache->getMediaUrl(
$formatMediaApi->getId(),
$fileName,
'{format}',
$formatMediaApi->getVersion(),
$formatMediaApi->getSubVersion()
);
$formatUri = \str_replace('.extension', '.{extension}', $formatUri);

$mediaData['formatPreferredExtension'] = $preferredExtension;
$mediaData['formatUri'] = $formatUri;
}

$this->referenceStore->add($apiMedia->getId());

Expand Down
Expand Up @@ -97,7 +97,8 @@
"properties": [],
"categories": [],
"targetGroups": [],
"formatUri": "@string@"
"formatUri": "@string@.matchRegex('/^\\/uploads\\/media\\/{format}\\/\\d{2}\\/\\d+-test-image\\.{extension}\\?v=1-0$/')",
"formatPreferredExtension": "png"
}
],
"images": []
Expand Down
76 changes: 57 additions & 19 deletions Tests/Unit/Content/Serializer/MediaSerializerTest.php
Expand Up @@ -24,10 +24,12 @@
use Sulu\Bundle\MediaBundle\Entity\FileVersion;
use Sulu\Bundle\MediaBundle\Entity\MediaInterface;
use Sulu\Bundle\MediaBundle\Media\FormatCache\FormatCacheInterface;
use Sulu\Bundle\MediaBundle\Media\FormatCache\LocalFormatCache;
use Sulu\Bundle\MediaBundle\Media\ImageConverter\ImageConverterInterface;
use Sulu\Bundle\MediaBundle\Media\Manager\MediaManagerInterface;
use Sulu\Bundle\WebsiteBundle\ReferenceStore\ReferenceStoreInterface;
use Sulu\Component\Serializer\ArraySerializerInterface;
use Symfony\Component\Filesystem\Filesystem;

class MediaSerializerTest extends TestCase
{
Expand All @@ -47,7 +49,7 @@ class MediaSerializerTest extends TestCase
private $imageConverter;

/**
* @var FormatCacheInterface|ObjectProphecy
* @var FormatCacheInterface
*/
private $formatCache;

Expand All @@ -66,14 +68,14 @@ protected function setUp(): void
$this->mediaManager = $this->prophesize(MediaManagerInterface::class);
$this->arraySerializer = $this->prophesize(ArraySerializerInterface::class);
$this->imageConverter = $this->prophesize(ImageConverterInterface::class);
$this->formatCache = $this->prophesize(FormatCacheInterface::class);
$this->referenceStore = $this->prophesize(ReferenceStoreInterface::class);
$this->formatCache = new LocalFormatCache(new Filesystem(), __DIR__ . '../../../Application/var/public/uploads/media', '/uploads/media/{slug}', 10);

$this->mediaSerializer = new MediaSerializer(
$this->mediaManager->reveal(),
$this->arraySerializer->reveal(),
$this->imageConverter->reveal(),
$this->formatCache->reveal(),
$this->formatCache,
$this->referenceStore->reveal()
);
}
Expand Down Expand Up @@ -111,11 +113,54 @@ public function testSerialize(): void
])->shouldBeCalled();

$this->imageConverter->getSupportedOutputImageFormats('image/png')
->willReturn(['jpg'])
->willReturn(['png'])
->shouldBeCalled();

$this->formatCache->getMediaUrl(1, 'media-1.jpg', '{format}', 1, 0)
->willReturn('/media/1/{format}/media-1.jpg?v=1-0')
$this->referenceStore->add(1)
->shouldBeCalled();

// call test function
$result = $this->mediaSerializer->serialize($media->reveal(), $locale);

$this->assertSame([
'id' => 1,
'formatPreferredExtension' => 'png',
'formatUri' => '/uploads/media/{format}/01/1-media-1.{extension}?v=1-0',
], $result);
}

public function testSerializeDocument(): void
{
$locale = 'en';
$media = $this->prophesize(MediaInterface::class);

// expected and unexpected object calls
$apiMedia = $this->prophesize(Media::class);
$apiMedia->getId()->willReturn(1)->shouldBeCalled();
$apiMedia->getName()->willReturn('media-1.pdf')->shouldBeCalled();
$apiMedia->getMimeType()->willReturn('application/pdf')->shouldBeCalled();

$apiMediaArgument = Argument::that(function (Media $apiMedia) use ($media, $locale) {
return $apiMedia->getEntity() === $media->reveal() && $locale === $apiMedia->getLocale();
});

// expected and unexpected service calls
$this->mediaManager->addFormatsAndUrl($apiMediaArgument)
->willReturn($apiMedia->reveal())
->shouldBeCalled();

$this->arraySerializer->serialize($apiMedia->reveal(), null)->willReturn([
'id' => 1,
'formats' => [],
'storageOptions' => [],
'thumbnails' => [],
'versions' => [],
'downloadCounter' => [],
'_hash' => [],
])->shouldBeCalled();

$this->imageConverter->getSupportedOutputImageFormats('application/pdf')
->willReturn([])
->shouldBeCalled();

$this->referenceStore->add(1)
Expand All @@ -126,7 +171,6 @@ public function testSerialize(): void

$this->assertSame([
'id' => 1,
'formatUri' => '/media/1/{format}/media-1.jpg?v=1-0',
], $result);
}

Expand Down Expand Up @@ -175,11 +219,7 @@ public function testSerializeWithPreviewImage(): void
])->shouldBeCalled();

$this->imageConverter->getSupportedOutputImageFormats('image/png')
->willReturn([])
->shouldBeCalled();

$this->formatCache->getMediaUrl(1, 'preview-media.png', '{format}', 1, 0)
->willReturn('/media/1/{format}/preview-media.png?v=1-0')
->willReturn(['png'])
->shouldBeCalled();

$this->referenceStore->add(1)
Expand All @@ -190,7 +230,8 @@ public function testSerializeWithPreviewImage(): void

$this->assertSame([
'id' => 1,
'formatUri' => '/media/1/{format}/preview-media.png?v=1-0',
'formatPreferredExtension' => 'png',
'formatUri' => '/uploads/media/{format}/01/1-preview-media.{extension}?v=1-0',
], $result);
}

Expand Down Expand Up @@ -229,11 +270,7 @@ public function testSerializeWithContext(): void
])->shouldBeCalled();

$this->imageConverter->getSupportedOutputImageFormats('image/png')
->willReturn([])
->shouldBeCalled();

$this->formatCache->getMediaUrl(1, 'media-1.png', '{format}', 1, 0)
->willReturn('/media/1/{format}/media-1.png?v=1-0')
->willReturn(['png'])
->shouldBeCalled();

$this->referenceStore->add(1)
Expand All @@ -244,7 +281,8 @@ public function testSerializeWithContext(): void

$this->assertSame([
'id' => 1,
'formatUri' => '/media/1/{format}/media-1.png?v=1-0',
'formatPreferredExtension' => 'png',
'formatUri' => '/uploads/media/{format}/01/1-media-1.{extension}?v=1-0',
], $result);
}
}
24 changes: 24 additions & 0 deletions UPGRADE.md
@@ -1,5 +1,29 @@
# Upgrade

## 0.10.0

### Add extension placeholder to serialized medias

Before this update the preferred extension was always added to the uri.
Now the preferred extension is extracted into a new field `preferredExtension` and in the uri the placeholder `{extension}` is used instead of the extension.

For non-image medias the `formatUri` as well as the `formatPreferredExtension` fields are omitted.

**Before:**
```json
{
"formatUri": "/media/1/{format}/media-1.png?v=1-0"
}
```

**After:**
```json
{
"formatPreferredExtension": "png",
"formatUri": "/media/1/{format}/media-1.{extension}?v=1-0"
}
```

## 0.9.0

### Make NavigationInvalidationSubscriber::collectNavigationContexts method private
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Expand Up @@ -85,6 +85,9 @@
]
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"php-http/discovery": true
}
}
}

0 comments on commit 9ec2faa

Please sign in to comment.