public
Last active

BinaryFileResponse with X-SendFile and Date Range

  • Download Gist
BinaryFileResponse.php
PHP
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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
<?php
 
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
 
namespace Symfony\Component\HttpFoundation;
 
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
 
/**
* BinaryFileResponse represents an HTTP response representing a binary file.
*
* @author Jordan Alliot <jordan.alliot@gmail.com>
* @author stealth35
*/
class BinaryFileResponse extends Response
{
private $file;
private $offset;
private $maxlen;
 
/**
* Creates a BinaryFileResponse.
*
* @param SplFileInfo|string $file The file to download
* @param integer $status The response status code
* @param array $headers An array of response headers
* @param boolean $autoValidation Whether ETag and Last-Modified headers
* are automatically set or not
* @param boolean $public Files are public by default
*/
public function __construct($file, $status = 200, $headers = array(), $autoValidation = true, $public = true)
{
parent::__construct('', $status, $headers);
 
$this->setFile($file);
 
$this->setProtocolVersion('1.1');
$this->headers->set('Content-Transfer-Encoding', 'binary');
 
if (true === $autoValidation) {
$this->setAutoLastModified();
$this->setAutoEtag();
}
 
if (true === $public) {
$this->setPublic();
}
 
$request = Request::createFromGlobals();
 
if ($request->headers->has('x-sendfile-type')) {
$this->processSendFile($request);
} else if ($request->headers->has('range')) {
$this->processRange($request);
}
}
 
/**
* Sets the file to download.
*
* Also sets some headers for the download, namely
* Content-Disposition, Content-Length and Content-Type.
*
* @param SplFileInfo|string $file The file to download
*/
public function setFile($file)
{
if (null === $file) {
throw new \InvalidArgumentException('File cannot be null.');
}
 
$file = new File((string) $file, true);
 
if (!$file->isReadable()) {
throw new FileException('File must be readable.');
}
 
$this->file = $file;
 
$this->makeDisposition();
$this->headers->set('Content-Length', $file->getSize());
$this->headers->set('Content-Type', $file->getMimeType() ?: 'application/octet-stream');
$this->headers->set('Accept-Ranges', 'bytes');
}
 
/**
* Returns the file.
*
* @return File
*/
public function getFile()
{
return $this->file;
}
 
/**
* Sets the Content-Disposition header.
*
* @param string $disposition Either "inline" or "attachment" (default)
* @param string $filename Name of the file (by default the original file name)
* May be unicode
* @param string $filenameFallback A string containing only ASCII characters that
* is semantically equivalent to $filename. If the filename is already ASCII,
* it can be omitted, or just copied from $filename
*/
public function makeDisposition($disposition = null, $filename = '', $filenameFallback = '')
{
$this->headers->set('Content-Disposition', $this->headers->makeDisposition(
$disposition ?: ResponseHeaderBag::DISPOSITION_ATTACHMENT,
$filename ?: $this->file->getBasename(),
$filenameFallback ?: rawurlencode($this->file->getBasename())
));
}
 
/**
* Automatically sets the Last-Modified header according to the file modification date.
*/
public function setAutoLastModified()
{
$this->setLastModified(\DateTime::createFromFormat('U', $this->file->getMTime()));
}
 
/**
* Automatically sets the ETag header according to the checksum of the file.
*/
public function setAutoEtag()
{
$this->setEtag(sha1_file($this->file->getPathname()));
}
 
/**
* Sends the file.
*/
public function sendContent()
{
if (!$this->isSuccessful()) {
parent::sendContent();
return;
}
 
$offset = (null !== $this->offset) ? $this->offset : 0;
$maxlen = (null !== $this->maxlen) ? $this->maxlen : -1;
 
$out = fopen('php://output', 'wb');
$file = fopen($this->file->getPathname(), 'rb');
 
stream_copy_to_stream($file, $out, $maxlen, $offset);
 
fclose($out);
fclose($file);
}
 
private function processSendFile(Request $request)
{
$sendfiletype = $request->headers->get('x-sendfile-type');
 
$this->headers->set($sendfiletype, $this->file->getPathname());
$this->maxlen = 0;
}
 
private function processRange(Request $request)
{
if ($request->headers->has('if-range') && ($this->getEtag() !== $request->headers->get('if-range'))) {
return;
}
 
$range = $request->headers->get('range');
 
list($start, $end) = explode('-', substr($range, 6));
 
if ('' !== $end) {
$this->maxlen = $end - $start;
} else {
$end = $this->file->getSize() - 1;
}
 
$this->offset = $start;
 
$this->setStatusCode(206);
$this->headers->set('Content-Range', sprintf('bytes %s-%s/%s', $start, $end, $this->file->getSize()));
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.