]> BookStack Code Mirror - bookstack/blob - app/Exports/Controllers/BookExportController.php
Permissions: Updated use of helpers to use enums
[bookstack] / app / Exports / Controllers / BookExportController.php
1 <?php
2
3 namespace BookStack\Exports\Controllers;
4
5 use BookStack\Entities\Queries\BookQueries;
6 use BookStack\Exceptions\NotFoundException;
7 use BookStack\Exports\ExportFormatter;
8 use BookStack\Exports\ZipExports\ZipExportBuilder;
9 use BookStack\Http\Controller;
10 use BookStack\Permissions\Permission;
11 use Throwable;
12
13 class BookExportController extends Controller
14 {
15     public function __construct(
16         protected BookQueries $queries,
17         protected ExportFormatter $exportFormatter,
18     ) {
19         $this->middleware(Permission::ContentExport->middleware());
20         $this->middleware('throttle:exports');
21     }
22
23     /**
24      * Export a book as a PDF file.
25      *
26      * @throws Throwable
27      */
28     public function pdf(string $bookSlug)
29     {
30         $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
31         $pdfContent = $this->exportFormatter->bookToPdf($book);
32
33         return $this->download()->directly($pdfContent, $bookSlug . '.pdf');
34     }
35
36     /**
37      * Export a book as a contained HTML file.
38      *
39      * @throws Throwable
40      */
41     public function html(string $bookSlug)
42     {
43         $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
44         $htmlContent = $this->exportFormatter->bookToContainedHtml($book);
45
46         return $this->download()->directly($htmlContent, $bookSlug . '.html');
47     }
48
49     /**
50      * Export a book as a plain text file.
51      */
52     public function plainText(string $bookSlug)
53     {
54         $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
55         $textContent = $this->exportFormatter->bookToPlainText($book);
56
57         return $this->download()->directly($textContent, $bookSlug . '.txt');
58     }
59
60     /**
61      * Export a book as a markdown file.
62      */
63     public function markdown(string $bookSlug)
64     {
65         $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
66         $textContent = $this->exportFormatter->bookToMarkdown($book);
67
68         return $this->download()->directly($textContent, $bookSlug . '.md');
69     }
70
71     /**
72      * Export a book to a contained ZIP export file.
73      * @throws NotFoundException
74      */
75     public function zip(string $bookSlug, ZipExportBuilder $builder)
76     {
77         $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
78         $zip = $builder->buildForBook($book);
79
80         return $this->download()->streamedFileDirectly($zip, $bookSlug . '.zip', true);
81     }
82 }