comparison vendor/symfony/http-foundation/BinaryFileResponse.php @ 4:a9cd425dd02b

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:11:55 +0000
parents c75dbcec494b
children
comparison
equal deleted inserted replaced
3:307d7a7fd348 4:a9cd425dd02b
9 * file that was distributed with this source code. 9 * file that was distributed with this source code.
10 */ 10 */
11 11
12 namespace Symfony\Component\HttpFoundation; 12 namespace Symfony\Component\HttpFoundation;
13 13
14 use Symfony\Component\HttpFoundation\File\Exception\FileException;
14 use Symfony\Component\HttpFoundation\File\File; 15 use Symfony\Component\HttpFoundation\File\File;
15 use Symfony\Component\HttpFoundation\File\Exception\FileException;
16 16
17 /** 17 /**
18 * BinaryFileResponse represents an HTTP response delivering a file. 18 * BinaryFileResponse represents an HTTP response delivering a file.
19 * 19 *
20 * @author Niklas Fiekas <niklas.fiekas@tu-clausthal.de> 20 * @author Niklas Fiekas <niklas.fiekas@tu-clausthal.de>
29 29
30 /** 30 /**
31 * @var File 31 * @var File
32 */ 32 */
33 protected $file; 33 protected $file;
34 protected $offset; 34 protected $offset = 0;
35 protected $maxlen; 35 protected $maxlen = -1;
36 protected $deleteFileAfterSend = false; 36 protected $deleteFileAfterSend = false;
37 37
38 /** 38 /**
39 * @param \SplFileInfo|string $file The file to stream 39 * @param \SplFileInfo|string $file The file to stream
40 * @param int $status The response status code 40 * @param int $status The response status code
41 * @param array $headers An array of response headers 41 * @param array $headers An array of response headers
42 * @param bool $public Files are public by default 42 * @param bool $public Files are public by default
43 * @param null|string $contentDisposition The type of Content-Disposition to set automatically with the filename 43 * @param string|null $contentDisposition The type of Content-Disposition to set automatically with the filename
44 * @param bool $autoEtag Whether the ETag header should be automatically set 44 * @param bool $autoEtag Whether the ETag header should be automatically set
45 * @param bool $autoLastModified Whether the Last-Modified header should be automatically set 45 * @param bool $autoLastModified Whether the Last-Modified header should be automatically set
46 */ 46 */
47 public function __construct($file, $status = 200, $headers = array(), $public = true, $contentDisposition = null, $autoEtag = false, $autoLastModified = true) 47 public function __construct($file, $status = 200, $headers = [], $public = true, $contentDisposition = null, $autoEtag = false, $autoLastModified = true)
48 { 48 {
49 parent::__construct(null, $status, $headers); 49 parent::__construct(null, $status, $headers);
50 50
51 $this->setFile($file, $contentDisposition, $autoEtag, $autoLastModified); 51 $this->setFile($file, $contentDisposition, $autoEtag, $autoLastModified);
52 52
58 /** 58 /**
59 * @param \SplFileInfo|string $file The file to stream 59 * @param \SplFileInfo|string $file The file to stream
60 * @param int $status The response status code 60 * @param int $status The response status code
61 * @param array $headers An array of response headers 61 * @param array $headers An array of response headers
62 * @param bool $public Files are public by default 62 * @param bool $public Files are public by default
63 * @param null|string $contentDisposition The type of Content-Disposition to set automatically with the filename 63 * @param string|null $contentDisposition The type of Content-Disposition to set automatically with the filename
64 * @param bool $autoEtag Whether the ETag header should be automatically set 64 * @param bool $autoEtag Whether the ETag header should be automatically set
65 * @param bool $autoLastModified Whether the Last-Modified header should be automatically set 65 * @param bool $autoLastModified Whether the Last-Modified header should be automatically set
66 * 66 *
67 * @return static 67 * @return static
68 */ 68 */
69 public static function create($file = null, $status = 200, $headers = array(), $public = true, $contentDisposition = null, $autoEtag = false, $autoLastModified = true) 69 public static function create($file = null, $status = 200, $headers = [], $public = true, $contentDisposition = null, $autoEtag = false, $autoLastModified = true)
70 { 70 {
71 return new static($file, $status, $headers, $public, $contentDisposition, $autoEtag, $autoLastModified); 71 return new static($file, $status, $headers, $public, $contentDisposition, $autoEtag, $autoLastModified);
72 } 72 }
73 73
74 /** 74 /**
163 $encoding = mb_detect_encoding($filename, null, true) ?: '8bit'; 163 $encoding = mb_detect_encoding($filename, null, true) ?: '8bit';
164 164
165 for ($i = 0, $filenameLength = mb_strlen($filename, $encoding); $i < $filenameLength; ++$i) { 165 for ($i = 0, $filenameLength = mb_strlen($filename, $encoding); $i < $filenameLength; ++$i) {
166 $char = mb_substr($filename, $i, 1, $encoding); 166 $char = mb_substr($filename, $i, 1, $encoding);
167 167
168 if ('%' === $char || ord($char) < 32 || ord($char) > 126) { 168 if ('%' === $char || \ord($char) < 32 || \ord($char) > 126) {
169 $filenameFallback .= '_'; 169 $filenameFallback .= '_';
170 } else { 170 } else {
171 $filenameFallback .= $char; 171 $filenameFallback .= $char;
172 } 172 }
173 } 173 }
219 // Do X-Accel-Mapping substitutions. 219 // Do X-Accel-Mapping substitutions.
220 // @link http://wiki.nginx.org/X-accel#X-Accel-Redirect 220 // @link http://wiki.nginx.org/X-accel#X-Accel-Redirect
221 foreach (explode(',', $request->headers->get('X-Accel-Mapping', '')) as $mapping) { 221 foreach (explode(',', $request->headers->get('X-Accel-Mapping', '')) as $mapping) {
222 $mapping = explode('=', $mapping, 2); 222 $mapping = explode('=', $mapping, 2);
223 223
224 if (2 === count($mapping)) { 224 if (2 === \count($mapping)) {
225 $pathPrefix = trim($mapping[0]); 225 $pathPrefix = trim($mapping[0]);
226 $location = trim($mapping[1]); 226 $location = trim($mapping[1]);
227 227
228 if (substr($path, 0, strlen($pathPrefix)) === $pathPrefix) { 228 if (substr($path, 0, \strlen($pathPrefix)) === $pathPrefix) {
229 $path = $location.substr($path, strlen($pathPrefix)); 229 $path = $location.substr($path, \strlen($pathPrefix));
230 break; 230 break;
231 } 231 }
232 } 232 }
233 } 233 }
234 } 234 }
237 } elseif ($request->headers->has('Range')) { 237 } elseif ($request->headers->has('Range')) {
238 // Process the range headers. 238 // Process the range headers.
239 if (!$request->headers->has('If-Range') || $this->hasValidIfRangeHeader($request->headers->get('If-Range'))) { 239 if (!$request->headers->has('If-Range') || $this->hasValidIfRangeHeader($request->headers->get('If-Range'))) {
240 $range = $request->headers->get('Range'); 240 $range = $request->headers->get('Range');
241 241
242 list($start, $end) = explode('-', substr($range, 6), 2) + array(0); 242 list($start, $end) = explode('-', substr($range, 6), 2) + [0];
243 243
244 $end = ('' === $end) ? $fileSize - 1 : (int) $end; 244 $end = ('' === $end) ? $fileSize - 1 : (int) $end;
245 245
246 if ('' === $start) { 246 if ('' === $start) {
247 $start = $fileSize - $end; 247 $start = $fileSize - $end;
303 stream_copy_to_stream($file, $out, $this->maxlen, $this->offset); 303 stream_copy_to_stream($file, $out, $this->maxlen, $this->offset);
304 304
305 fclose($out); 305 fclose($out);
306 fclose($file); 306 fclose($file);
307 307
308 if ($this->deleteFileAfterSend) { 308 if ($this->deleteFileAfterSend && file_exists($this->file->getPathname())) {
309 unlink($this->file->getPathname()); 309 unlink($this->file->getPathname());
310 } 310 }
311 311
312 return $this; 312 return $this;
313 } 313 }