PageRenderTime 63ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/concrete/src/Http/FlysystemFileResponse.php

https://gitlab.com/koodersmiikka/operaatio-terveys
PHP | 260 lines | 134 code | 44 blank | 82 comment | 24 complexity | 68a7c6112f80f5e351b998fe2e7d05a5 MD5 | raw file
  1. <?php
  2. namespace Concrete\Core\Http;
  3. use Concrete\Flysystem\File;
  4. use Concrete\Flysystem\FilesystemInterface;
  5. use Symfony\Component\HttpFoundation\File\Exception\FileException;
  6. /**
  7. * Heavily based on BinaryFileResponse represents an HTTP response delivering a file.
  8. *
  9. * @author Korvin Szanto <korvinszanto@gmail.com>
  10. * @author Niklas Fiekas <niklas.fiekas@tu-clausthal.de>
  11. * @author stealth35 <stealth35-php@live.fr>
  12. * @author Igor Wiedler <igor@wiedler.ch>
  13. * @author Jordan Alliot <jordan.alliot@gmail.com>
  14. * @author Sergey Linnik <linniksa@gmail.com>
  15. */
  16. class FlysystemFileResponse extends Response
  17. {
  18. protected static $trustXSendfileTypeHeader = false;
  19. /** @type File */
  20. protected $file;
  21. /** @type FilesystemInterface */
  22. protected $filesystem;
  23. protected $offset;
  24. protected $maxlen;
  25. /**
  26. * Constructor.
  27. *
  28. * @param File $file The file to stream
  29. * @param FilesystemInterface $filesystem The filesystem instance to get info with
  30. * @param int $status The response status code
  31. * @param array $headers An array of response headers
  32. * @param bool $public Files are public by default
  33. * @param null|string $contentDisposition The type of Content-Disposition to set automatically with the filename
  34. * @param bool $autoEtag Whether the ETag header should be automatically set
  35. */
  36. public function __construct($file, FilesystemInterface $filesystem, $status = 200, $headers = array(), $public = true, $contentDisposition = null, $autoEtag = false)
  37. {
  38. parent::__construct(null, $status, $headers);
  39. $this->filesystem = $filesystem;
  40. $this->setFile($file, $contentDisposition, $autoEtag);
  41. if ($public) {
  42. $this->setPublic();
  43. }
  44. }
  45. /**
  46. * @param \SplFileInfo|string $file The file to stream
  47. * @param int $status The response status code
  48. * @param array $headers An array of response headers
  49. * @param bool $public Files are public by default
  50. * @param null|string $contentDisposition The type of Content-Disposition to set automatically with the filename
  51. * @param bool $autoEtag Whether the ETag header should be automatically set
  52. *
  53. * @return BinaryFileResponse The created response
  54. */
  55. public static function create($file = null, $status = 200, $headers = array(), $public = true, $contentDisposition = null, $autoEtag = false)
  56. {
  57. return new static($file, $status, $headers, $public, $contentDisposition, $autoEtag);
  58. }
  59. /**
  60. * Sets the file to stream.
  61. *
  62. * @param \SplFileInfo|string $file The file to stream
  63. * @param string $contentDisposition
  64. * @param bool $autoEtag
  65. *
  66. * @return BinaryFileResponse
  67. *
  68. * @throws FileException
  69. */
  70. public function setFile($file, $contentDisposition = null, $autoEtag = false)
  71. {
  72. if ($file instanceof File) {
  73. $file->setFilesystem($this->filesystem);
  74. } else {
  75. $file = $this->filesystem->get($file);
  76. }
  77. if (!$this->filesystem->has($file->getPath())) {
  78. throw new FileException('File must be readable.');
  79. }
  80. $this->file = $file;
  81. if ($autoEtag) {
  82. $this->setAutoEtag();
  83. }
  84. if ($contentDisposition) {
  85. $this->setContentDisposition($contentDisposition);
  86. }
  87. return $this;
  88. }
  89. /**
  90. * Gets the file.
  91. *
  92. * @return File The file to stream
  93. */
  94. public function getFile()
  95. {
  96. return $this->file;
  97. }
  98. /**
  99. * Automatically sets the ETag header according to the checksum of the file.
  100. */
  101. public function setAutoEtag()
  102. {
  103. $this->setEtag(sha1_file($this->file->getPath()));
  104. return $this;
  105. }
  106. /**
  107. * Sets the Content-Disposition header with the given filename.
  108. *
  109. * @param string $disposition ResponseHeaderBag::DISPOSITION_INLINE or ResponseHeaderBag::DISPOSITION_ATTACHMENT
  110. * @param string $filename Optionally use this filename instead of the real name of the file
  111. * @param string $filenameFallback A fallback filename, containing only ASCII characters. Defaults to an automatically encoded filename
  112. *
  113. * @return BinaryFileResponse
  114. */
  115. public function setContentDisposition($disposition, $filename = '', $filenameFallback = '')
  116. {
  117. if ($filename === '') {
  118. $filename = basename($this->file->getPath());
  119. }
  120. $dispositionHeader = $this->headers->makeDisposition($disposition, $filename, $filenameFallback);
  121. $this->headers->set('Content-Disposition', $dispositionHeader);
  122. return $this;
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function prepare(\Symfony\Component\HttpFoundation\Request $request)
  128. {
  129. $this->headers->set('Content-Length', $this->file->getSize());
  130. if (!$this->headers->has('Accept-Ranges')) {
  131. // Only accept ranges on safe HTTP methods
  132. $this->headers->set('Accept-Ranges', $request->isMethodSafe() ? 'bytes' : 'none');
  133. }
  134. if (!$this->headers->has('Content-Type')) {
  135. $this->headers->set('Content-Type', $this->file->getMimetype() ?: 'application/octet-stream');
  136. }
  137. if ('HTTP/1.0' != $request->server->get('SERVER_PROTOCOL')) {
  138. $this->setProtocolVersion('1.1');
  139. }
  140. $this->ensureIEOverSSLCompatibility($request);
  141. $this->offset = 0;
  142. $this->maxlen = -1;
  143. if ($request->headers->has('Range')) {
  144. // Process the range headers.
  145. if (!$request->headers->has('If-Range') || $this->getEtag() == $request->headers->get('If-Range')) {
  146. $range = $request->headers->get('Range');
  147. $fileSize = $this->file->getSize();
  148. list($start, $end) = explode('-', substr($range, 6), 2) + array(0);
  149. $end = ('' === $end) ? $fileSize - 1 : (int) $end;
  150. if ('' === $start) {
  151. $start = $fileSize - $end;
  152. $end = $fileSize - 1;
  153. } else {
  154. $start = (int) $start;
  155. }
  156. if ($start <= $end) {
  157. if ($start < 0 || $end > $fileSize - 1) {
  158. $this->setStatusCode(416);
  159. } elseif ($start !== 0 || $end !== $fileSize - 1) {
  160. $this->maxlen = $end < $fileSize ? $end - $start + 1 : -1;
  161. $this->offset = $start;
  162. $this->setStatusCode(206);
  163. $this->headers->set('Content-Range', sprintf('bytes %s-%s/%s', $start, $end, $fileSize));
  164. $this->headers->set('Content-Length', $end - $start + 1);
  165. }
  166. }
  167. }
  168. }
  169. return $this;
  170. }
  171. /**
  172. * Sends the file.
  173. */
  174. public function sendContent()
  175. {
  176. if (!$this->isSuccessful()) {
  177. parent::sendContent();
  178. return;
  179. }
  180. if (0 === $this->maxlen) {
  181. return;
  182. }
  183. $out = fopen('php://output', 'wb');
  184. $file = $this->filesystem->readStream($this->file->getPath());
  185. stream_copy_to_stream($file, $out, $this->maxlen, $this->offset);
  186. fclose($out);
  187. fclose($file);
  188. }
  189. /**
  190. * {@inheritdoc}
  191. *
  192. * @throws \LogicException when the content is not null
  193. */
  194. public function setContent($content)
  195. {
  196. if (null !== $content) {
  197. throw new \LogicException('The content cannot be set on a BinaryFileResponse instance.');
  198. }
  199. }
  200. /**
  201. * {@inheritdoc}
  202. *
  203. * @return false
  204. */
  205. public function getContent()
  206. {
  207. return false;
  208. }
  209. /**
  210. * Trust X-Sendfile-Type header.
  211. */
  212. public static function trustXSendfileTypeHeader()
  213. {
  214. self::$trustXSendfileTypeHeader = true;
  215. }
  216. }