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