Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Copy & Paste] Assets #165

Merged
merged 10 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion config/assets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ services:
Pimcore\Bundle\StudioBackendBundle\Asset\Service\DocumentServiceInterface:
class: Pimcore\Bundle\StudioBackendBundle\Asset\Service\DocumentService

Pimcore\Bundle\StudioBackendBundle\Asset\Service\ExecutionEngine\CloneServiceInterface:
class: Pimcore\Bundle\StudioBackendBundle\Asset\Service\ExecutionEngine\CloneService

Pimcore\Bundle\StudioBackendBundle\Asset\Service\ExecutionEngine\ZipServiceInterface:
class: Pimcore\Bundle\StudioBackendBundle\Asset\Service\ExecutionEngine\ZipService

#
# Updaters
#
Expand All @@ -67,4 +73,18 @@ services:
#

Pimcore\Bundle\StudioBackendBundle\Asset\Patcher\Adapter\MetadataAdapter:
tags: [ 'pimcore.studio_backend.patch_adapter' ]
tags: [ 'pimcore.studio_backend.patch_adapter' ]

#
# Handler
#

Pimcore\Bundle\StudioBackendBundle\Asset\ExecutionEngine\AutomationAction\Messenger\Handler\ZipCollectionHandler: ~
Pimcore\Bundle\StudioBackendBundle\Asset\ExecutionEngine\AutomationAction\Messenger\Handler\ZipCreationHandler: ~
Pimcore\Bundle\StudioBackendBundle\Asset\ExecutionEngine\AutomationAction\Messenger\Handler\AssetCopyHandler: ~

#
# Event Subscriber
#

Pimcore\Bundle\StudioBackendBundle\Asset\EventSubscriber\ZipDownloadSubscriber: ~
26 changes: 0 additions & 26 deletions config/execution_engine.yaml

This file was deleted.

5 changes: 3 additions & 2 deletions config/pimcore/execution_engine.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ pimcore_generic_execution_engine:
framework:
messenger:
routing:
Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\AutomationAction\Messenger\Messages\ZipCollectionMessage: pimcore_generic_execution_engine
Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\AutomationAction\Messenger\Messages\ZipCreationMessage: pimcore_generic_execution_engine
Pimcore\Bundle\StudioBackendBundle\Asset\ExecutionEngine\AutomationAction\Messenger\Messages\ZipCollectionMessage: pimcore_generic_execution_engine
Pimcore\Bundle\StudioBackendBundle\Asset\ExecutionEngine\AutomationAction\Messenger\Messages\ZipCreationMessage: pimcore_generic_execution_engine
Pimcore\Bundle\StudioBackendBundle\Asset\ExecutionEngine\AutomationAction\Messenger\Messages\AssetCopyMessage: pimcore_generic_execution_engine
90 changes: 90 additions & 0 deletions src/Asset/Controller/CloneController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?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;

use OpenApi\Attributes\Post;
use Pimcore\Bundle\StudioBackendBundle\Asset\Service\ExecutionEngine\CloneServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\DatabaseException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Path\IdParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Content\IdJson;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\CreatedResponse;
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\Util\Constants\ElementTypes;
use Pimcore\Bundle\StudioBackendBundle\Util\Constants\HttpResponseCodes;
use Pimcore\Bundle\StudioBackendBundle\Util\Constants\UserPermissions;
use Pimcore\Bundle\StudioBackendBundle\Util\Traits\PaginatedResponseTrait;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Component\Serializer\SerializerInterface;

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

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

/**
* @throws DatabaseException|NotFoundException
*/
#[Route('/assets/{id}/clone/{parentId}', name: 'pimcore_studio_api_assets_clone', methods: ['POST'])]
#[IsGranted(UserPermissions::USER_MANAGEMENT->value)]
#[Post(
path: self::API_PATH . '/assets/{id}/clone/{parentId}',
operationId: 'cloneElement',
summary: 'Clone a specific asset.',
tags: [Tags::Assets->value]
)]
#[SuccessResponse(
description: 'Successfully copied asset',
)]
#[CreatedResponse(
description: 'Successfully copied parent asset and created jobRun for copying child assets',
content: new IdJson('ID of created jobRun')
)]
#[IdParameter(type: ElementTypes::TYPE_ASSET)]
#[IdParameter(type: ElementTypes::TYPE_ASSET, name: 'parentId')]
#[DefaultResponses([
HttpResponseCodes::NOT_FOUND,
])]
public function cloneElement(
int $id,
int $parentId
): JsonResponse {
$status = 200;
$data = null;
$jobRunId = $this->cloneService->cloneAssetRecursively($id, $parentId);
if ($jobRunId) {
$status = 201;
$data = $this->serializer->serialize(['id' => $jobRunId], 'json');
}

return new JsonResponse($data, $status, [], true);
}
}
2 changes: 1 addition & 1 deletion src/Asset/Controller/CreateZipController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
use OpenApi\Attributes\Property;
use OpenApi\Attributes\RequestBody;
use Pimcore\Bundle\StudioBackendBundle\Asset\MappedParameter\CreateZipParameter;
use Pimcore\Bundle\StudioBackendBundle\Asset\Service\ExecutionEngine\ZipServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Service\ZipServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Content\ScalarItemsJson;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\DefaultResponses;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\SuccessResponse;
Expand Down
2 changes: 1 addition & 1 deletion src/Asset/Controller/DownloadZipController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
use OpenApi\Attributes\Get;
use Pimcore\Bundle\StudioBackendBundle\Asset\Attributes\Response\Content\AssetMediaType;
use Pimcore\Bundle\StudioBackendBundle\Asset\Attributes\Response\Header\ContentDisposition;
use Pimcore\Bundle\StudioBackendBundle\Asset\MappedParameter\ZipPathParameter;
use Pimcore\Bundle\StudioBackendBundle\Asset\Service\DownloadServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\MappedParameter\ZipPathParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Query\PathParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\DefaultResponses;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\SuccessResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\EventSubscriber;
namespace Pimcore\Bundle\StudioBackendBundle\Asset\EventSubscriber;

