class DownloadResponseFactory
{
- protected Request $request;
-
- public function __construct(Request $request)
- {
- $this->request = $request;
+ public function __construct(
+ protected Request $request
+ ) {
}
/**
/**
* Create a response that forces a download, from a given stream of content.
*/
- public function streamedDirectly($stream, string $fileName): StreamedResponse
+ public function streamedDirectly($stream, string $fileName, int $fileSize): StreamedResponse
{
- return response()->stream(function () use ($stream) {
-
- // End & flush the output buffer, if we're in one, otherwise we still use memory.
- // Output buffer may or may not exist depending on PHP `output_buffering` setting.
- // Ignore in testing since output buffers are used to gather a response.
- if (!empty(ob_get_status()) && !app()->runningUnitTests()) {
- ob_end_clean();
- }
-
- fpassthru($stream);
- fclose($stream);
+ $rangeStream = new RangeSupportedStream($stream, $fileSize, $this->request->headers);
+ return response()->stream(function () use ($rangeStream) {
+ $rangeStream->outputAndClose();
}, 200, $this->getHeaders($fileName));
}
* correct for the file, in a way so the browser can show the content in browser,
* for a given content stream.
*/
- public function streamedInline($stream, string $fileName): StreamedResponse
+ public function streamedInline($stream, string $fileName, int $fileSize): StreamedResponse
{
- $sniffContent = fread($stream, 2000);
- $mime = (new WebSafeMimeSniffer())->sniff($sniffContent);
+ $rangeStream = new RangeSupportedStream($stream, $fileSize, $this->request->headers);
+ $mime = $rangeStream->sniffMime();
- return response()->stream(function () use ($sniffContent, $stream) {
- echo $sniffContent;
- fpassthru($stream);
- fclose($stream);
+ return response()->stream(function () use ($rangeStream) {
+ $rangeStream->outputAndClose();
}, 200, $this->getHeaders($fileName, $mime));
}
--- /dev/null
+<?php
+
+namespace BookStack\Http;
+
+use BookStack\Util\WebSafeMimeSniffer;
+use Symfony\Component\HttpFoundation\HeaderBag;
+
+class RangeSupportedStream
+{
+ protected string $sniffContent;
+
+ public function __construct(
+ protected $stream,
+ protected int $fileSize,
+ protected HeaderBag $requestHeaders,
+ ) {
+ }
+
+ /**
+ * Sniff a mime type from the stream.
+ */
+ public function sniffMime(): string
+ {
+ $offset = min(2000, $this->fileSize);
+ $this->sniffContent = fread($this->stream, $offset);
+
+ return (new WebSafeMimeSniffer())->sniff($this->sniffContent);
+ }
+
+ /**
+ * Output the current stream to stdout before closing out the stream.
+ */
+ public function outputAndClose(): void
+ {
+ // End & flush the output buffer, if we're in one, otherwise we still use memory.
+ // Output buffer may or may not exist depending on PHP `output_buffering` setting.
+ // Ignore in testing since output buffers are used to gather a response.
+ if (!empty(ob_get_status()) && !app()->runningUnitTests()) {
+ ob_end_clean();
+ }
+
+ $outStream = fopen('php://output', 'w');
+ $offset = 0;
+
+ if (!empty($this->sniffContent)) {
+ fwrite($outStream, $this->sniffContent);
+ $offset = strlen($this->sniffContent);
+ }
+
+ $toWrite = $this->fileSize - $offset;
+ stream_copy_to_stream($this->stream, $outStream, $toWrite);
+ fpassthru($this->stream);
+
+ fclose($this->stream);
+ fclose($outStream);
+ }
+}
/**
* Stream an attachment from storage.
*
- * @throws FileNotFoundException
- *
* @return resource|null
*/
public function streamAttachmentFromStorage(Attachment $attachment)
return $this->getStorageDisk()->readStream($this->adjustPathForStorageDisk($attachment->path));
}
+ /**
+ * Read the file size of an attachment from storage, in bytes.
+ */
+ public function getAttachmentFileSize(Attachment $attachment): int
+ {
+ return $this->getStorageDisk()->size($this->adjustPathForStorageDisk($attachment->path));
+ }
+
/**
* Store a new attachment upon user upload.
*
$fileName = $attachment->getFileName();
$attachmentStream = $this->attachmentService->streamAttachmentFromStorage($attachment);
+ $attachmentSize = $this->attachmentService->getAttachmentFileSize($attachment);
if ($request->get('open') === 'true') {
- return $this->download()->streamedInline($attachmentStream, $fileName);
+ return $this->download()->streamedInline($attachmentStream, $fileName, $attachmentSize);
}
- return $this->download()->streamedDirectly($attachmentStream, $fileName);
+ return $this->download()->streamedDirectly($attachmentStream, $fileName, $attachmentSize);
}
/**