From d63a80f80d90c5c3c32bd158646cad08931394f7 Mon Sep 17 00:00:00 2001 From: mattamon Date: Tue, 3 Dec 2024 15:29:36 +0100 Subject: [PATCH 1/4] Add thumbnail clear --- .../Controller/Image/ImageClearController.php | 77 +++++++++++++++++++ src/Asset/Service/AssetService.php | 25 ++++++ src/Asset/Service/AssetServiceInterface.php | 7 ++ 3 files changed, 109 insertions(+) create mode 100644 src/Asset/Controller/Image/ImageClearController.php diff --git a/src/Asset/Controller/Image/ImageClearController.php b/src/Asset/Controller/Image/ImageClearController.php new file mode 100644 index 00000000..66628358 --- /dev/null +++ b/src/Asset/Controller/Image/ImageClearController.php @@ -0,0 +1,77 @@ +value)] + #[IsGranted(UserPermissions::THUMBNAILS->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::AssetThumbnails->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; } From e19c327dcee3dc35d10aa68979b232a11575f7ac Mon Sep 17 00:00:00 2001 From: mattamon Date: Tue, 3 Dec 2024 14:30:52 +0000 Subject: [PATCH 2/4] Apply php-cs-fixer changes --- src/Asset/Controller/Image/ImageClearController.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Asset/Controller/Image/ImageClearController.php b/src/Asset/Controller/Image/ImageClearController.php index 66628358..1ad1715b 100644 --- a/src/Asset/Controller/Image/ImageClearController.php +++ b/src/Asset/Controller/Image/ImageClearController.php @@ -24,7 +24,6 @@ use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\DefaultResponses; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\SuccessResponse; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Config\Tags; -use Pimcore\Bundle\StudioBackendBundle\Thumbnail\Service\ThumbnailServiceInterface; use Pimcore\Bundle\StudioBackendBundle\Util\Constant\ElementTypes; use Pimcore\Bundle\StudioBackendBundle\Util\Constant\HttpResponseCodes; use Pimcore\Bundle\StudioBackendBundle\Util\Constant\UserPermissions; @@ -66,7 +65,7 @@ public function __construct( #[DefaultResponses([ HttpResponseCodes::UNAUTHORIZED, HttpResponseCodes::NOT_FOUND, - HttpResponseCodes::INTERNAL_SERVER_ERROR + HttpResponseCodes::INTERNAL_SERVER_ERROR, ])] public function clearImageThumbnails(int $id): Response { From 16b0f772757a24a324a6a6b39da0917fab148e28 Mon Sep 17 00:00:00 2001 From: mattamon Date: Tue, 3 Dec 2024 15:42:10 +0100 Subject: [PATCH 3/4] Add translations and smaller fixes --- ...rController.php => ThumbnailImageClearController.php} | 7 +++---- translations/studio_api_docs.en.yaml | 9 +++++++-- 2 files changed, 10 insertions(+), 6 deletions(-) rename src/Asset/Controller/Image/{ImageClearController.php => ThumbnailImageClearController.php} (92%) diff --git a/src/Asset/Controller/Image/ImageClearController.php b/src/Asset/Controller/Image/ThumbnailImageClearController.php similarity index 92% rename from src/Asset/Controller/Image/ImageClearController.php rename to src/Asset/Controller/Image/ThumbnailImageClearController.php index 1ad1715b..f1ce790a 100644 --- a/src/Asset/Controller/Image/ImageClearController.php +++ b/src/Asset/Controller/Image/ThumbnailImageClearController.php @@ -14,7 +14,7 @@ * @license http://www.pimcore.org/license GPLv3 and PCL */ -namespace Pimcore\Bundle\StudioBackendBundle\Thumbnail\Controller; +namespace Pimcore\Bundle\StudioBackendBundle\Asset\Controller\Image; use OpenApi\Attributes\Delete; use Pimcore\Bundle\StudioBackendBundle\Asset\Service\AssetServiceInterface; @@ -36,7 +36,7 @@ /** * @internal */ -final class ImageClearController extends AbstractApiController +final class ThumbnailImageClearController extends AbstractApiController { use PaginatedResponseTrait; @@ -52,13 +52,12 @@ public function __construct( */ #[Route('/assets/{id}/image/thumbnail/clear', name: 'pimcore_studio_api_clear_image_thumbnail', methods: ['DELETE'])] #[IsGranted(UserPermissions::ASSETS->value)] - #[IsGranted(UserPermissions::THUMBNAILS->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::AssetThumbnails->value] + tags: [Tags::Assets->value] )] #[IdParameter(type: ElementTypes::TYPE_ASSET)] #[SuccessResponse] 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: | From 32080631c2e21c8bd47b4c8f24141e1071a8a7cc Mon Sep 17 00:00:00 2001 From: mattamon Date: Tue, 3 Dec 2024 15:48:21 +0100 Subject: [PATCH 4/4] Split line --- .../Controller/Image/ThumbnailImageClearController.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Asset/Controller/Image/ThumbnailImageClearController.php b/src/Asset/Controller/Image/ThumbnailImageClearController.php index f1ce790a..a83eca04 100644 --- a/src/Asset/Controller/Image/ThumbnailImageClearController.php +++ b/src/Asset/Controller/Image/ThumbnailImageClearController.php @@ -50,7 +50,11 @@ public function __construct( /** * @throws UserNotFoundException */ - #[Route('/assets/{id}/image/thumbnail/clear', name: 'pimcore_studio_api_clear_image_thumbnail', methods: ['DELETE'])] + #[Route( + path: '/assets/{id}/image/thumbnail/clear', + name: 'pimcore_studio_api_clear_image_thumbnail', + methods: ['DELETE'] + )] #[IsGranted(UserPermissions::ASSETS->value)] #[Delete( path: self::PREFIX . '/assets/{id}/image/thumbnail/clear',