From ffdc6b6fec20d3b21c9d38872878b158fc203385 Mon Sep 17 00:00:00 2001 From: Matthias Schuhmayer <38959016+mattamon@users.noreply.github.com> Date: Wed, 4 Dec 2024 08:17:00 +0100 Subject: [PATCH] [Task][Asset] Add thumbnail clear (#612) * Add thumbnail clear * Apply php-cs-fixer changes * Add translations and smaller fixes * Split line --------- Co-authored-by: mattamon --- .../Image/ThumbnailImageClearController.php | 79 +++++++++++++++++++ src/Asset/Service/AssetService.php | 25 ++++++ src/Asset/Service/AssetServiceInterface.php | 7 ++ translations/studio_api_docs.en.yaml | 9 ++- 4 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 src/Asset/Controller/Image/ThumbnailImageClearController.php diff --git a/src/Asset/Controller/Image/ThumbnailImageClearController.php b/src/Asset/Controller/Image/ThumbnailImageClearController.php new file mode 100644 index 00000000..a83eca04 --- /dev/null +++ b/src/Asset/Controller/Image/ThumbnailImageClearController.php @@ -0,0 +1,79 @@ +value)] + #[Delete( + path: self::PREFIX . '/assets/{id}/image/thumbnail/clear', + operationId: 'asset_image_clear_thumbnail', + description: 'asset_image_clear_thumbnail_description', + summary: 'asset_image_clear_thumbnail_summary', + tags: [Tags::Assets->value] + )] + #[IdParameter(type: ElementTypes::TYPE_ASSET)] + #[SuccessResponse] + #[DefaultResponses([ + HttpResponseCodes::UNAUTHORIZED, + HttpResponseCodes::NOT_FOUND, + HttpResponseCodes::INTERNAL_SERVER_ERROR, + ])] + public function clearImageThumbnails(int $id): Response + { + $this->assetService->clearThumbnails($id); + + return new Response(); + } +} diff --git a/src/Asset/Service/AssetService.php b/src/Asset/Service/AssetService.php index 5d6c2e8d..95466bcd 100644 --- a/src/Asset/Service/AssetService.php +++ b/src/Asset/Service/AssetService.php @@ -16,6 +16,7 @@ namespace Pimcore\Bundle\StudioBackendBundle\Asset\Service; +use Exception; use Pimcore\Bundle\StaticResolverBundle\Models\Asset\AssetServiceResolverInterface; use Pimcore\Bundle\StaticResolverBundle\Models\Element\ServiceResolverInterface; use Pimcore\Bundle\StudioBackendBundle\Asset\Event\PreResponse\AssetEvent; @@ -33,6 +34,8 @@ use Pimcore\Bundle\StudioBackendBundle\DataIndex\Query\AssetQueryInterface; use Pimcore\Bundle\StudioBackendBundle\DataIndex\Request\ElementParameters; use Pimcore\Bundle\StudioBackendBundle\Exception\Api\AccessDeniedException; +use Pimcore\Bundle\StudioBackendBundle\Exception\Api\DatabaseException; +use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ForbiddenException; use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidElementTypeException; use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidFilterServiceTypeException; use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidFilterTypeException; @@ -252,6 +255,28 @@ public function getUniqueAssetName(string $targetPath, string $filename): string } } + /** + * @throws DatabaseException|ForbiddenException + */ + public function clearThumbnails(int $id): void + { + $asset = $this->getAsset($id); + + if (!$asset->getPermissions()->isPublish()) { + throw new ForbiddenException('Not allowed to clear thumbnails for this asset'); + } + + $asset = $this->serviceResolver->getElementById('asset', $id); + + $asset->clearThumbnails(true); // force clear + + try { + $asset->save(); + } catch (Exception $e) { + throw new DatabaseException($e->getMessage()); + } + } + private function dispatchAssetEvent(mixed $asset): void { $this->eventDispatcher->dispatch( diff --git a/src/Asset/Service/AssetServiceInterface.php b/src/Asset/Service/AssetServiceInterface.php index b652f241..d49787cb 100644 --- a/src/Asset/Service/AssetServiceInterface.php +++ b/src/Asset/Service/AssetServiceInterface.php @@ -26,6 +26,8 @@ use Pimcore\Bundle\StudioBackendBundle\Asset\Schema\Type\Unknown; use Pimcore\Bundle\StudioBackendBundle\Asset\Schema\Type\Video; use Pimcore\Bundle\StudioBackendBundle\DataIndex\Request\ElementParameters; +use Pimcore\Bundle\StudioBackendBundle\Exception\Api\DatabaseException; +use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ForbiddenException; use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidFilterServiceTypeException; use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidFilterTypeException; use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidQueryTypeException; @@ -103,4 +105,9 @@ public function getAssetElementByPath( ): AssetModel; public function getUniqueAssetName(string $targetPath, string $filename): string; + + /** + * @throws DatabaseException|ForbiddenException + */ + public function clearThumbnails(int $id): void; } diff --git a/translations/studio_api_docs.en.yaml b/translations/studio_api_docs.en.yaml index f30bde53..c100ad6a 100644 --- a/translations/studio_api_docs.en.yaml +++ b/translations/studio_api_docs.en.yaml @@ -100,8 +100,13 @@ asset_get_tree_description: | You can use different query parameters to filter the assets and it is possible to exclude folders from the result completely asset_get_tree_success_response: Paginated assets with total count as header param as JSON asset_get_tree_summary: Get all asset data for the tree -asset_image_download_by_format_description: | - Download the image asset based on the provided {id} and {format}.
The {id} must be an ID of existing asset image +asset_image_create_thumbnail_description: | + Create a new image thumbnail based on the provided {id} and configuration parameters.
+ The {id} must be an ID of existing asset image +asset_image_clear_thumbnail_summary: Clear image thumbnail by ID +asset_image_clear_thumbnail_description: | + Clear the image thumbnail based on the provided {id}.
+ The {id} must be an ID of existing asset image asset_image_download_by_format_success_response: Image asset binary file based on format asset_image_download_by_format_summary: Download image asset by ID and format asset_image_download_by_thumbnail_description: |