Skip to content

Commit

Permalink
[Versions] Detail Endpoint Improvements (#392)
Browse files Browse the repository at this point in the history
* unify hydrator for asset versions

* remove version test due to the core restrictions
  • Loading branch information
lukmzig authored Aug 28, 2024
1 parent dbeaba7 commit 6f8d3d6
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 228 deletions.
2 changes: 0 additions & 2 deletions src/Response/Schemas.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
use Pimcore\Bundle\StudioBackendBundle\Version\Schema\AssetVersion;
use Pimcore\Bundle\StudioBackendBundle\Version\Schema\DataObjectVersion;
use Pimcore\Bundle\StudioBackendBundle\Version\Schema\DocumentVersion;
use Pimcore\Bundle\StudioBackendBundle\Version\Schema\ImageVersion;

/**
* @internal
Expand All @@ -56,7 +55,6 @@

public const VERSIONS = [
AssetVersion::class,
ImageVersion::class,
DataObjectVersion::class,
DocumentVersion::class,
];
Expand Down
39 changes: 0 additions & 39 deletions src/Version/Event/ImageVersionEvent.php

This file was deleted.

41 changes: 13 additions & 28 deletions src/Version/Hydrator/AssetVersionHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@
use Pimcore\Bundle\StudioBackendBundle\Asset\Service\DocumentServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\Asset\MimeTypes;
use Pimcore\Bundle\StudioBackendBundle\Version\Event\AssetVersionEvent;
use Pimcore\Bundle\StudioBackendBundle\Version\Event\ImageVersionEvent;
use Pimcore\Bundle\StudioBackendBundle\Version\Schema\AssetVersion;
use Pimcore\Bundle\StudioBackendBundle\Version\Schema\ImageVersion;
use Pimcore\Bundle\StudioBackendBundle\Version\Service\VersionDetailServiceInterface;
use Pimcore\Model\Asset;
use Pimcore\Model\Asset\Image;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
Expand All @@ -46,7 +43,7 @@ public function __construct(
*/
public function hydrate(
Asset $asset
): ImageVersion|AssetVersion {
): AssetVersion {
if (
$asset instanceof Asset\Document &&
$asset->getMimeType() === MimeTypes::PDF->value &&
Expand All @@ -57,12 +54,20 @@ public function hydrate(
);
}

if ($asset instanceof Image) {
return $this->hydrateImage($asset);
}
return $this->hydrateAsset($asset);
}

private function hydrateAsset(Asset $asset): AssetVersion
{
$hydratedAsset = new AssetVersion(
fileName: $asset->getFilename()
$asset->getType(),
$asset->getFilename(),
$asset->getCreationDate(),
$asset->getModificationDate(),
$this->versionDetailService->getAssetFileSize($asset) ?? $asset->getFileSize(),
$asset->getMimeType(),
$this->customMetadataVersionHydrator->hydrate($asset->getMetadata()),
$this->versionDetailService->getDimensions($asset)
);

$this->eventDispatcher->dispatch(
Expand All @@ -72,24 +77,4 @@ public function hydrate(

return $hydratedAsset;
}

private function hydrateImage(Image $image): ImageVersion
{
$hydratedImage = new ImageVersion(
$image->getFilename(),
$image->getCreationDate(),
$image->getModificationDate(),
$this->versionDetailService->getAssetFileSize($image) ?? $image->getFileSize(),
$image->getMimeType(),
$this->customMetadataVersionHydrator->hydrate($image->getMetadata()),
$this->versionDetailService->getImageDimensions($image)
);

$this->eventDispatcher->dispatch(
new ImageVersionEvent($hydratedImage),
ImageVersionEvent::EVENT_NAME
);

return $hydratedImage;
}
}
3 changes: 1 addition & 2 deletions src/Version/Hydrator/AssetVersionHydratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
namespace Pimcore\Bundle\StudioBackendBundle\Version\Hydrator;

use Pimcore\Bundle\StudioBackendBundle\Version\Schema\AssetVersion;
use Pimcore\Bundle\StudioBackendBundle\Version\Schema\ImageVersion;
use Pimcore\Model\Asset;

/**
Expand All @@ -27,5 +26,5 @@ interface AssetVersionHydratorInterface
{
public function hydrate(
Asset $asset
): ImageVersion|AssetVersion;
): AssetVersion;
}
56 changes: 53 additions & 3 deletions src/Version/Schema/AssetVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,78 @@

namespace Pimcore\Bundle\StudioBackendBundle\Version\Schema;

use OpenApi\Attributes\Items;
use OpenApi\Attributes\Property;
use OpenApi\Attributes\Schema;
use Pimcore\Bundle\StudioBackendBundle\Util\Schema\AdditionalAttributesInterface;
use Pimcore\Bundle\StudioBackendBundle\Util\Trait\AdditionalAttributesTrait;

#[Schema(
title: 'AssetVersion',
required: ['fileName'],
required: ['type', 'fileName', 'creationDate', 'fileSize', 'mimeType', 'metadata', 'dimensions'],
type: 'object'
)]
final class AssetVersion implements AdditionalAttributesInterface
class AssetVersion implements AdditionalAttributesInterface
{
use AdditionalAttributesTrait;

public function __construct(
#[Property(description: 'asset type', type: 'string', example: 'image')]
private readonly string $type,
#[Property(description: 'file name', type: 'string', example: 'myImageFile.png')]
private readonly string $fileName
private readonly string $fileName,
#[Property(description: 'creation date', type: 'integer', example: 1707312457)]
private readonly int $creationDate,
#[Property(description: 'modification date', type: 'integer', example: 1707312457)]
private readonly ?int $modificationDate,
#[Property(description: 'file size', type: 'integer', example: 41862)]
private readonly int $fileSize,
#[Property(description: 'mime type', type: 'string', example: 'image/png')]
private readonly string $mimeType,
#[Property(description: 'Metadata', type: 'array', items: new Items(ref: CustomMetadataVersion::class))]
private readonly array $metadata,
#[Property(description: 'dimensions', type: Dimensions::class, example: '{"width":1920,"height":1080}')]
private readonly Dimensions $dimensions,
) {
}

public function getType(): string
{
return $this->type;
}

public function getFileName(): string
{
return $this->fileName;
}

public function getCreationDate(): int
{
return $this->creationDate;
}

public function getModificationDate(): ?int
{
return $this->modificationDate;
}

public function getFileSize(): int
{
return $this->fileSize;
}

public function getMimeType(): string
{
return $this->mimeType;
}

public function getMetadata(): array
{
return $this->metadata;
}

public function getDimensions(): Dimensions
{
return $this->dimensions;
}
}
87 changes: 0 additions & 87 deletions src/Version/Schema/ImageVersion.php

This file was deleted.

14 changes: 6 additions & 8 deletions src/Version/Service/VersionDetailService.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
use Pimcore\Bundle\StudioBackendBundle\Version\Schema\DataObjectVersion;
use Pimcore\Bundle\StudioBackendBundle\Version\Schema\Dimensions;
use Pimcore\Bundle\StudioBackendBundle\Version\Schema\DocumentVersion;
use Pimcore\Bundle\StudioBackendBundle\Version\Schema\ImageVersion;
use Pimcore\Model\Asset;
use Pimcore\Model\Asset\Image;
use Pimcore\Model\Element\ElementInterface;
use Pimcore\Model\UserInterface;
use Symfony\Component\Finder\Exception\AccessDeniedException;
Expand All @@ -53,7 +51,7 @@ public function __construct(
public function getVersionData(
int $id,
UserInterface $user
): AssetVersion|ImageVersion|DataObjectVersion|DocumentVersion {
): AssetVersion|DataObjectVersion|DocumentVersion {
$version = $this->repository->getVersionById($id);
$element = $this->repository->getElementFromVersion($version, $user);

Expand All @@ -69,9 +67,9 @@ public function getVersionData(
*
* @throws ElementStreamResourceNotFoundException
*/
public function getImageDimensions(Image $image): Dimensions
public function getDimensions(Asset $asset): Dimensions
{
$path = $this->getLocalAssetPath($image);
$path = $this->getLocalAssetPath($asset);

if (is_readable($path)) {
$assetDimensions = getimagesize($path);
Expand All @@ -93,9 +91,9 @@ public function getImageDimensions(Image $image): Dimensions
*
* @throws ElementStreamResourceNotFoundException
*/
public function getAssetFileSize(Asset $image): ?int
public function getAssetFileSize(Asset $asset): ?int
{
$path = $this->getLocalAssetPath($image);
$path = $this->getLocalAssetPath($asset);

if (is_readable($path)) {
return filesize($path);
Expand Down Expand Up @@ -125,7 +123,7 @@ private function getLocalAssetPath(Asset $asset): string
private function hydrate(
ElementInterface $element,
string $class
): AssetVersion|ImageVersion|DocumentVersion|DataObjectVersion {
): AssetVersion|DocumentVersion|DataObjectVersion {
if ($this->versionHydratorLocator->has($class)) {
return $this->versionHydratorLocator->get($class)->hydrate($element);
}
Expand Down
8 changes: 3 additions & 5 deletions src/Version/Service/VersionDetailServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
use Pimcore\Bundle\StudioBackendBundle\Version\Schema\DataObjectVersion;
use Pimcore\Bundle\StudioBackendBundle\Version\Schema\Dimensions;
use Pimcore\Bundle\StudioBackendBundle\Version\Schema\DocumentVersion;
use Pimcore\Bundle\StudioBackendBundle\Version\Schema\ImageVersion;
use Pimcore\Model\Asset;
use Pimcore\Model\Asset\Image;
use Pimcore\Model\UserInterface;
use Symfony\Component\Finder\Exception\AccessDeniedException;

Expand All @@ -40,15 +38,15 @@ interface VersionDetailServiceInterface
public function getVersionData(
int $id,
UserInterface $user
): AssetVersion|ImageVersion|DataObjectVersion|DocumentVersion;
): AssetVersion|DataObjectVersion|DocumentVersion;

/**
* @throws ElementStreamResourceNotFoundException
*/
public function getImageDimensions(Image $image): Dimensions;
public function getDimensions(Asset $asset): Dimensions;

/**
* @throws ElementStreamResourceNotFoundException
*/
public function getAssetFileSize(Asset $image): ?int;
public function getAssetFileSize(Asset $asset): ?int;
}
Loading

0 comments on commit 6f8d3d6

Please sign in to comment.