Skip to content
Open
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
1 change: 1 addition & 0 deletions src/ExecutionEngine/Util/StepConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ enum StepConfig: string
case CONFIG_COLUMNS = 'columns';
case CONFIG_FILTERS = 'filters';
case SETTINGS_DELIMITER = 'delimiter';
case SETTINGS_FILE_NAME = 'fileName';
Comment thread
Jonathon-Meney-Torq marked this conversation as resolved.
case SETTINGS_HEADER = 'header';
case SETTINGS_HEADER_NO_HEADER = 'no_header';
case SETTINGS_HEADER_TITLE = 'title';
Expand Down
12 changes: 11 additions & 1 deletion src/Export/Attribute/Request/ExportDataRequestBody.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ public function __construct(bool $addDelimiter = true)
new Property(
property: StepConfig::SETTINGS_HEADER->value,
type: 'string',
enum: StepConfig::values(),
enum: [
StepConfig::SETTINGS_HEADER_NO_HEADER->value,
StepConfig::SETTINGS_HEADER_TITLE->value,
StepConfig::SETTINGS_HEADER_NAME->value,
],
example: StepConfig::SETTINGS_HEADER_TITLE->value
),
];
Expand All @@ -45,6 +49,12 @@ enum: StepConfig::values(),
example: ';'
);
}
$configProperties[] = new Property(
property: StepConfig::SETTINGS_FILE_NAME->value,
type: 'string',
example: $addDelimiter ? 'my-grid-export.csv' : 'my-grid-export.xlsx',
nullable: true
);
parent::__construct(
content: new JsonContent(
properties: [
Expand Down
12 changes: 11 additions & 1 deletion src/Export/Attribute/Request/ExportFolderDataRequestBody.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ public function __construct(bool $addDelimiter = true)
new Property(
property: StepConfig::SETTINGS_HEADER->value,
type: 'string',
enum: StepConfig::values(),
enum: [
StepConfig::SETTINGS_HEADER_NO_HEADER->value,
StepConfig::SETTINGS_HEADER_TITLE->value,
StepConfig::SETTINGS_HEADER_NAME->value,
],
example: StepConfig::SETTINGS_HEADER_TITLE->value
),
];
Expand All @@ -46,6 +50,12 @@ enum: StepConfig::values(),
example: ';'
);
}
$configProperties[] = new Property(
property: StepConfig::SETTINGS_FILE_NAME->value,
type: 'string',
example: $addDelimiter ? 'my-grid-export.csv' : 'my-grid-export.xlsx',
nullable: true
);

