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