1
|
<?php
|
2
|
|
3
|
/*
|
4
|
* This file is part of the Symfony package.
|
5
|
*
|
6
|
* (c) Fabien Potencier <fabien@symfony.com>
|
7
|
*
|
8
|
* For the full copyright and license information, please view the LICENSE
|
9
|
* file that was distributed with this source code.
|
10
|
*/
|
11
|
|
12
|
namespace Symfony\Component\HttpFoundation;
|
13
|
|
14
|
use Symfony\Component\HttpFoundation\File\File;
|
15
|
use Symfony\Component\HttpFoundation\File\Exception\FileException;
|
16
|
|
17
|
/**
|
18
|
* BinaryFileResponse represents an HTTP response delivering a file.
|
19
|
*
|
20
|
* @author Niklas Fiekas <niklas.fiekas@tu-clausthal.de>
|
21
|
* @author stealth35 <stealth35-php@live.fr>
|
22
|
* @author Igor Wiedler <igor@wiedler.ch>
|
23
|
* @author Jordan Alliot <jordan.alliot@gmail.com>
|
24
|
* @author Sergey Linnik <linniksa@gmail.com>
|
25
|
*/
|
26
|
class BinaryFileResponse extends Response
|
27
|
{
|
28
|
protected static $trustXSendfileTypeHeader = false;
|
29
|
|
30
|
/**
|
31
|
* @var File
|
32
|
*/
|
33
|
protected $file;
|
34
|
protected $offset;
|
35
|
protected $maxlen;
|
36
|
protected $deleteFileAfterSend = false;
|
37
|
|
38
|
/**
|
39
|
* Constructor.
|
40
|
*
|
41
|
* @param \SplFileInfo|string $file The file to stream
|
42
|
* @param int $status The response status code
|
43
|
* @param array $headers An array of response headers
|
44
|
* @param bool $public Files are public by default
|
45
|
* @param null|string $contentDisposition The type of Content-Disposition to set automatically with the filename
|
46
|
* @param bool $autoEtag Whether the ETag header should be automatically set
|
47
|
* @param bool $autoLastModified Whether the Last-Modified header should be automatically set
|
48
|
*/
|
49
|
public function __construct($file, $status = 200, $headers = array(), $public = true, $contentDisposition = null, $autoEtag = false, $autoLastModified = true)
|
50
|
{
|
51
|
parent::__construct(null, $status, $headers);
|
52
|
|
53
|
$this->setFile($file, $contentDisposition, $autoEtag, $autoLastModified);
|
54
|
|
55
|
if ($public) {
|
56
|
$this->setPublic();
|
57
|
}
|
58
|
}
|
59
|
|
60
|
/**
|
61
|
* @param \SplFileInfo|string $file The file to stream
|
62
|
* @param int $status The response status code
|
63
|
* @param array $headers An array of response headers
|
64
|
* @param bool $public Files are public by default
|
65
|
* @param null|string $contentDisposition The type of Content-Disposition to set automatically with the filename
|
66
|
* @param bool $autoEtag Whether the ETag header should be automatically set
|
67
|
* @param bool $autoLastModified Whether the Last-Modified header should be automatically set
|
68
|
*
|
69
|
* @return BinaryFileResponse The created response
|
70
|
*/
|
71
|
public static function create($file = null, $status = 200, $headers = array(), $public = true, $contentDisposition = null, $autoEtag = false, $autoLastModified = true)
|
72
|
{
|
73
|
return new static($file, $status, $headers, $public, $contentDisposition, $autoEtag, $autoLastModified);
|
74
|
}
|
75
|
|
76
|
/**
|
77
|
* Sets the file to stream.
|
78
|
*
|
79
|
* @param \SplFileInfo|string $file The file to stream
|
80
|
* @param string $contentDisposition
|
81
|
* @param bool $autoEtag
|
82
|
* @param bool $autoLastModified
|
83
|
*
|
84
|
* @return BinaryFileResponse
|
85
|
*
|
86
|
* @throws FileException
|
87
|
*/
|
88
|
public function setFile($file, $contentDisposition = null, $autoEtag = false, $autoLastModified = true)
|
89
|
{
|
90
|
if (!$file instanceof File) {
|
91
|
if ($file instanceof \SplFileInfo) {
|
92
|
$file = new File($file->getPathname());
|
93
|
} else {
|
94
|
$file = new File((string) $file);
|
95
|
}
|
96
|
}
|
97
|
|
98
|
if (!$file->isReadable()) {
|
99
|
throw new FileException('File must be readable.');
|
100
|
}
|
101
|
|
102
|
$this->file = $file;
|
103
|
|
104
|
if ($autoEtag) {
|
105
|
$this->setAutoEtag();
|
106
|
}
|
107
|
|
108
|
if ($autoLastModified) {
|
109
|
$this->setAutoLastModified();
|
110
|
}
|
111
|
|
112
|
if ($contentDisposition) {
|
113
|
$this->setContentDisposition($contentDisposition);
|
114
|
}
|
115
|
|
116
|
return $this;
|
117
|
}
|
118
|
|
119
|
/**
|
120
|
* Gets the file.
|
121
|
*
|
122
|
* @return File The file to stream
|
123
|
*/
|
124
|
public function getFile()
|
125
|
{
|
126
|
return $this->file;
|
127
|
}
|
128
|
|
129
|
/**
|
130
|
* Automatically sets the Last-Modified header according the file modification date.
|
131
|
*/
|
132
|
public function setAutoLastModified()
|
133
|
{
|
134
|
$this->setLastModified(\DateTime::createFromFormat('U', $this->file->getMTime()));
|
135
|
|
136
|
return $this;
|
137
|
}
|
138
|
|
139
|
/**
|
140
|
* Automatically sets the ETag header according to the checksum of the file.
|
141
|
*/
|
142
|
public function setAutoEtag()
|
143
|
{
|
144
|
$this->setEtag(sha1_file($this->file->getPathname()));
|
145
|
|
146
|
return $this;
|
147
|
}
|
148
|
|
149
|
/**
|
150
|
* Sets the Content-Disposition header with the given filename.
|
151
|
*
|
152
|
* @param string $disposition ResponseHeaderBag::DISPOSITION_INLINE or ResponseHeaderBag::DISPOSITION_ATTACHMENT
|
153
|
* @param string $filename Optionally use this filename instead of the real name of the file
|
154
|
* @param string $filenameFallback A fallback filename, containing only ASCII characters. Defaults to an automatically encoded filename
|
155
|
*
|
156
|
* @return BinaryFileResponse
|
157
|
*/
|
158
|
public function setContentDisposition($disposition, $filename = '', $filenameFallback = '')
|
159
|
{
|
160
|
if ($filename === '') {
|
161
|
$filename = $this->file->getFilename();
|
162
|
}
|
163
|
|
164
|
if ('' === $filenameFallback && (!preg_match('/^[\x20-\x7e]*$/', $filename) || false !== strpos($filename, '%'))) {
|
165
|
$encoding = mb_detect_encoding($filename, null, true);
|
166
|
|
167
|
for ($i = 0; $i < mb_strlen($filename, $encoding); ++$i) {
|
168
|
$char = mb_substr($filename, $i, 1, $encoding);
|
169
|
|
170
|
if ('%' === $char || ord($char) < 32 || ord($char) > 126) {
|
171
|
$filenameFallback .= '_';
|
172
|
} else {
|
173
|
$filenameFallback .= $char;
|
174
|
}
|
175
|
}
|
176
|
}
|
177
|
|
178
|
$dispositionHeader = $this->headers->makeDisposition($disposition, $filename, $filenameFallback);
|
179
|
$this->headers->set('Content-Disposition', $dispositionHeader);
|
180
|
|
181
|
return $this;
|
182
|
}
|
183
|
|
184
|
/**
|
185
|
* {@inheritdoc}
|
186
|
*/
|
187
|
public function prepare(Request $request)
|
188
|
{
|
189
|
$this->headers->set('Content-Length', $this->file->getSize());
|
190
|
|
191
|
if (!$this->headers->has('Accept-Ranges')) {
|
192
|
// Only accept ranges on safe HTTP methods
|
193
|
$this->headers->set('Accept-Ranges', $request->isMethodSafe() ? 'bytes' : 'none');
|
194
|
}
|
195
|
|
196
|
if (!$this->headers->has('Content-Type')) {
|
197
|
$this->headers->set('Content-Type', $this->file->getMimeType() ?: 'application/octet-stream');
|
198
|
}
|
199
|
|
200
|
if ('HTTP/1.0' !== $request->server->get('SERVER_PROTOCOL')) {
|
201
|
$this->setProtocolVersion('1.1');
|
202
|
}
|
203
|
|
204
|
$this->ensureIEOverSSLCompatibility($request);
|
205
|
|
206
|
$this->offset = 0;
|
207
|
$this->maxlen = -1;
|
208
|
|
209
|
if (self::$trustXSendfileTypeHeader && $request->headers->has('X-Sendfile-Type')) {
|
210
|
// Use X-Sendfile, do not send any content.
|
211
|
$type = $request->headers->get('X-Sendfile-Type');
|
212
|
$path = $this->file->getRealPath();
|
213
|
// Fall back to scheme://path for stream wrapped locations.
|
214
|
if (false === $path) {
|
215
|
$path = $this->file->getPathname();
|
216
|
}
|
217
|
if (strtolower($type) === 'x-accel-redirect') {
|
218
|
// Do X-Accel-Mapping substitutions.
|
219
|
// @link http://wiki.nginx.org/X-accel#X-Accel-Redirect
|
220
|
foreach (explode(',', $request->headers->get('X-Accel-Mapping', '')) as $mapping) {
|
221
|
$mapping = explode('=', $mapping, 2);
|
222
|
|
223
|
if (2 === count($mapping)) {
|
224
|
$pathPrefix = trim($mapping[0]);
|
225
|
$location = trim($mapping[1]);
|
226
|
|
227
|
if (substr($path, 0, strlen($pathPrefix)) === $pathPrefix) {
|
228
|
$path = $location.substr($path, strlen($pathPrefix));
|
229
|
break;
|
230
|
}
|
231
|
}
|
232
|
}
|
233
|
}
|
234
|
$this->headers->set($type, $path);
|
235
|
$this->maxlen = 0;
|
236
|
} elseif ($request->headers->has('Range')) {
|
237
|
// Process the range headers.
|
238
|
if (!$request->headers->has('If-Range') || $this->hasValidIfRangeHeader($request->headers->get('If-Range'))) {
|
239
|
$range = $request->headers->get('Range');
|
240
|
$fileSize = $this->file->getSize();
|
241
|
|
242
|
list($start, $end) = explode('-', substr($range, 6), 2) + array(0);
|
243
|
|
244
|
$end = ('' === $end) ? $fileSize - 1 : (int) $end;
|
245
|
|
246
|
if ('' === $start) {
|
247
|
$start = $fileSize - $end;
|
248
|
$end = $fileSize - 1;
|
249
|
} else {
|
250
|
$start = (int) $start;
|
251
|
}
|
252
|
|
253
|
if ($start <= $end) {
|
254
|
if ($start < 0 || $end > $fileSize - 1) {
|
255
|
$this->setStatusCode(416);
|
256
|
$this->headers->set('Content-Range', sprintf('bytes */%s', $fileSize));
|
257
|
} elseif ($start !== 0 || $end !== $fileSize - 1) {
|
258
|
$this->maxlen = $end < $fileSize ? $end - $start + 1 : -1;
|
259
|
$this->offset = $start;
|
260
|
|
261
|
$this->setStatusCode(206);
|
262
|
$this->headers->set('Content-Range', sprintf('bytes %s-%s/%s', $start, $end, $fileSize));
|
263
|
$this->headers->set('Content-Length', $end - $start + 1);
|
264
|
}
|
265
|
}
|
266
|
}
|
267
|
}
|
268
|
|
269
|
return $this;
|
270
|
}
|
271
|
|
272
|
private function hasValidIfRangeHeader($header)
|
273
|
{
|
274
|
if ($this->getEtag() === $header) {
|
275
|
return true;
|
276
|
}
|
277
|
|
278
|
if (null === $lastModified = $this->getLastModified()) {
|
279
|
return false;
|
280
|
}
|
281
|
|
282
|
return $lastModified->format('D, d M Y H:i:s').' GMT' === $header;
|
283
|
}
|
284
|
|
285
|
/**
|
286
|
* Sends the file.
|
287
|
*
|
288
|
* {@inheritdoc}
|
289
|
*/
|
290
|
public function sendContent()
|
291
|
{
|
292
|
if (!$this->isSuccessful()) {
|
293
|
return parent::sendContent();
|
294
|
}
|
295
|
|
296
|
if (0 === $this->maxlen) {
|
297
|
return $this;
|
298
|
}
|
299
|
|
300
|
$out = fopen('php://output', 'wb');
|
301
|
$file = fopen($this->file->getPathname(), 'rb');
|
302
|
|
303
|
stream_copy_to_stream($file, $out, $this->maxlen, $this->offset);
|
304
|
|
305
|
fclose($out);
|
306
|
fclose($file);
|
307
|
|
308
|
if ($this->deleteFileAfterSend) {
|
309
|
unlink($this->file->getPathname());
|
310
|
}
|
311
|
|
312
|
return $this;
|
313
|
}
|
314
|
|
315
|
/**
|
316
|
* {@inheritdoc}
|
317
|
*
|
318
|
* @throws \LogicException when the content is not null
|
319
|
*/
|
320
|
public function setContent($content)
|
321
|
{
|
322
|
if (null !== $content) {
|
323
|
throw new \LogicException('The content cannot be set on a BinaryFileResponse instance.');
|
324
|
}
|
325
|
}
|
326
|
|
327
|
/**
|
328
|
* {@inheritdoc}
|
329
|
*
|
330
|
* @return false
|
331
|
*/
|
332
|
public function getContent()
|
333
|
{
|
334
|
return false;
|
335
|
}
|
336
|
|
337
|
/**
|
338
|
* Trust X-Sendfile-Type header.
|
339
|
*/
|
340
|
public static function trustXSendfileTypeHeader()
|
341
|
{
|
342
|
self::$trustXSendfileTypeHeader = true;
|
343
|
}
|
344
|
|
345
|
/**
|
346
|
* If this is set to true, the file will be unlinked after the request is send
|
347
|
* Note: If the X-Sendfile header is used, the deleteFileAfterSend setting will not be used.
|
348
|
*
|
349
|
* @param bool $shouldDelete
|
350
|
*
|
351
|
* @return BinaryFileResponse
|
352
|
*/
|
353
|
public function deleteFileAfterSend($shouldDelete)
|
354
|
{
|
355
|
$this->deleteFileAfterSend = $shouldDelete;
|
356
|
|
357
|
return $this;
|
358
|
}
|
359
|
}
|