Skip to content

Commit

Permalink
Add thumbnail clear
Browse files Browse the repository at this point in the history
  • Loading branch information
mattamon committed Dec 3, 2024
1 parent 4f570df commit d63a80f
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
77 changes: 77 additions & 0 deletions src/Asset/Controller/Image/ImageClearController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Thumbnail\Controller;

use OpenApi\Attributes\Delete;
use Pimcore\Bundle\StudioBackendBundle\Asset\Service\AssetServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\UserNotFoundException;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Parameter\Path\IdParameter;
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;
use Pimcore\Bundle\StudioBackendBundle\Util\Trait\PaginatedResponseTrait;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Component\Serializer\SerializerInterface;

/**
* @internal
*/
final class ImageClearController extends AbstractApiController
{
use PaginatedResponseTrait;

public function __construct(
SerializerInterface $serializer,
private readonly AssetServiceInterface $assetService
) {
parent::__construct($serializer);
}

/**
* @throws UserNotFoundException
*/
#[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]
)]
#[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();
}
}
25 changes: 25 additions & 0 deletions src/Asset/Service/AssetService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
7 changes: 7 additions & 0 deletions src/Asset/Service/AssetServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

0 comments on commit d63a80f

Please sign in to comment.