]> BookStack Code Mirror - bookstack/blob - app/Exports/Controllers/ChapterExportController.php
Exports: Added rate limits for UI exports
[bookstack] / app / Exports / Controllers / ChapterExportController.php
1 <?php
2
3 namespace BookStack\Exports\Controllers;
4
5 use BookStack\Entities\Queries\ChapterQueries;
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 ChapterExportController extends Controller
13 {
14     public function __construct(
15         protected ChapterQueries $queries,
16         protected ExportFormatter $exportFormatter,
17     ) {
18         $this->middleware('can:content-export');
19         $this->middleware('throttle:exports');
20     }
21
22     /**
23      * Exports a chapter to pdf.
24      *
25      * @throws NotFoundException
26      * @throws Throwable
27      */
28     public function pdf(string $bookSlug, string $chapterSlug)
29     {
30         $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
31         $pdfContent = $this->exportFormatter->chapterToPdf($chapter);
32
33         return $this->download()->directly($pdfContent, $chapterSlug . '.pdf');
34     }
35
36     /**
37      * Export a chapter to a self-contained HTML file.
38      *
39      * @throws NotFoundException
40      * @throws Throwable
41      */
42     public function html(string $bookSlug, string $chapterSlug)
43     {
44         $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
45         $containedHtml = $this->exportFormatter->chapterToContainedHtml($chapter);
46
47         return $this->download()->directly($containedHtml, $chapterSlug . '.html');
48     }
49
50     /**
51      * Export a chapter to a simple plaintext .txt file.
52      *
53      * @throws NotFoundException
54      */
55     public function plainText(string $bookSlug, string $chapterSlug)
56     {
57         $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
58         $chapterText = $this->exportFormatter->chapterToPlainText($chapter);
59
60         return $this->download()->directly($chapterText, $chapterSlug . '.txt');
61     }
62
63     /**
64      * Export a chapter to a simple markdown file.
65      *
66      * @throws NotFoundException
67      */
68     public function markdown(string $bookSlug, string $chapterSlug)
69     {
70         $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
71         $chapterText = $this->exportFormatter->chapterToMarkdown($chapter);
72
73         return $this->download()->directly($chapterText, $chapterSlug . '.md');
74     }
75
76     /**
77      * Export a book to a contained ZIP export file.
78      * @throws NotFoundException
79      */
80     public function zip(string $bookSlug, string $chapterSlug, ZipExportBuilder $builder)
81     {
82         $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
83         $zip = $builder->buildForChapter($chapter);
84
85         return $this->download()->streamedFileDirectly($zip, $chapterSlug . '.zip', filesize($zip), true);
86     }
87 }