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