Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

BinaryStreamResponse.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Bundle\EzPublishIOBundle;
8
9 use DateTime;
10 use eZ\Publish\Core\IO\IOServiceInterface;
11 use eZ\Publish\Core\IO\Values\BinaryFile;
12 use LogicException;
13 use Symfony\Component\HttpFoundation\BinaryFileResponse;
14 use Symfony\Component\HttpFoundation\Request;
15 use Symfony\Component\HttpFoundation\Response;
16
20 class BinaryStreamResponse extends Response
21 {
22 protected static $trustXSendfileTypeHeader = false;
23
25 protected $file;
26
28 protected $ioService;
29
30 protected $offset;
31
32 protected $maxlen;
33
46 public function __construct(BinaryFile $binaryFile, IOServiceInterface $ioService, $status = 200, $headers = [], $public = true, $contentDisposition = null, $autoLastModified = true)
47 {
48 $this->ioService = $ioService;
49
50 parent::__construct(null, $status, $headers);
51
52 $this->setFile($binaryFile, $contentDisposition, $autoLastModified);
53
54 if ($public) {
55 $this->setPublic();
56 }
57 }
58
69 public function setFile($file, $contentDisposition = null, $autoLastModified = true)
70 {
71 $this->file = $file;
72
73 if ($autoLastModified) {
74 $this->setAutoLastModified();
75 }
76
77 if ($contentDisposition) {
78 $this->setContentDisposition($contentDisposition);
79 }
80
81 return $this;
82 }
83
89 public function getFile()
90 {
91 return $this->file;
92 }
93
97 public function setAutoLastModified()
98 {
99 $this->setLastModified(DateTime::createFromFormat('U', $this->file->mtime->getTimestamp()));
100
101 return $this;
102 }
103
113 public function setContentDisposition($disposition, $filename = '', $filenameFallback = '')
114 {
115 if ($filename === '') {
116 $filename = $this->file->id;
117 }
118
119 if (empty($filenameFallback)) {
120 $filenameFallback = mb_convert_encoding($filename, 'ASCII');
121 }
122
123 $dispositionHeader = $this->headers->makeDisposition($disposition, $filename, $filenameFallback);
124 $this->headers->set('Content-Disposition', $dispositionHeader);
125
126 return $this;
127 }
128
132 public function prepare(Request $request)
133 {
134 $this->headers->set('Content-Length', $this->file->size);
135 $this->headers->set('Accept-Ranges', 'bytes');
136 $this->headers->set('Content-Transfer-Encoding', 'binary');
137
138 if (!$this->headers->has('Content-Type')) {
139 $this->headers->set(
140 'Content-Type',
141 $this->ioService->getMimeType($this->file->id) ?: 'application/octet-stream'
142 );
143 }
144
145 if ('HTTP/1.0' != $request->server->get('SERVER_PROTOCOL')) {
146 $this->setProtocolVersion('1.1');
147 }
148
149 $this->ensureIEOverSSLCompatibility($request);
150
151 $this->offset = 0;
152 $this->maxlen = -1;
153
154 if ($request->headers->has('Range')) {
155 // Process the range headers.
156 if (!$request->headers->has('If-Range') || $this->getEtag() == $request->headers->get('If-Range')) {
157 $range = $request->headers->get('Range');
158 $fileSize = $this->file->size;
159
160 list($start, $end) = explode('-', substr($range, 6), 2) + [0];
161
162 $end = ('' === $end) ? $fileSize - 1 : (int)$end;
163
164 if ('' === $start) {
165 $start = $fileSize - $end;
166 $end = $fileSize - 1;
167 } else {
168 $start = (int)$start;
169 }
170
171 if ($start <= $end) {
172 if ($start < 0 || $end > $fileSize - 1) {
173 $this->setStatusCode(416); // HTTP_REQUESTED_RANGE_NOT_SATISFIABLE
174 } elseif ($start !== 0 || $end !== $fileSize - 1) {
175 $this->maxlen = $end < $fileSize ? $end - $start + 1 : -1;
176 $this->offset = $start;
177
178 $this->setStatusCode(206); // HTTP_PARTIAL_CONTENT
179 $this->headers->set('Content-Range', sprintf('bytes %s-%s/%s', $start, $end, $fileSize));
180 $this->headers->set('Content-Length', $end - $start + 1);
181 }
182 }
183 }
184 }
185
186 return $this;
187 }
188
192 public function sendContent()
193 {
194 if (!$this->isSuccessful()) {
195 parent::sendContent();
196
197 return;
198 }
199
200 if (0 === $this->maxlen) {
201 return;
202 }
203
204 $out = fopen('php://output', 'wb');
205 $in = $this->ioService->getFileInputStream($this->file);
206 stream_copy_to_stream($in, $out, $this->maxlen, $this->offset);
207
208 fclose($out);
209 }
210
216 public function setContent($content)
217 {
218 if (null !== $content) {
219 throw new LogicException('The content cannot be set on a BinaryStreamResponse instance.');
220 }
221 }
222
223 public function getContent()
224 {
225 return null;
226 }
227 }
eZ\Bundle\EzPublishIOBundle\BinaryStreamResponse
A modified version of HttpFoundation's BinaryFileResponse that accepts a stream as the input.
Definition: BinaryStreamResponse.php:21
eZ\Bundle\EzPublishIOBundle\BinaryStreamResponse\$trustXSendfileTypeHeader
static $trustXSendfileTypeHeader
Definition: BinaryStreamResponse.php:22
eZ\Bundle\EzPublishIOBundle\BinaryStreamResponse\prepare
prepare(Request $request)
{}
Definition: BinaryStreamResponse.php:132
eZ\Bundle\EzPublishIOBundle\BinaryStreamResponse\$file
$file
Definition: BinaryStreamResponse.php:25
eZ\Bundle\EzPublishIOBundle\BinaryStreamResponse\getContent
getContent()
Definition: BinaryStreamResponse.php:223
eZ\Bundle\EzPublishIOBundle\BinaryStreamResponse\sendContent
sendContent()
Sends the file.
Definition: BinaryStreamResponse.php:192
eZ\Bundle\EzPublishIOBundle\BinaryStreamResponse\setContent
setContent($content)
{}
Definition: BinaryStreamResponse.php:216
eZ\Bundle\EzPublishIOBundle\BinaryStreamResponse\$offset
$offset
Definition: BinaryStreamResponse.php:30
eZ\Bundle\EzPublishIOBundle\BinaryStreamResponse\setAutoLastModified
setAutoLastModified()
Automatically sets the Last-Modified header according the file modification date.
Definition: BinaryStreamResponse.php:97
eZ\Bundle\EzPublishIOBundle\BinaryStreamResponse\__construct
__construct(BinaryFile $binaryFile, IOServiceInterface $ioService, $status=200, $headers=[], $public=true, $contentDisposition=null, $autoLastModified=true)
Constructor.
Definition: BinaryStreamResponse.php:46
eZ\Bundle\EzPublishIOBundle\BinaryStreamResponse\getFile
getFile()
Gets the file.
Definition: BinaryStreamResponse.php:89
eZ\Bundle\EzPublishIOBundle\BinaryStreamResponse\$maxlen
$maxlen
Definition: BinaryStreamResponse.php:32
eZ\Bundle\EzPublishIOBundle\BinaryStreamResponse\$ioService
$ioService
Definition: BinaryStreamResponse.php:28
eZ\Bundle\EzPublishIOBundle\BinaryStreamResponse\setContentDisposition
setContentDisposition($disposition, $filename='', $filenameFallback='')
Sets the Content-Disposition header with the given filename.
Definition: BinaryStreamResponse.php:113
eZ\Bundle\EzPublishIOBundle\BinaryStreamResponse\setFile
setFile($file, $contentDisposition=null, $autoLastModified=true)
Sets the file to stream.
Definition: BinaryStreamResponse.php:69
eZ\Publish\Core\IO\Values\BinaryFile
Definition: Core/IO/Values/BinaryFile.php:22
eZ\Publish\Core\IO\IOServiceInterface
Interface for Input/Output handling of binary files.
Definition: IOServiceInterface.php:16
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.