mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-08-09 10:22:51 +03:00
Range requests: Extracted stream output handling to new class
This commit is contained in:
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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user