3 namespace BookStack\Http\Responses;
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 protected Request $request;
14 public function __construct(Request $request)
16 $this->request = $request;
20 * Create a response that directly forces a download in the browser.
22 public function directly(string $content, string $fileName): Response
24 return response()->make($content, 200, $this->getHeaders($fileName));
28 * Create a response that forces a download, from a given stream of content.
30 public function streamedDirectly($stream, string $fileName): StreamedResponse
32 return response()->stream(function () use ($stream) {
34 // End & flush the output buffer, if we're in one, otherwise we still use memory.
35 // Output buffer may or may not exist depending on PHP `output_buffering` setting.
36 // Ignore in testing since output buffers are used to gather a response.
37 if (!empty(ob_get_status()) && !app()->runningUnitTests()) {
43 }, 200, $this->getHeaders($fileName));
47 * Create a file download response that provides the file with a content-type
48 * correct for the file, in a way so the browser can show the content in browser,
49 * for a given content stream.
51 public function streamedInline($stream, string $fileName): StreamedResponse
53 $sniffContent = fread($stream, 2000);
54 $mime = (new WebSafeMimeSniffer())->sniff($sniffContent);
56 return response()->stream(function () use ($sniffContent, $stream) {
60 }, 200, $this->getHeaders($fileName, $mime));
64 * Get the common headers to provide for a download response.
66 protected function getHeaders(string $fileName, string $mime = 'application/octet-stream'): array
68 $disposition = ($mime === 'application/octet-stream') ? 'attachment' : 'inline';
69 $downloadName = str_replace('"', '', $fileName);
72 'Content-Type' => $mime,
73 'Content-Disposition' => "{$disposition}; filename=\"{$downloadName}\"",
74 'X-Content-Type-Options' => 'nosniff',