3 namespace BookStack\Http;
5 use BookStack\Util\WebSafeMimeSniffer;
6 use Illuminate\Http\Request;
7 use Illuminate\Http\Response;
8 use Symfony\Component\HttpFoundation\StreamedResponse;
10 class DownloadResponseFactory
12 public function __construct(
13 protected Request $request
18 * Create a response that directly forces a download in the browser.
20 public function directly(string $content, string $fileName): Response
22 return response()->make($content, 200, $this->getHeaders($fileName));
26 * Create a response that forces a download, from a given stream of content.
28 public function streamedDirectly($stream, string $fileName, int $fileSize): StreamedResponse
30 $rangeStream = new RangeSupportedStream($stream, $fileSize, $this->request->headers);
31 return response()->stream(function () use ($rangeStream) {
32 $rangeStream->outputAndClose();
33 }, 200, $this->getHeaders($fileName));
37 * Create a file download response that provides the file with a content-type
38 * correct for the file, in a way so the browser can show the content in browser,
39 * for a given content stream.
41 public function streamedInline($stream, string $fileName, int $fileSize): StreamedResponse
43 $rangeStream = new RangeSupportedStream($stream, $fileSize, $this->request->headers);
44 $mime = $rangeStream->sniffMime();
46 return response()->stream(function () use ($rangeStream) {
47 $rangeStream->outputAndClose();
48 }, 200, $this->getHeaders($fileName, $mime));
52 * Get the common headers to provide for a download response.
54 protected function getHeaders(string $fileName, string $mime = 'application/octet-stream'): array
56 $disposition = ($mime === 'application/octet-stream') ? 'attachment' : 'inline';
57 $downloadName = str_replace('"', '', $fileName);
60 'Content-Type' => $mime,
61 'Content-Disposition' => "{$disposition}; filename=\"{$downloadName}\"",
62 'X-Content-Type-Options' => 'nosniff',