Skip to content
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
44 changes: 23 additions & 21 deletions src/Export/Service/DownloadService.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\HttpResponseHeaders;
use Pimcore\Bundle\StudioBackendBundle\Util\Trait\StreamedResponseTrait;
use Pimcore\Bundle\StudioBackendBundle\Util\Trait\TempFilePathTrait;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
use function sprintf;
Expand All @@ -39,7 +40,8 @@

public function __construct(
private ExecutionEngineServiceInterface $executionEngineService,
private StorageServiceInterface $storageService
private LoggerInterface $logger,
private StorageServiceInterface $storageService,
) {
}

Expand All @@ -59,27 +61,27 @@ public function downloadResourceByJobRunId(
$filePath = $folderName . '/' . $fileName;
$storage = $this->validateStorage($filePath, $jobRunId);

$streamedResponse = $this->getFileStreamedResponse(
$filePath,
$mimeType,
$downloadName,
$storage
return $this->getFileStreamedResponse(
path: $filePath,
mimeType: $mimeType,
filename: $downloadName,
storage: $storage,
onStreamComplete: function () use ($storage, $filePath, $folderName, $jobRunId): void {
try {
$storage->delete($filePath);
$this->storageService->cleanUpFolder($folderName);
$this->executionEngineService->hideJobRun($jobRunId);
} catch (FilesystemException $e) {
$this->logger->error(
'Failed to clean up temporary folder {folder}',
[
'folder' => $folderName,
'exception' => $e,
]
);
}
},
);

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

return $streamedResponse;
}

/**
Expand Down
23 changes: 19 additions & 4 deletions src/Util/Trait/StreamedResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Pimcore\Model\Asset\Thumbnail\ImageThumbnailInterface;
use Pimcore\Model\Asset\Video;
use Pimcore\Model\Asset\Video\ImageThumbnailInterface as VideoImageThumbnailInterface;
use Closure;
use Symfony\Component\HttpFoundation\StreamedResponse;
use function is_resource;
use function sprintf;
Expand All @@ -42,11 +43,12 @@ protected function getStreamedResponse(
?int $fileSize = null,
): StreamedResponse {
$stream = $element->getStream();
$asset = $this->getAsset($element);

if (!is_resource($stream)) {
throw new ElementStreamResourceNotFoundException(
$element->getId(),
$element->getType()
$asset->getId(),
$asset->getType()
);
}

Expand All @@ -62,7 +64,7 @@ function () use ($stream) {
$this->getResponseHeaders(
mimeType: $element->getMimeType(),
fileSize: $fileSize,
filename: $element->getFilename(),
filename: $asset->getFilename(),
contentDisposition: $contentDisposition,
additionalHeaders: $additionalHeaders
)
Expand Down Expand Up @@ -106,13 +108,17 @@ protected function getFileStreamedResponse(
string $filename,
FilesystemOperator $storage,
string $contentDisposition = HttpResponseHeaders::ATTACHMENT_TYPE->value,
?Closure $onStreamComplete = null,
): StreamedResponse {
try {
$stream = $storage->readStream($path);

return new StreamedResponse(
function () use ($stream) {
function () use ($stream, $onStreamComplete) {
fpassthru($stream);
if ($onStreamComplete !== null) {
$onStreamComplete();
}
},
HttpResponseCodes::SUCCESS->value,
$this->getResponseHeaders(
Expand All @@ -135,6 +141,15 @@ function () use ($stream) {

}

private function getAsset(Asset|VideoImageThumbnailInterface|ImageThumbnailInterface $element): Asset
{
if ($element instanceof Asset) {
return $element;
}

return $element->getAsset();
}

private function getResponseHeaders(
string $mimeType,
int $fileSize,
Expand Down
Loading
Loading