mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-10-01 01:36:00 -04:00
Range requests: Extracted stream output handling to new class
This commit is contained in:
parent
b191d8f99f
commit
b4d9029dc3
@ -9,11 +9,9 @@ use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
|
||||
class DownloadResponseFactory
|
||||
{
|
||||
protected Request $request;
|
||||
|
||||
public function __construct(Request $request)
|
||||
{
|
||||
$this->request = $request;
|
||||
public function __construct(
|
||||
protected Request $request
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
@ -27,19 +25,11 @@ class DownloadResponseFactory
|
||||
/**
|
||||
* 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));
|
||||
}
|
||||
|
||||
@ -48,15 +38,13 @@ class DownloadResponseFactory
|
||||
* 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));
|
||||
}
|
||||
|
||||
|
57
app/Http/RangeSupportedStream.php
Normal file
57
app/Http/RangeSupportedStream.php
Normal file
@ -0,0 +1,57 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
@ -66,8 +66,6 @@ class AttachmentService
|
||||
/**
|
||||
* Stream an attachment from storage.
|
||||
*
|
||||
* @throws FileNotFoundException
|
||||
*
|
||||
* @return resource|null
|
||||
*/
|
||||
public function streamAttachmentFromStorage(Attachment $attachment)
|
||||
@ -75,6 +73,14 @@ class AttachmentService
|
||||
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.
|
||||
*
|
||||
|
@ -226,12 +226,13 @@ class AttachmentController extends Controller
|
||||
|
||||
$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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user