Skip to content

Commit

Permalink
[Asset Upload] Info endpoint (#234)
Browse files Browse the repository at this point in the history
* add endpoint to check if asset already exists

* Apply php-cs-fixer changes

* fix: STAN

* fix response codes

---------

Co-authored-by: lukmzig <[email protected]>
  • Loading branch information
lukmzig and lukmzig authored Jul 9, 2024
1 parent b7f07dd commit 93c739a
Show file tree
Hide file tree
Showing 12 changed files with 215 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/Asset/Controller/CloneController.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public function __construct(
#[IdParameter(type: ElementTypes::TYPE_ASSET)]
#[IdParameter(type: ElementTypes::TYPE_ASSET, name: 'parentId')]
#[DefaultResponses([
HttpResponseCodes::UNAUTHORIZED,
HttpResponseCodes::NOT_FOUND,
])]
public function cloneElement(
Expand Down
1 change: 1 addition & 0 deletions src/Asset/Controller/DeleteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public function __construct(
)]
#[IdParameter(type: ElementTypes::TYPE_ASSET)]
#[DefaultResponses([
HttpResponseCodes::UNAUTHORIZED,
HttpResponseCodes::NOT_FOUND,
])]
public function deleteAsset(
Expand Down
4 changes: 3 additions & 1 deletion src/Asset/Controller/PatchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\SuccessResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Config\Tags;
use Pimcore\Bundle\StudioBackendBundle\Patcher\Service\PatchServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Util\Constants\ElementTypes;
use Pimcore\Bundle\StudioBackendBundle\Util\Constants\HttpResponseCodes;
use Pimcore\Bundle\StudioBackendBundle\Util\Constants\UserPermissions;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -60,10 +61,11 @@ public function __construct(
#[PatchSuccessResponseWithErrors(content: new PatchErrorJson())]
#[DefaultResponses([
HttpResponseCodes::UNAUTHORIZED,
HttpResponseCodes::NOT_FOUND,
])]
public function patchAssets(#[MapRequestPayload] PatchAssetParameter $patchAssetParameter): Response
{
$errors = $this->patchService->patch('asset', $patchAssetParameter->getData());
$errors = $this->patchService->patch(ElementTypes::TYPE_ASSET, $patchAssetParameter->getData());

return $this->patchResponse($errors);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Asset/Controller/UpdateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function __construct(
])]
public function updateAsset(int $id, #[MapRequestPayload] UpdateAssetParameter $updateAsset): JsonResponse
{
$this->updateService->update('asset', $id, $updateAsset->getData());
$this->updateService->update(ElementTypes::TYPE_ASSET, $id, $updateAsset->getData());

return $this->jsonResponse($this->assetService->getAsset($id));
}
Expand Down
1 change: 1 addition & 0 deletions src/Asset/Controller/Upload/AddController.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public function __construct(
['file']
)]
#[DefaultResponses([
HttpResponseCodes::UNAUTHORIZED,
HttpResponseCodes::NOT_FOUND,
])]
public function addAsset(
Expand Down
91 changes: 91 additions & 0 deletions src/Asset/Controller/Upload/InfoController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?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\Asset\Controller\Upload;

use OpenApi\Attributes\Get;
use Pimcore\Bundle\StudioBackendBundle\Asset\OpenApi\Attributes\Parameters\Query\NameParameter;
use Pimcore\Bundle\StudioBackendBundle\Asset\Service\UploadServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\AccessDeniedException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Path\IdParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Content\BoolJson;
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\ElementTypes;
use Pimcore\Bundle\StudioBackendBundle\Util\Constants\HttpResponseCodes;
use Pimcore\Bundle\StudioBackendBundle\Util\Constants\UserPermissions;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Attribute\MapQueryParameter;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Component\Serializer\SerializerInterface;

/**
* @internal
*/
final class InfoController extends AbstractApiController
{
public function __construct(
SerializerInterface $serializer,
private readonly UploadServiceInterface $uploadService,
private readonly SecurityServiceInterface $securityService
) {
parent::__construct($serializer);
}

/**
* @throws AccessDeniedException
* @throws NotFoundException
* @throws UserNotFoundException
*/
#[Route('/assets/exists/{parentId}', name: 'pimcore_studio_api_get_asset', methods: ['GET'])]
#[IsGranted(UserPermissions::ASSETS->value)]
#[Get(
path: self::API_PATH . '/assets/exists/{parentId}',
operationId: 'getAssetExists',
description: 'Get information if asset already exists by parentId path parameter and fileName query string',
summary: 'Get asset info by parentId and fileName',
tags: [Tags::Assets->name]
)]
#[IdParameter(type: ElementTypes::TYPE_ASSET, name: 'parentId')]
#[NameParameter(name: 'fileName', description: 'Name of the file to upload', example: 'file.jpg')]
#[SuccessResponse(
description: 'Returns true if asset with the same name and in the same path already exists, false otherwise',
content: new BoolJson(name: 'exists', description: 'True if asset exists, false otherwise')
)]
#[DefaultResponses([
HttpResponseCodes::UNAUTHORIZED,
HttpResponseCodes::NOT_FOUND,
])]
public function getAssetExists(int $parentId, #[MapQueryParameter] string $fileName): JsonResponse
{

return $this->jsonResponse(
[
'exists' => $this->uploadService->fileExists(
$parentId,
$fileName,
$this->securityService->getCurrentUser()
),
]
);
}
}
1 change: 1 addition & 0 deletions src/Asset/Controller/Upload/ReplaceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public function __construct(
['file']
)]
#[DefaultResponses([
HttpResponseCodes::UNAUTHORIZED,
HttpResponseCodes::NOT_FOUND,
])]
public function replaceAsset(
Expand Down
1 change: 1 addition & 0 deletions src/Asset/Controller/Upload/ZipController.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public function __construct(
[self::FILE_KEY]
)]
#[DefaultResponses([
HttpResponseCodes::UNAUTHORIZED,
HttpResponseCodes::NOT_FOUND,
])]
public function addAssetsZip(
Expand Down
43 changes: 43 additions & 0 deletions src/Asset/OpenApi/Attributes/Parameters/Query/NameParameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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\Asset\OpenApi\Attributes\Parameters\Query;

use Attribute;
use OpenApi\Attributes\QueryParameter;
use OpenApi\Attributes\Schema;
use Pimcore\Bundle\StudioBackendBundle\Util\Constants\Thumbnails;

#[Attribute(Attribute::TARGET_METHOD)]
final class NameParameter extends QueryParameter
{
public function __construct(
string $name = 'thumbnailName',
string $description = 'Find asset by matching thumbnail name.',
string $example = Thumbnails::DEFAULT_THUMBNAIL_ID->value,
) {
parent::__construct(
name: $name,
description: $description,
in: 'query',
required: true,
schema: new Schema(
type: 'string',
example: $example
),
);
}
}
19 changes: 19 additions & 0 deletions src/Asset/Service/UploadService.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Pimcore\Bundle\GenericExecutionEngineBundle\Model\Job;
use Pimcore\Bundle\GenericExecutionEngineBundle\Model\JobStep;
use Pimcore\Bundle\StaticResolverBundle\Models\Asset\AssetResolverInterface;
use Pimcore\Bundle\StaticResolverBundle\Models\Asset\AssetServiceResolverInterface;
use Pimcore\Bundle\StaticResolverBundle\Models\Element\ServiceResolverInterface;
use Pimcore\Bundle\StudioBackendBundle\Asset\ExecutionEngine\AutomationAction\Messenger\Messages\AssetUploadMessage;
use Pimcore\Bundle\StudioBackendBundle\Asset\ExecutionEngine\Util\EnvironmentVariables;
Expand Down Expand Up @@ -50,13 +51,31 @@
public function __construct(
private AssetServiceInterface $assetService,
private AssetResolverInterface $assetResolver,
private AssetServiceResolverInterface $assetServiceResolver,
private JobExecutionAgentInterface $jobExecutionAgent,
private ServiceResolverInterface $serviceResolver,
private SynchronousProcessingServiceInterface $synchronousProcessingService,
) {

}

/**
* @throws AccessDeniedException
* @throws NotFoundException
*/
public function fileExists(
int $parentId,
string $fileName,
UserInterface $user
): bool {
$parent = $this->assetService->getAssetElement($user, $parentId);

return $this->assetServiceResolver->pathExists(
$parent->getRealFullPath() . '/' . $fileName,
ElementTypes::TYPE_ASSET
);
}

/**
* @throws AccessDeniedException
* @throws DatabaseException
Expand Down
10 changes: 10 additions & 0 deletions src/Asset/Service/UploadServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@
*/
interface UploadServiceInterface
{
/**
* @throws AccessDeniedException
* @throws NotFoundException
*/
public function fileExists(
int $parentId,
string $fileName,
UserInterface $user
): bool;

/**
* @throws AccessDeniedException
* @throws DatabaseException
Expand Down
43 changes: 43 additions & 0 deletions src/OpenApi/Attributes/Response/Content/BoolJson.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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\OpenApi\Attributes\Response\Content;

use OpenApi\Attributes\JsonContent;
use OpenApi\Attributes\Property;

/**
* @internal
*/
final class BoolJson extends JsonContent
{
public function __construct(string $name = '', string $description = '')
{
parent::__construct(
required: [$name],
properties: [
new Property(
$name,
title: $name,
description: $description,
type: 'boolean',
example: true
),
],
type: 'object',
);
}
}

0 comments on commit 93c739a

Please sign in to comment.