]> BookStack Code Mirror - bookstack/blob - app/Exports/Controllers/PageExportController.php
ZIP Exports: Improved temp file tracking & clean-up
[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     }
21
22     /**
23      * Exports a page to a PDF.
24      * https://github.com/barryvdh/laravel-dompdf.
25      *
26      * @throws NotFoundException
27      * @throws Throwable
28      */
29     public function pdf(string $bookSlug, string $pageSlug)
30     {
31         $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
32         $page->html = (new PageContent($page))->render();
33         $pdfContent = $this->exportFormatter->pageToPdf($page);
34
35         return $this->download()->directly($pdfContent, $pageSlug . '.pdf');
36     }
37
38     /**
39      * Export a page to a self-contained HTML file.
40      *
41      * @throws NotFoundException
42      * @throws Throwable
43      */
44     public function html(string $bookSlug, string $pageSlug)
45     {
46         $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
47         $page->html = (new PageContent($page))->render();
48         $containedHtml = $this->exportFormatter->pageToContainedHtml($page);
49
50         return $this->download()->directly($containedHtml, $pageSlug . '.html');
51     }
52
53     /**
54      * Export a page to a simple plaintext .txt file.
55      *
56      * @throws NotFoundException
57      */
58     public function plainText(string $bookSlug, string $pageSlug)
59     {
60         $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
61         $pageText = $this->exportFormatter->pageToPlainText($page);
62
63         return $this->download()->directly($pageText, $pageSlug . '.txt');
64     }
65
66     /**
67      * Export a page to a simple markdown .md file.
68      *
69      * @throws NotFoundException
70      */
71     public function markdown(string $bookSlug, string $pageSlug)
72     {
73         $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
74         $pageText = $this->exportFormatter->pageToMarkdown($page);
75
76         return $this->download()->directly($pageText, $pageSlug . '.md');
77     }
78
79     /**
80      * Export a page to a contained ZIP export file.
81      * @throws NotFoundException
82      */
83     public function zip(string $bookSlug, string $pageSlug, ZipExportBuilder $builder)
84     {
85         $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
86         $zip = $builder->buildForPage($page);
87
88         return $this->download()->streamedFileDirectly($zip, $pageSlug . '.zip', filesize($zip), true);
89     }
90 }