-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathStreamedResponseTrait.php
More file actions
170 lines (154 loc) · 5.45 KB
/
StreamedResponseTrait.php
File metadata and controls
170 lines (154 loc) · 5.45 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?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\Util\Trait;
use League\Flysystem\FilesystemException;
use League\Flysystem\FilesystemOperator;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ElementStreamResourceNotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\StreamResourceNotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\HttpResponseCodes;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\HttpResponseHeaders;
use Pimcore\Model\Asset;
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;
/**
* @internal
*/
trait StreamedResponseTrait
{
/**
* @throws ElementStreamResourceNotFoundException
*/
protected function getStreamedResponse(
Asset|VideoImageThumbnailInterface|ImageThumbnailInterface $element,
string $contentDisposition = HttpResponseHeaders::ATTACHMENT_TYPE->value,
array $additionalHeaders = [],
?int $fileSize = null,
): StreamedResponse {
$stream = $element->getStream();
$asset = $this->getAsset($element);
if (!is_resource($stream)) {
throw new ElementStreamResourceNotFoundException(
$asset->getId(),
$asset->getType()
);
}
if (!$fileSize) {
$fileSize = $element->getFileSize();
}
return new StreamedResponse(
function () use ($stream) {
fpassthru($stream);
},
HttpResponseCodes::SUCCESS->value,
$this->getResponseHeaders(
mimeType: $element->getMimeType(),
fileSize: $fileSize,
filename: $asset->getFilename(),
contentDisposition: $contentDisposition,
additionalHeaders: $additionalHeaders
)
);
}
/**
* @throws FilesystemException
*/
protected function getVideoStreamedResponse(
Video $video,
FilesystemOperator $storage,
string $storagePath,
string $contentDisposition = HttpResponseHeaders::ATTACHMENT_TYPE->value,
): StreamedResponse {
$stream = $storage->readStream($storagePath);
return new StreamedResponse(
function () use ($stream) {
fpassthru($stream);
},
HttpResponseCodes::SUCCESS->value,
$this->getResponseHeaders(
mimeType: 'video/mp4',
fileSize: $storage->fileSize($storagePath),
filename: $video->getFilename(),
contentDisposition: $contentDisposition,
additionalHeaders: [
HttpResponseHeaders::HEADER_ACCEPT_RANGES->value => 'bytes',
]
)
);
}
/**
* @throws StreamResourceNotFoundException
*/
protected function getFileStreamedResponse(
string $path,
string $mimeType,
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, $onStreamComplete) {
fpassthru($stream);
if ($onStreamComplete !== null) {
$onStreamComplete();
}
},
HttpResponseCodes::SUCCESS->value,
$this->getResponseHeaders(
mimeType: $mimeType,
fileSize: $storage->fileSize($path),
filename: $filename,
contentDisposition: $contentDisposition
),
);
} catch (FilesystemException $e) {
throw new StreamResourceNotFoundException(
sprintf(
'Could not process stream for file %s: %s',
$path,
$e->getMessage()
)
);
}
}
private function getAsset(Asset|VideoImageThumbnailInterface|ImageThumbnailInterface $element): Asset
{
if ($element instanceof Asset) {
return $element;
}
return $element->getAsset();
}
private function getResponseHeaders(
string $mimeType,
int $fileSize,
string $filename,
string $contentDisposition,
array $additionalHeaders = []
): array {
return array_merge($additionalHeaders, [
HttpResponseHeaders::HEADER_CONTENT_TYPE->value => $mimeType,
HttpResponseHeaders::HEADER_CONTENT_DISPOSITION->value => sprintf(
'%s; filename="%s"',
$contentDisposition,
$filename
),
HttpResponseHeaders::HEADER_CONTENT_LENGTH->value => $fileSize,
]);
}
}