]> BookStack Code Mirror - bookstack/blob - app/Exports/Controllers/PageExportController.php
Exports: Added rate limits for UI exports
[bookstack] / app / Exports / Controllers / PageExportController.php
1 <?php
2
3 namespace BookStack\Exports\Controllers;
4
5 use BookStack\Entities\Queries\PageQueries;
6 use BookStack\Entities\Tools\PageContent;
7 use BookStack\Exceptions\NotFoundException;
8 use BookStack\Exports\ExportFormatter;
9 use BookStack\Exports\ZipExports\ZipExportBuilder;
10 use BookStack\Http\Controller;
11 use Throwable;
12
13 class PageExportController extends Controller
14 {
15     public function __construct(
16         protected PageQueries $queries,
17         protected ExportFormatter $exportFormatter,
18     ) {
19         $this->middleware('can:content-export');
20         $this->middleware('throttle:exports');
21     }
22
23     /**
24      * Exports a page to a PDF.
25      * https://github.com/barryvdh/laravel-dompdf.
26      *
27      * @throws NotFoundException
28      * @throws Throwable
29      */
30     public function pdf(string $bookSlug, string $pageSlug)
31     {
32         $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
33         $page->html = (new PageContent($page))->render();
34         $pdfContent = $this->exportFormatter->pageToPdf($page);
35
36         return $this->download()->directly($pdfContent, $pageSlug . '.pdf');
37     }
38
39     /**
40      * Export a page to a self-contained HTML file.
41      *
42      * @throws NotFoundException
43      * @throws Throwable
44      */
45     public function html(string $bookSlug, string $pageSlug)
46     {
47         $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
48         $page->html = (new PageContent($page))->render();
49         $containedHtml = $this->exportFormatter->pageToContainedHtml($page);
50
51         return $this->download()->directly($containedHtml, $pageSlug . '.html');
52     }
53
54     /**
55      * Export a page to a simple plaintext .txt file.
56      *
57      * @throws NotFoundException
58      */
59     public function plainText(string $bookSlug, string $pageSlug)
60     {
61         $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
62         $pageText = $this->exportFormatter->pageToPlainText($page);
63
64         return $this->download()->directly($pageText, $pageSlug . '.txt');
65     }
66
67     /**
68      * Export a page to a simple markdown .md file.
69      *
70      * @throws NotFoundException
71      */
72     public function markdown(string $bookSlug, string $pageSlug)
73     {
74         $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
75         $pageText = $this->exportFormatter->pageToMarkdown($page);
76
77         return $this->download()->directly($pageText, $pageSlug . '.md');
78     }
79
80     /**
81      * Export a page to a contained ZIP export file.
82      * @throws NotFoundException
83      */
84     public function zip(string $bookSlug, string $pageSlug, ZipExportBuilder $builder)
85     {
86         $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
87         $zip = $builder->buildForPage($page);
88
89         return $this->download()->streamedFileDirectly($zip, $pageSlug . '.zip', filesize($zip), true);
90     }
91 }