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