Skip to content

Commit

Permalink
[Improvement] Reports export csv (#709)
Browse files Browse the repository at this point in the history
* Added csv export messages, handler and controller

* Refactor csv export service part I

* Refactor csv export service part I

* Apply php-cs-fixer changes

* Refactor csv export service part 2

* Apply php-cs-fixer changes

* Added custom report csv endpoint

* Apply php-cs-fixer changes

* Deleted unnecessary class

* Changed endpoint to post

* Apply php-cs-fixer changes

* Moved download/delete csv endpoint to export tag

* Apply php-cs-fixer changes

* Fix tests

* Apply php-cs-fixer changes

* Permissions

* Apply php-cs-fixer changes

* Inject interface instead of service, added api tag
  • Loading branch information
mcop1 authored Jan 22, 2025
1 parent 1d4bd63 commit b986081
Show file tree
Hide file tree
Showing 45 changed files with 1,046 additions and 317 deletions.
2 changes: 0 additions & 2 deletions config/assets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ services:

Pimcore\Bundle\StudioBackendBundle\Asset\ExecutionEngine\AutomationAction\Messenger\Handler\AssetCloneHandler: ~
Pimcore\Bundle\StudioBackendBundle\Asset\ExecutionEngine\AutomationAction\Messenger\Handler\AssetUploadHandler: ~
Pimcore\Bundle\StudioBackendBundle\Asset\ExecutionEngine\AutomationAction\Messenger\Handler\CsvCreationHandler: ~
Pimcore\Bundle\StudioBackendBundle\Asset\ExecutionEngine\AutomationAction\Messenger\Handler\CsvAssetDataCollectionHandler: ~
Pimcore\Bundle\StudioBackendBundle\Asset\ExecutionEngine\AutomationAction\Messenger\Handler\CsvFolderDataCollectionHandler: ~
Pimcore\Bundle\StudioBackendBundle\Asset\ExecutionEngine\AutomationAction\Messenger\Handler\ZipDownloadHandler: ~
Expand All @@ -96,7 +95,6 @@ services:
#

Pimcore\Bundle\StudioBackendBundle\Asset\EventSubscriber\CloneSubscriber: ~
Pimcore\Bundle\StudioBackendBundle\Asset\EventSubscriber\CsvCreationSubscriber: ~
Pimcore\Bundle\StudioBackendBundle\Asset\EventSubscriber\UploadSubscriber: ~
Pimcore\Bundle\StudioBackendBundle\Asset\EventSubscriber\ZipDownloadSubscriber: ~
Pimcore\Bundle\StudioBackendBundle\Asset\EventSubscriber\ZipUploadSubscriber: ~
Expand Down
18 changes: 17 additions & 1 deletion config/custom_reports.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,26 @@ services:
# Services
#

Pimcore\Bundle\StudioBackendBundle\CustomReport\Service\CsvServiceInterface:
class: Pimcore\Bundle\StudioBackendBundle\CustomReport\Service\CsvService

Pimcore\Bundle\StudioBackendBundle\CustomReport\Service\CustomReportServiceInterface:
class: Pimcore\Bundle\StudioBackendBundle\CustomReport\Service\CustomReportService

Pimcore\Bundle\StudioBackendBundle\CustomReport\Service\AdapterServiceInterface:
class: Pimcore\Bundle\StudioBackendBundle\CustomReport\Service\AdapterService
arguments:
$adapters: '@pimcore.custom_report.adapter.factories'
$adapters: '@pimcore.custom_report.adapter.factories'

#
# Messages
#

Pimcore\Bundle\StudioBackendBundle\CustomReport\ExecutionEngine\AutomationAction\Messenger\Messages\CsvCollectionMessage: ~

#
# Handler
#

Pimcore\Bundle\StudioBackendBundle\CustomReport\ExecutionEngine\AutomationAction\Messenger\Handler\CsvCollectionHandler: ~

33 changes: 33 additions & 0 deletions config/export.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
services:
_defaults:
autowire: true
autoconfigure: true
public: false

# controllers are imported separately to make sure they're public
# and have a tag that allows actions to type-hint services
Pimcore\Bundle\StudioBackendBundle\Export\Controller\:
resource: '../src/Export/Controller'
public: true
tags: [ 'controller.service_arguments' ]

#
# Handler
#

Pimcore\Bundle\StudioBackendBundle\Export\ExecutionEngine\AutomationAction\Messenger\Handler\CsvCreationHandler: ~

#
# Services
#

Pimcore\Bundle\StudioBackendBundle\Export\Csv\CsvExportService: ~
Pimcore\Bundle\StudioBackendBundle\Export\ExportServiceInterface: '@Pimcore\Bundle\StudioBackendBundle\Export\Csv\CsvExportService'
Pimcore\Bundle\StudioBackendBundle\Export\Service\DownloadServiceInterface:
class: Pimcore\Bundle\StudioBackendBundle\Export\Service\DownloadService

#
# Event Subscriber
#

Pimcore\Bundle\StudioBackendBundle\Export\EventSubscriber\Csv\CsvCreationSubscriber: ~
5 changes: 3 additions & 2 deletions config/pimcore/execution_engine.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ framework:
routing:
Pimcore\Bundle\StudioBackendBundle\Asset\ExecutionEngine\AutomationAction\Messenger\Messages\CsvAssetCollectionMessage: pimcore_generic_execution_engine
Pimcore\Bundle\StudioBackendBundle\Asset\ExecutionEngine\AutomationAction\Messenger\Messages\CsvFolderCollectionMessage: pimcore_generic_execution_engine
Pimcore\Bundle\StudioBackendBundle\Asset\ExecutionEngine\AutomationAction\Messenger\Messages\CsvCreationMessage: pimcore_generic_execution_engine
Pimcore\Bundle\StudioBackendBundle\Asset\ExecutionEngine\AutomationAction\Messenger\Messages\AssetCloneMessage: pimcore_generic_execution_engine
Pimcore\Bundle\StudioBackendBundle\Asset\ExecutionEngine\AutomationAction\Messenger\Messages\AssetUploadMessage: pimcore_generic_execution_engine
Pimcore\Bundle\StudioBackendBundle\Asset\ExecutionEngine\AutomationAction\Messenger\Messages\ZipDownloadMessage: pimcore_generic_execution_engine
Expand All @@ -23,5 +22,7 @@ framework:
Pimcore\Bundle\StudioBackendBundle\Element\ExecutionEngine\AutomationAction\Messenger\Messages\PatchMessage: pimcore_generic_execution_engine
Pimcore\Bundle\StudioBackendBundle\Element\ExecutionEngine\AutomationAction\Messenger\Messages\PatchFolderMessage: pimcore_generic_execution_engine
Pimcore\Bundle\StudioBackendBundle\Element\ExecutionEngine\AutomationAction\Messenger\Messages\RewriteRefMessage: pimcore_generic_execution_engine
Pimcore\Bundle\StudioBackendBundle\Export\ExecutionEngine\AutomationAction\Messenger\Messages\CsvCreationMessage: pimcore_generic_execution_engine
Pimcore\Bundle\StudioBackendBundle\DataObject\ExecutionEngine\AutomationAction\Messenger\Messages\CloneMessage: pimcore_generic_execution_engine
Pimcore\Bundle\StudioBackendBundle\Tag\ExecutionEngine\AutomationAction\Messenger\Messages\BatchTagOperationMessage: pimcore_generic_execution_engine
Pimcore\Bundle\StudioBackendBundle\Tag\ExecutionEngine\AutomationAction\Messenger\Messages\BatchTagOperationMessage: pimcore_generic_execution_engine
Pimcore\Bundle\StudioBackendBundle\CustomReport\ExecutionEngine\AutomationAction\Messenger\Messages\CsvCollectionMessage: pimcore_generic_execution_engine
2 changes: 1 addition & 1 deletion src/Asset/Controller/Download/DeleteZipController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
namespace Pimcore\Bundle\StudioBackendBundle\Asset\Controller\Download;

use OpenApi\Attributes\Delete;
use Pimcore\Bundle\StudioBackendBundle\Asset\Service\DownloadServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Asset\Service\ExecutionEngine\ZipServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ForbiddenException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Export\Service\DownloadServiceInterface;
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;
Expand Down
2 changes: 1 addition & 1 deletion src/Asset/Controller/Download/DownloadZipController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@

use OpenApi\Attributes\Get;
use Pimcore\Bundle\StudioBackendBundle\Asset\Attribute\Response\Header\ContentDisposition;
use Pimcore\Bundle\StudioBackendBundle\Asset\Service\DownloadServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Asset\Service\ExecutionEngine\ZipServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\EnvironmentException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ForbiddenException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\StreamResourceNotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Export\Service\DownloadServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Parameter\Path\IdParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\Content\MediaType;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\DefaultResponses;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function __invoke(CsvAssetCollectionMessage $message): void
),
];

