mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-08-09 10:22:51 +03:00
Range requests: Added basic HTTP range support
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace BookStack\Http;
|
||||
|
||||
use BookStack\Util\WebSafeMimeSniffer;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
@@ -19,7 +18,7 @@ class DownloadResponseFactory
|
||||
*/
|
||||
public function directly(string $content, string $fileName): Response
|
||||
{
|
||||
return response()->make($content, 200, $this->getHeaders($fileName));
|
||||
return response()->make($content, 200, $this->getHeaders($fileName, strlen($content)));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -27,10 +26,13 @@ class DownloadResponseFactory
|
||||
*/
|
||||
public function streamedDirectly($stream, string $fileName, int $fileSize): StreamedResponse
|
||||
{
|
||||
$rangeStream = new RangeSupportedStream($stream, $fileSize, $this->request->headers);
|
||||
return response()->stream(function () use ($rangeStream) {
|
||||
$rangeStream->outputAndClose();
|
||||
}, 200, $this->getHeaders($fileName));
|
||||
$rangeStream = new RangeSupportedStream($stream, $fileSize, $this->request);
|
||||
$headers = array_merge($this->getHeaders($fileName, $fileSize), $rangeStream->getResponseHeaders());
|
||||
return response()->stream(
|
||||
fn() => $rangeStream->outputAndClose(),
|
||||
$rangeStream->getResponseStatus(),
|
||||
$headers,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -40,24 +42,28 @@ class DownloadResponseFactory
|
||||
*/
|
||||
public function streamedInline($stream, string $fileName, int $fileSize): StreamedResponse
|
||||
{
|
||||
$rangeStream = new RangeSupportedStream($stream, $fileSize, $this->request->headers);
|
||||
$rangeStream = new RangeSupportedStream($stream, $fileSize, $this->request);
|
||||
$mime = $rangeStream->sniffMime();
|
||||
$headers = array_merge($this->getHeaders($fileName, $fileSize, $mime), $rangeStream->getResponseHeaders());
|
||||
|
||||
return response()->stream(function () use ($rangeStream) {
|
||||
$rangeStream->outputAndClose();
|
||||
}, 200, $this->getHeaders($fileName, $mime));
|
||||
return response()->stream(
|
||||
fn() => $rangeStream->outputAndClose(),
|
||||
$rangeStream->getResponseStatus(),
|
||||
$headers,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the common headers to provide for a download response.
|
||||
*/
|
||||
protected function getHeaders(string $fileName, string $mime = 'application/octet-stream'): array
|
||||
protected function getHeaders(string $fileName, int $fileSize, string $mime = 'application/octet-stream'): array
|
||||
{
|
||||
$disposition = ($mime === 'application/octet-stream') ? 'attachment' : 'inline';
|
||||
$downloadName = str_replace('"', '', $fileName);
|
||||
|
||||
return [
|
||||
'Content-Type' => $mime,
|
||||
'Content-Length' => $fileSize,
|
||||
'Content-Disposition' => "{$disposition}; filename=\"{$downloadName}\"",
|
||||
'X-Content-Type-Options' => 'nosniff',
|
||||
];
|
||||
|
Reference in New Issue
Block a user