]> BookStack Code Mirror - bookstack/blob - app/Exports/Controllers/ChapterExportController.php
Lexical: Added about button/view
[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     }
20
21     /**
22      * Exports a chapter to pdf.
23      *
24      * @throws NotFoundException
25      * @throws Throwable
26      */
27     public function pdf(string $bookSlug, string $chapterSlug)
28     {
29         $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
30         $pdfContent = $this->exportFormatter->chapterToPdf($chapter);
31
32         return $this->download()->directly($pdfContent, $chapterSlug . '.pdf');
33     }
34
35     /**
36      * Export a chapter to a self-contained HTML file.
37      *
38      * @throws NotFoundException
39      * @throws Throwable
40      */
41     public function html(string $bookSlug, string $chapterSlug)
42     {
43         $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
44         $containedHtml = $this->exportFormatter->chapterToContainedHtml($chapter);
45
46         return $this->download()->directly($containedHtml, $chapterSlug . '.html');
47     }
48
49     /**
50      * Export a chapter to a simple plaintext .txt file.
51      *
52      * @throws NotFoundException
53      */
54     public function plainText(string $bookSlug, string $chapterSlug)
55     {
56         $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
57         $chapterText = $this->exportFormatter->chapterToPlainText($chapter);
58
59         return $this->download()->directly($chapterText, $chapterSlug . '.txt');
60     }
61
62     /**
63      * Export a chapter to a simple markdown file.
64      *
65      * @throws NotFoundException
66      */
67     public function markdown(string $bookSlug, string $chapterSlug)
68     {
69         $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
70         $chapterText = $this->exportFormatter->chapterToMarkdown($chapter);
71
72         return $this->download()->directly($chapterText, $chapterSlug . '.md');
73     }
74
75     /**
76      * Export a book to a contained ZIP export file.
77      * @throws NotFoundException
78      */
79     public function zip(string $bookSlug, string $chapterSlug, ZipExportBuilder $builder)
80     {
81         $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
82         $zip = $builder->buildForChapter($chapter);
83
84         return $this->download()->streamedDirectly(fopen($zip, 'r'), $chapterSlug . '.zip', filesize($zip));
85     }
86 }