]> BookStack Code Mirror - bookstack/blob - app/Http/DownloadResponseFactory.php
Range requests: Extracted stream output handling to new class
[bookstack] / app / Http / DownloadResponseFactory.php
1 <?php
2
3 namespace BookStack\Http;
4
5 use BookStack\Util\WebSafeMimeSniffer;
6 use Illuminate\Http\Request;
7 use Illuminate\Http\Response;
8 use Symfony\Component\HttpFoundation\StreamedResponse;
9
10 class DownloadResponseFactory
11 {
12     public function __construct(
13         protected Request $request
14     ) {
15     }
16
17     /**
18      * Create a response that directly forces a download in the browser.
19      */
20     public function directly(string $content, string $fileName): Response
21     {
22         return response()->make($content, 200, $this->getHeaders($fileName));
23     }
24
25     /**
26      * Create a response that forces a download, from a given stream of content.
27      */
28     public function streamedDirectly($stream, string $fileName, int $fileSize): StreamedResponse
29     {
30         $rangeStream = new RangeSupportedStream($stream, $fileSize, $this->request->headers);
31         return response()->stream(function () use ($rangeStream) {
32             $rangeStream->outputAndClose();
33         }, 200, $this->getHeaders($fileName));
34     }
35
36     /**
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.
40      */
41     public function streamedInline($stream, string $fileName, int $fileSize): StreamedResponse
42     {
43         $rangeStream = new RangeSupportedStream($stream, $fileSize, $this->request->headers);
44         $mime = $rangeStream->sniffMime();
45
46         return response()->stream(function () use ($rangeStream) {
47             $rangeStream->outputAndClose();
48         }, 200, $this->getHeaders($fileName, $mime));
49     }
50
51     /**
52      * Get the common headers to provide for a download response.
53      */
54     protected function getHeaders(string $fileName, string $mime = 'application/octet-stream'): array
55     {
56         $disposition = ($mime === 'application/octet-stream') ? 'attachment' : 'inline';
57         $downloadName = str_replace('"', '', $fileName);
58
59         return [
60             'Content-Type'           => $mime,
61             'Content-Disposition'    => "{$disposition}; filename=\"{$downloadName}\"",
62             'X-Content-Type-Options' => 'nosniff',
63         ];
64     }
65 }