use Pimcore\Bundle\GenericExecutionEngineBundle\Event\JobRunStateChangedEvent;
use Pimcore\Bundle\GenericExecutionEngineBundle\Model\JobRunStates;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?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\ExecutionEngine\AutomationAction\Messenger\Handler;

use Exception;
use Pimcore\Bundle\GenericExecutionEngineBundle\Messenger\Handler\AbstractAutomationActionHandler;
use Pimcore\Bundle\StaticResolverBundle\Models\User\UserResolverInterface;
use Pimcore\Bundle\StudioBackendBundle\Asset\ExecutionEngine\AutomationAction\Messenger\Messages\AssetCopyMessage;
use Pimcore\Bundle\StudioBackendBundle\Asset\Service\AssetServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Asset\Service\ExecutionEngine\CloneServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Model\AbortActionData;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\Config;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\Traits\HandlerValidationTrait;
use Pimcore\Bundle\StudioBackendBundle\Util\Constants\Asset\CloneEnvironmentVariables;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;

/**
* @internal
*/
#[AsMessageHandler]
final class AssetCopyHandler extends AbstractAutomationActionHandler
{
use HandlerValidationTrait;

public function __construct(
private readonly AssetServiceInterface $assetService,
private readonly UserResolverInterface $userResolver,
private readonly CloneServiceInterface $cloneService
) {
parent::__construct();
}

/**
* @throws Exception
*/
public function __invoke(AssetCopyMessage $message): void
{
$jobRun = $this->getJobRun($message);
$validatedParameters = $this->validateJobParameters(
$message,
$jobRun,
$this->userResolver,
[
CloneEnvironmentVariables::ORIGINAL_PARENT_ID->value,
CloneEnvironmentVariables::PARENT_ID->value,
],
);

if ($validatedParameters instanceof AbortActionData) {
$this->abortAction(
$validatedParameters->getTranslationKey(),
$validatedParameters->getTranslationParameters(),
Config::CONTEXT->value,
$validatedParameters->getExceptionClassName()
);
}

$user = $validatedParameters->getUser();
$assetElement = $validatedParameters->getSubject();
$environmentVariables = $validatedParameters->getEnvironmentData();
$source = $this->assetService->getAssetElement($user, $assetElement->getId());
$parent = $this->cloneService->getNewCloneTarget(
$user,
$source,
$environmentVariables[CloneEnvironmentVariables::ORIGINAL_PARENT_ID->value],
$environmentVariables[CloneEnvironmentVariables::PARENT_ID->value],
);

// TODO Send SSE for percentage update
$this->cloneService->cloneElement(
$source,
$parent,
$user
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\AutomationAction\Messenger\Handler;
namespace Pimcore\Bundle\StudioBackendBundle\Asset\ExecutionEngine\AutomationAction\Messenger\Handler;

use Exception;
use Pimcore\Bundle\GenericExecutionEngineBundle\Messenger\Handler\AbstractAutomationActionHandler;
use Pimcore\Bundle\StaticResolverBundle\Models\User\UserResolverInterface;
use Pimcore\Bundle\StudioBackendBundle\Asset\ExecutionEngine\AutomationAction\Messenger\Messages\ZipCollectionMessage;
use Pimcore\Bundle\StudioBackendBundle\Asset\Service\AssetServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\AutomationAction\Messenger\Messages\ZipCollectionMessage;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Service\ZipServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Asset\Service\ExecutionEngine\ZipServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Model\AbortActionData;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\Config;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\Traits\HandlerValidationTrait;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;

/**
Expand All @@ -31,6 +33,8 @@
#[AsMessageHandler]
final class ZipCollectionHandler extends AbstractAutomationActionHandler
{
use HandlerValidationTrait;

public function __construct(
private readonly AssetServiceInterface $assetService,
private readonly UserResolverInterface $userResolver
Expand All @@ -43,31 +47,26 @@ public function __construct(
*/
public function __invoke(ZipCollectionMessage $message): void
{
$asset = $message->getElement();

if (!$asset) {
$this->abortAction(
'no_asset_found',
[],
'studio_backend',
NotFoundException::class
);
}

$jobRun = $this->getJobRun($message);
$validatedParameters = $this->validateJobParameters(
$message,
$jobRun,
$this->userResolver
);

$user = $this->userResolver->getById($jobRun->getOwnerId());

if (!$user) {
if ($validatedParameters instanceof AbortActionData) {
$this->abortAction(
'no_user_found',
[],
'studio_backend',
NotFoundException::class
$validatedParameters->getTranslationKey(),
$validatedParameters->getTranslationParameters(),
Config::CONTEXT->value,
$validatedParameters->getExceptionClassName()
);
}

$asset = $this->assetService->getAssetElement($user, $asset->getId());
$user = $validatedParameters->getUser();
$jobSubject = $validatedParameters->getSubject();

$asset = $this->assetService->getAssetElement($user, $jobSubject->getId());

$context = $jobRun->getContext();

Expand Down
Loading
Loading