-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDownloadService.php
More file actions
150 lines (137 loc) · 5.18 KB
/
DownloadService.php
File metadata and controls
150 lines (137 loc) · 5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
declare(strict_types=1);
/**
* This source file is available under the terms of the
* Pimcore Open Core License (POCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (https://www.pimcore.com)
* @license Pimcore Open Core License (POCL)
*/
namespace Pimcore\Bundle\StudioBackendBundle\Export\Service;
use League\Flysystem\FilesystemException;
use League\Flysystem\FilesystemOperator;
use Pimcore\Bundle\StudioBackendBundle\Element\Service\StorageServiceInterface;
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\ExecutionEngine\Service\ExecutionEngineServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\Asset\MimeTypes;
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;
/**
* @internal
*/
final readonly class DownloadService implements DownloadServiceInterface
{
use StreamedResponseTrait;
use TempFilePathTrait;
public function __construct(
private ExecutionEngineServiceInterface $executionEngineService,
private LoggerInterface $logger,
private StorageServiceInterface $storageService,
) {
}
/**
* @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;
$storage = $this->validateStorage($filePath, $jobRunId);
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,
]
);
}
},
);
}
/**
* @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
);
$this->executionEngineService->hideJobRun($jobRunId);
} catch (FilesystemException $e) {
throw new EnvironmentException(
sprintf(
'Failed to delete file based on jobRunId %d: %s',
$jobRunId,
$e->getMessage()
),
);
}
}
public function downloadJSON(string $json, string $filename): Response
{
$response = new Response($json);
$response->headers->set(
HttpResponseHeaders::HEADER_CONTENT_TYPE->value,
MimeTypes::JSON->value
);
$response->headers->set(
HttpResponseHeaders::HEADER_CONTENT_DISPOSITION->value,
'attachment; filename="' . $filename .'"'
);
return $response;
}
/**
* @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;
}
}