diff --git a/config/assets.yaml b/config/assets.yaml index 28aee31b2..9d22a85be 100644 --- a/config/assets.yaml +++ b/config/assets.yaml @@ -17,4 +17,13 @@ services: # Encoder Pimcore\Bundle\StudioBackendBundle\Asset\Encoder\TextEncoderInterface: - class: Pimcore\Bundle\StudioBackendBundle\Asset\Encoder\TextEncoder \ No newline at end of file + class: Pimcore\Bundle\StudioBackendBundle\Asset\Encoder\TextEncoder + + Pimcore\Bundle\StudioBackendBundle\Asset\Service\AssetServiceInterface: + class: Pimcore\Bundle\StudioBackendBundle\Asset\Service\AssetService + + Pimcore\Bundle\StudioBackendBundle\Asset\Service\CustomSettingsServiceInterface: + class: Pimcore\Bundle\StudioBackendBundle\Asset\Service\CustomSettingsService + + Pimcore\Bundle\StudioBackendBundle\Asset\Service\DataServiceInterface: + class: Pimcore\Bundle\StudioBackendBundle\Asset\Service\DataService \ No newline at end of file diff --git a/src/Asset/Controller/CollectionController.php b/src/Asset/Controller/CollectionController.php index adc6a7a46..2437cd6a4 100644 --- a/src/Asset/Controller/CollectionController.php +++ b/src/Asset/Controller/CollectionController.php @@ -18,6 +18,7 @@ use OpenApi\Attributes\Get; use Pimcore\Bundle\StudioBackendBundle\Asset\Attributes\Response\Property\AnyOfAsset; +use Pimcore\Bundle\StudioBackendBundle\Asset\Service\AssetServiceInterface; use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController; use Pimcore\Bundle\StudioBackendBundle\DataIndex\AssetSearchServiceInterface; use Pimcore\Bundle\StudioBackendBundle\DataIndex\OpenSearchFilterInterface; @@ -53,8 +54,7 @@ final class CollectionController extends AbstractApiController public function __construct( SerializerInterface $serializer, - private readonly AssetSearchServiceInterface $assetSearchService, - private readonly FilterServiceProviderInterface $filterServiceProvider + private readonly AssetServiceInterface $assetService, ) { parent::__construct($serializer); } @@ -90,19 +90,12 @@ public function __construct( ])] public function getAssets(#[MapQueryString] ElementParameters $parameters): JsonResponse { - $filterService = $this->filterServiceProvider->create(OpenSearchFilterInterface::SERVICE_TYPE); - - $assetQuery = $filterService->applyFilters( - $parameters, - ElementTypes::TYPE_ASSET - ); - - $result = $this->assetSearchService->searchAssets($assetQuery); + $collection = $this->assetService->getAssets($parameters); return $this->getPaginatedCollection( $this->serializer, - $result->getItems(), - $result->getTotalItems() + $collection->getItems(), + $collection->getTotalItems() ); } } diff --git a/src/Asset/Controller/CustomSettingsController.php b/src/Asset/Controller/CustomSettingsController.php index c084a2565..403405826 100644 --- a/src/Asset/Controller/CustomSettingsController.php +++ b/src/Asset/Controller/CustomSettingsController.php @@ -17,20 +17,15 @@ namespace Pimcore\Bundle\StudioBackendBundle\Asset\Controller; use OpenApi\Attributes\Get; -use Pimcore\Bundle\StaticResolverBundle\Models\Element\ServiceResolverInterface; use Pimcore\Bundle\StudioBackendBundle\Asset\Attributes\Response\Content\CustomSettingsJson; -use Pimcore\Bundle\StudioBackendBundle\Asset\Hydrator\CustomSettingsHydratorInterface; +use Pimcore\Bundle\StudioBackendBundle\Asset\Service\CustomSettingsServiceInterface; use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Path\IdParameter; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\DefaultResponses; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\SuccessResponse; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Config\Tags; -use Pimcore\Bundle\StudioBackendBundle\Security\Service\SecurityServiceInterface; -use Pimcore\Bundle\StudioBackendBundle\Util\Constants\ElementPermissions; -use Pimcore\Bundle\StudioBackendBundle\Util\Constants\ElementTypes; use Pimcore\Bundle\StudioBackendBundle\Util\Constants\HttpResponseCodes; use Pimcore\Bundle\StudioBackendBundle\Util\Traits\ElementProviderTrait; -use Pimcore\Model\Asset; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Serializer\SerializerInterface; @@ -44,9 +39,7 @@ final class CustomSettingsController extends AbstractApiController public function __construct( SerializerInterface $serializer, - private readonly CustomSettingsHydratorInterface $hydrator, - private readonly SecurityServiceInterface $securityService, - private readonly ServiceResolverInterface $serviceResolver, + private readonly CustomSettingsServiceInterface $customSettingsService ) { parent::__construct($serializer); } @@ -72,16 +65,6 @@ public function __construct( ])] public function getAssetCustomSettingsById(int $id): JsonResponse { - /** @var Asset $asset */ - $asset = $this->getElement($this->serviceResolver, ElementTypes::TYPE_ASSET, $id); - $this->securityService->hasElementPermission( - $asset, - $this->securityService->getCurrentUser(), - ElementPermissions::VIEW_PERMISSION - ); - - return $this->jsonResponse( - $this->hydrator->hydrate($asset->getCustomSettings()) - ); + return $this->jsonResponse($this->customSettingsService->getCustomSettings($id)); } } diff --git a/src/Asset/Controller/Data/TextController.php b/src/Asset/Controller/Data/TextController.php index 44018f49e..107d1c93d 100644 --- a/src/Asset/Controller/Data/TextController.php +++ b/src/Asset/Controller/Data/TextController.php @@ -17,8 +17,7 @@ namespace Pimcore\Bundle\StudioBackendBundle\Asset\Controller\Data; use OpenApi\Attributes\Get; -use Pimcore\Bundle\StaticResolverBundle\Models\Element\ServiceResolverInterface; -use Pimcore\Bundle\StudioBackendBundle\Asset\Encoder\TextEncoderInterface; +use Pimcore\Bundle\StudioBackendBundle\Asset\Service\DataServiceInterface; use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Path\IdParameter; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Content\DataJson; @@ -40,8 +39,7 @@ final class TextController extends AbstractApiController public function __construct( SerializerInterface $serializer, - private readonly ServiceResolverInterface $serviceResolver, - private readonly TextEncoderInterface $textEncoder, + private readonly DataServiceInterface $dataService ) { parent::__construct($serializer); @@ -68,8 +66,6 @@ public function __construct( ])] public function getTextData(int $id): JsonResponse { - $element = $this->getElement($this->serviceResolver, 'asset', $id); - - return $this->jsonResponse(['data' => $this->textEncoder->encodeUTF8($element)]); + return $this->jsonResponse(['data' => $this->dataService->getUTF8EncodedData($id)]); } } diff --git a/src/Asset/Controller/GetController.php b/src/Asset/Controller/GetController.php index 8fcb2946b..793db41d1 100644 --- a/src/Asset/Controller/GetController.php +++ b/src/Asset/Controller/GetController.php @@ -18,8 +18,8 @@ use OpenApi\Attributes\Get; use Pimcore\Bundle\StudioBackendBundle\Asset\Attributes\Response\Content\OneOfAssetJson; +use Pimcore\Bundle\StudioBackendBundle\Asset\Service\AssetServiceInterface; use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController; -use Pimcore\Bundle\StudioBackendBundle\DataIndex\AssetSearchServiceInterface; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Path\IdParameter; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\DefaultResponses; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\SuccessResponse; @@ -36,7 +36,7 @@ final class GetController extends AbstractApiController { public function __construct( SerializerInterface $serializer, - private readonly AssetSearchServiceInterface $assetSearchService, + private readonly AssetServiceInterface $assetService, ) { parent::__construct($serializer); } @@ -63,6 +63,6 @@ public function __construct( ])] public function getAssetById(int $id): JsonResponse { - return $this->jsonResponse($this->assetSearchService->getAssetById($id)); + return $this->jsonResponse($this->assetService->getAsset($id)); } } diff --git a/src/Asset/Event/AssetEvent.php b/src/Asset/Event/AssetEvent.php new file mode 100644 index 000000000..051e383e0 --- /dev/null +++ b/src/Asset/Event/AssetEvent.php @@ -0,0 +1,39 @@ +asset; + } +} diff --git a/src/Asset/Event/CustomSettingsEvent.php b/src/Asset/Event/CustomSettingsEvent.php new file mode 100644 index 000000000..ade8f1694 --- /dev/null +++ b/src/Asset/Event/CustomSettingsEvent.php @@ -0,0 +1,39 @@ +customSettings; + } +} diff --git a/src/Asset/Schema/Asset.php b/src/Asset/Schema/Asset.php index 91a95ca86..e5e11271a 100644 --- a/src/Asset/Schema/Asset.php +++ b/src/Asset/Schema/Asset.php @@ -21,6 +21,8 @@ use OpenApi\Attributes\Schema; use Pimcore\Bundle\StudioBackendBundle\Asset\Schema\Type\Permissions; use Pimcore\Bundle\StudioBackendBundle\Response\Element; +use Pimcore\Bundle\StudioBackendBundle\Util\Schema\AdditionalAttributesInterface; +use Pimcore\Bundle\StudioBackendBundle\Util\Traits\AdditionalAttributesTrait; /** * @internal @@ -29,8 +31,10 @@ title: 'Asset', type: 'object' )] -class Asset extends Element +class Asset extends Element implements AdditionalAttributesInterface { + use AdditionalAttributesTrait; + public function __construct( #[Property(description: 'IconName', type: 'string', example: 'pimcore_icon_pdf')] private readonly string $iconName, diff --git a/src/Asset/Schema/CustomSettings.php b/src/Asset/Schema/CustomSettings.php index 3000ff89b..3212364aa 100644 --- a/src/Asset/Schema/CustomSettings.php +++ b/src/Asset/Schema/CustomSettings.php @@ -20,6 +20,8 @@ use OpenApi\Attributes\Property; use OpenApi\Attributes\Schema; use Pimcore\Bundle\StudioBackendBundle\Asset\Schema\CustomSettings\FixedCustomSettings; +use Pimcore\Bundle\StudioBackendBundle\Util\Schema\AdditionalAttributesInterface; +use Pimcore\Bundle\StudioBackendBundle\Util\Traits\AdditionalAttributesTrait; /** * @internal @@ -28,22 +30,24 @@ title: 'CustomSettings', type: 'object' )] -final readonly class CustomSettings +final class CustomSettings implements AdditionalAttributesInterface { + use AdditionalAttributesTrait; + public function __construct( #[Property( description: 'fixed custom settings', type: FixedCustomSettings::class, example: '{ embeddedMetadata: { FileSize: 360 KiB }, checksum: b3685e8348e7ac4d30d0268f7e58902a }') ] - private ?FixedCustomSettings $fixedCustomSettings = null, + private readonly ?FixedCustomSettings $fixedCustomSettings = null, #[Property( description: 'dynamic custom settings - can be any key-value pair', type: 'array', items: new Items(), example: '{ imageWidth: 1280, imageHeight: 720 }') ] - private array $dynamicCustomSettings = [], + private readonly array $dynamicCustomSettings = [], ) { } diff --git a/src/Asset/Service/AssetService.php b/src/Asset/Service/AssetService.php new file mode 100644 index 000000000..317502257 --- /dev/null +++ b/src/Asset/Service/AssetService.php @@ -0,0 +1,88 @@ +filterServiceProvider->create(OpenSearchFilterInterface::SERVICE_TYPE); + + $assetQuery = $filterService->applyFilters( + $parameters, + ElementTypes::TYPE_ASSET + ); + + $result = $this->assetSearchService->searchAssets($assetQuery); + + $items = $result->getItems(); + + foreach ($items as $item) { + $this->eventDispatcher->dispatch( + new AssetEvent($item), + AssetEvent::EVENT_NAME + ); + + } + + return new Collection($result->getTotalItems(), $items); + } + + public function getAsset(int $id): Asset|Archive|Audio|Document|Folder|Image|Text|Unknown|Video + { + $asset = $this->assetSearchService->getAssetById($id); + + $this->eventDispatcher->dispatch( + new AssetEvent($asset), + AssetEvent::EVENT_NAME + ); + + return $asset; + } +} diff --git a/src/Asset/Service/AssetServiceInterface.php b/src/Asset/Service/AssetServiceInterface.php new file mode 100644 index 000000000..fdd44111c --- /dev/null +++ b/src/Asset/Service/AssetServiceInterface.php @@ -0,0 +1,39 @@ +getElement($this->serviceResolver, ElementTypes::TYPE_ASSET, $id); + $this->securityService->hasElementPermission( + $asset, + $this->securityService->getCurrentUser(), + ElementPermissions::VIEW_PERMISSION + ); + $customSettings = $this->hydrator->hydrate($asset->getCustomSettings()); + + $this->eventDispatcher->dispatch( + new CustomSettingsEvent($customSettings), + CustomSettingsEvent::EVENT_NAME + ); + + return $customSettings; + } +} diff --git a/src/Asset/Service/CustomSettingsServiceInterface.php b/src/Asset/Service/CustomSettingsServiceInterface.php new file mode 100644 index 000000000..a64c9cf23 --- /dev/null +++ b/src/Asset/Service/CustomSettingsServiceInterface.php @@ -0,0 +1,27 @@ +getElement($this->serviceResolver, 'asset', $id); + + return $this->textEncoder->encodeUTF8($element); + } +} diff --git a/src/Asset/Service/DataServiceInterface.php b/src/Asset/Service/DataServiceInterface.php new file mode 100644 index 000000000..bd5402bad --- /dev/null +++ b/src/Asset/Service/DataServiceInterface.php @@ -0,0 +1,25 @@ +