parent::__construct(
content: new JsonContent(
Expand Down
15 changes: 8 additions & 7 deletions src/Export/Service/DownloadService.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ public function downloadResourceByJobRunId(
): StreamedResponse {
$this->executionEngineService->validateJobRun($jobRunId);

if ($downloadName === null) {
$downloadName = $this->resolveDownloadName($jobRunId);
}
$downloadName = $this->resolveDownloadName($jobRunId)
?? $downloadName
?? DownloadDefaults::DEFAULT_ZIP_FILENAME->value;
Comment thread
Jonathon-Meney-Torq marked this conversation as resolved.

$fileName = $this->getTempFileName($jobRunId, $tempFileName);
$folderName = $this->getTempFileName($jobRunId, $tempFolderName);
Expand Down Expand Up @@ -157,16 +157,17 @@ public function downloadJSON(string $json, string $filename): Response
return $response;
}

private function resolveDownloadName(int $jobRunId): string
private function resolveDownloadName(int $jobRunId): ?string
{
try {
$jobRun = $this->jobRunRepository->getJobRunById($jobRunId);
$environmentData = $jobRun->getJob()?->getEnvironmentData() ?? [];

return $environmentData[ZipServiceInterface::ZIP_DOWNLOAD_FILENAME] ??
DownloadDefaults::DEFAULT_ZIP_FILENAME->value;
return $environmentData[DownloadServiceInterface::EXPORT_DOWNLOAD_FILENAME] ??
$environmentData[ZipServiceInterface::ZIP_DOWNLOAD_FILENAME] ??
null;
} catch (Exception) {
return DownloadDefaults::DEFAULT_ZIP_FILENAME->value;
return null;
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/Export/Service/DownloadServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
*/
interface DownloadServiceInterface
{
public const EXPORT_DOWNLOAD_FILENAME = 'export_download_filename';

/**
* @throws EnvironmentException|ForbiddenException|NotFoundException|StreamResourceNotFoundException
*/
Expand Down
36 changes: 32 additions & 4 deletions src/Export/Service/ExecutionEngine/ExportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@
use Pimcore\Bundle\StudioBackendBundle\Export\ExecutionEngine\Util\JobSteps;
use Pimcore\Bundle\StudioBackendBundle\Export\MappedParameter\ExportFolderParameter;
use Pimcore\Bundle\StudioBackendBundle\Export\MappedParameter\ExportParameter;
use Pimcore\Bundle\StudioBackendBundle\Export\Service\DownloadServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Export\Util\Constant\ExportFormat;
use Pimcore\Bundle\StudioBackendBundle\Security\Service\SecurityServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\ElementTypes;
use Pimcore\Model\Element\ElementDescriptor;
use Pimcore\Model\UserInterface;
use function basename;
use function is_string;
use function trim;

/**
* @internal
Expand Down Expand Up @@ -91,7 +95,12 @@
$user = $this->securityService->getCurrentUser();
}

return $this->generateExportFileJob($jobSteps, $exportFormat, $user->getId());
return $this->generateExportFileJob(
$jobSteps,
$exportFormat,
$user->getId(),
downloadFileName: $this->getDownloadFileName($exportParameter->getConfig())
);
}

/**
Expand Down Expand Up @@ -127,7 +136,8 @@
$exportFormat,
$this->securityService->getCurrentUser()->getId(),
[new ElementDescriptor($elementType, $folderId)],
true
true,
$this->getDownloadFileName($exportParameter->getConfig())
);
}

Expand All @@ -136,7 +146,8 @@
string $exportFormat,
int $ownerId,
array $selectedElements = [],
bool $isFolder = false
bool $isFolder = false,
?string $downloadFileName = null
): int {
$name = $this->createJobNameByFormat($exportFormat);
if ($isFolder) {
Expand All @@ -145,15 +156,32 @@
: Jobs::COLLECT_CSV_FOLDER_EXPORT_ELEMENTS->value;
}

$environmentData = [];
if ($downloadFileName !== null) {
$environmentData[DownloadServiceInterface::EXPORT_DOWNLOAD_FILENAME] = $downloadFileName;
}

$jobRun = $this->jobExecutionAgent->startJobExecution(
new Job($name, $jobSteps, $selectedElements),
new Job($name, $jobSteps, $selectedElements, $environmentData),
$ownerId,
Config::CONTEXT_STOP_ON_ERROR->value
);

return $jobRun->getId();
}

private function getDownloadFileName(array $config): ?string
{
$fileName = $config[StepConfig::SETTINGS_FILE_NAME->value] ?? null;
if (!is_string($fileName) || trim($fileName) === '') {
return null;
}

$fileName = preg_replace('/[^\w\-. ]/', '_', basename(trim($fileName)));

Check warning on line 180 in src/Export/Service/ExecutionEngine/ExportService.php

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Enable the "u" flag or use a Unicode-aware alternative.

See more on https://sonarcloud.io/project/issues?id=pimcore_studio-backend-bundle&issues=AaBeF_qL2Y27x6jZQjDv&open=AaBeF_qL2Y27x6jZQjDv&pullRequest=2023

return $fileName === '' ? null : $fileName;
}

private function mapJobSteps(
array $elements,
array $collectionSettings,
Expand Down
101 changes: 101 additions & 0 deletions tests/Unit/Export/Service/DownloadServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\EnvironmentException;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Service\ExecutionEngineServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Export\Service\DownloadService;
use Pimcore\Bundle\StudioBackendBundle\Export\Service\DownloadServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\Asset\MimeTypes;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\HttpResponseHeaders;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -453,6 +454,106 @@ public function testResolveDownloadNameFromJobRunEnvironmentData(): void
);
}

/**
* @throws Exception
*/
public function testExportDownloadNameOverridesZipMetadataAndControllerFallback(): void
{
$stream = fopen('php://memory', 'rb+');
fwrite($stream, 'data');
rewind($stream);

$storage = $this->makeEmpty(FilesystemOperator::class, [
'readStream' => $stream,
'fileSize' => 4,
]);

$job = new Job(
name: 'test-job',
steps: [new JobStep('step', 'SomeMessage', '', [])],
environmentData: [
DownloadServiceInterface::EXPORT_DOWNLOAD_FILENAME => 'Product_2026-09-01_14-32-10.csv',
ZipServiceInterface::ZIP_DOWNLOAD_FILENAME => 'my-folder.zip',
],
);

$jobRun = $this->makeEmpty(JobRun::class, [
'getJob' => $job,
]);

$service = $this->createService(
storageService: $this->makeEmpty(StorageServiceInterface::class, [
'getTempStorage' => $storage,
'tempFileExists' => true,
]),
jobRunRepository: $this->makeEmpty(JobRunRepositoryInterface::class, [
'getJobRunById' => $jobRun,
]),
);

$response = $service->downloadResourceByJobRunId(
jobRunId: 1,
tempFileName: 'download-csv-{id}.csv',
tempFolderName: 'download-csv-{id}',
mimeType: 'text/csv',
downloadName: 'export.csv',
);

$this->assertSame(
'attachment; filename="Product_2026-09-01_14-32-10.csv"',
$response->headers->get('Content-Disposition'),
'The export file name from the job environment data must override both the ZIP metadata and the controller-provided fallback'
);
}

/**
* @throws Exception
*/
public function testControllerFallbackUsedWhenNoExportDownloadName(): void
{
$stream = fopen('php://memory', 'rb+');
fwrite($stream, 'data');
rewind($stream);

$storage = $this->makeEmpty(FilesystemOperator::class, [
'readStream' => $stream,
'fileSize' => 4,
]);

$job = new Job(
name: 'test-job',
steps: [new JobStep('step', 'SomeMessage', '', [])],
environmentData: [],
);

$jobRun = $this->makeEmpty(JobRun::class, [
'getJob' => $job,
]);

$service = $this->createService(
storageService: $this->makeEmpty(StorageServiceInterface::class, [
'getTempStorage' => $storage,
'tempFileExists' => true,
]),
jobRunRepository: $this->makeEmpty(JobRunRepositoryInterface::class, [
'getJobRunById' => $jobRun,
]),
);

$response = $service->downloadResourceByJobRunId(
jobRunId: 1,
tempFileName: 'download-csv-{id}.csv',
tempFolderName: 'download-csv-{id}',
mimeType: 'text/csv',
downloadName: 'export.csv',
);

$this->assertSame(
'attachment; filename="export.csv"',
$response->headers->get('Content-Disposition'),
);
}

/**
* @throws Exception
*/
Expand Down
Loading