$this->updateContextArrayValues($jobRun, StepConfig::ASSET_EXPORT_DATA->value, $assetData);
$this->updateContextArrayValues($jobRun, StepConfig::CSV_EXPORT_DATA->value, $assetData);
} catch (Exception $e) {
$this->abort($this->getAbortData(
Config::CSV_DATA_COLLECTION_FAILED_MESSAGE->value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function __invoke(CsvFolderCollectionMessage $message): void
),
];

$this->updateContextArrayValues($jobRun, StepConfig::ASSET_EXPORT_DATA->value, $assetData);
$this->updateContextArrayValues($jobRun, StepConfig::CSV_EXPORT_DATA->value, $assetData);
} catch (Exception $e) {
$this->abort($this->getAbortData(
Config::CSV_DATA_COLLECTION_FAILED_MESSAGE->value,
Expand Down
1 change: 0 additions & 1 deletion src/Asset/ExecutionEngine/Util/JobSteps.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,4 @@ enum JobSteps: string
case ASSET_CLONING = 'studio_ee_job_step_asset_cloning';
case ASSET_UPLOADING = 'studio_ee_job_step_asset_uploading';
case CSV_COLLECTION = 'studio_ee_job_step_csv_collection';
case CSV_CREATION = 'studio_ee_job_step_csv_creation';
}
95 changes: 0 additions & 95 deletions src/Asset/Service/DownloadService.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,12 @@
namespace Pimcore\Bundle\StudioBackendBundle\Asset\Service;

use Exception;
use League\Flysystem\FilesystemException;
use League\Flysystem\FilesystemOperator;
use Pimcore\Bundle\StudioBackendBundle\Asset\MappedParameter\ImageDownloadConfigParameter;
use Pimcore\Bundle\StudioBackendBundle\Element\Service\StorageServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ElementStreamResourceNotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\EnvironmentException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ForbiddenException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidAssetFormatTypeException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidElementTypeException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidThumbnailException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\StreamResourceNotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ThumbnailResizingFailedException;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Service\ExecutionEngineServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\Asset\FormatTypes;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\HttpResponseHeaders;
use Pimcore\Bundle\StudioBackendBundle\Util\Trait\StreamedResponseTrait;
Expand All @@ -41,7 +33,6 @@
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\StreamedResponse;
use function in_array;
use function sprintf;

/**
* @internal
Expand All @@ -52,8 +43,6 @@
use TempFilePathTrait;

public function __construct(
private ExecutionEngineServiceInterface $executionEngineService,
private StorageServiceInterface $storageService,
private ThumbnailServiceInterface $thumbnailService,
private array $defaultFormats,
) {
Expand Down Expand Up @@ -145,88 +134,4 @@ public function downloadImageByThumbnail(
false
);
}

/**
* @throws EnvironmentException|ForbiddenException|NotFoundException|StreamResourceNotFoundException
*/
public function downloadResourceByJobRunId(
int $jobRunId,
string $tempFileName,
string $tempFolderName,
string $mimeType,
string $downloadName,
): StreamedResponse {
$this->executionEngineService->validateJobRun($jobRunId);
$fileName = $this->getTempFileName($jobRunId, $tempFileName);
$folderName = $this->getTempFileName($jobRunId, $tempFolderName);
$filePath = $folderName . '/' . $fileName;

$streamedResponse = $this->getFileStreamedResponse(
$filePath,
$mimeType,
$downloadName,
$this->validateStorage($filePath, $jobRunId)
);

try {
$this->storageService->cleanUpFolder($folderName);
} catch (FilesystemException) {
throw new EnvironmentException(
sprintf(
'Failed to clean up temporary folder %s',
$folderName
)
);
}

return $streamedResponse;
}

/**
* @throws EnvironmentException|NotFoundException
*/
public function cleanupDataByJobRunId(
int $jobRunId,
string $folderName,
string $fileName
): void {
$this->executionEngineService->validateJobRun($jobRunId);
$this->validateStorage($this->getTempFilePath($jobRunId, $folderName . '/' . $fileName), $jobRunId);

try {
$this->storageService->cleanUpFolder(
$this->getTempFileName(
$jobRunId,
$folderName
),
true
);
} catch (FilesystemException $e) {
throw new EnvironmentException(
sprintf(
'Failed to delete file based on jobRunId %d: %s',
$jobRunId,
$e->getMessage()
),
);
}
}

/**
* @throws EnvironmentException
*/
private function validateStorage(string $filePath, int $jobRunId): FilesystemOperator
{
$storage = $this->storageService->getTempStorage();
if (!$this->storageService->tempFileExists($filePath)) {
throw new EnvironmentException(
sprintf(
'Resource not found for jobRun with Id %d',
$jobRunId
)
);
}

return $storage;
}
}
24 changes: 0 additions & 24 deletions src/Asset/Service/DownloadServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,8 @@

use Pimcore\Bundle\StudioBackendBundle\Asset\MappedParameter\ImageDownloadConfigParameter;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ElementStreamResourceNotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\EnvironmentException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ForbiddenException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidAssetFormatTypeException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidElementTypeException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\StreamResourceNotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ThumbnailResizingFailedException;
use Pimcore\Model\Asset;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
Expand Down Expand Up @@ -64,24 +60,4 @@ public function downloadImageByThumbnail(
Asset $image,
string $thumbnailName
): BinaryFileResponse;

/**
* @throws EnvironmentException|ForbiddenException|NotFoundException|StreamResourceNotFoundException
*/
public function downloadResourceByJobRunId(
int $jobRunId,
string $tempFileName,
string $tempFolderName,
string $mimeType,
string $downloadName,
): StreamedResponse;

/**
* @throws EnvironmentException|NotFoundException
*/
public function cleanupDataByJobRunId(
int $jobRunId,
string $folderName,
string $fileName
): void;
}
Loading

0 comments on commit b986081

Please sign in to comment.