]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Api/ChapterExportApiController.php
Added role permissions for exporting content
[bookstack] / app / Http / Controllers / Api / ChapterExportApiController.php
1 <?php
2
3 namespace BookStack\Http\Controllers\Api;
4
5 use BookStack\Entities\Models\Chapter;
6 use BookStack\Entities\Tools\ExportFormatter;
7 use Throwable;
8
9 class ChapterExportApiController extends ApiController
10 {
11     protected $exportFormatter;
12
13     /**
14      * ChapterExportController constructor.
15      */
16     public function __construct(ExportFormatter $exportFormatter)
17     {
18         $this->exportFormatter = $exportFormatter;
19         $this->middleware('can:content-export');
20     }
21
22     /**
23      * Export a chapter as a PDF file.
24      *
25      * @throws Throwable
26      */
27     public function exportPdf(int $id)
28     {
29         $chapter = Chapter::visible()->findOrFail($id);
30         $pdfContent = $this->exportFormatter->chapterToPdf($chapter);
31
32         return $this->downloadResponse($pdfContent, $chapter->slug . '.pdf');
33     }
34
35     /**
36      * Export a chapter as a contained HTML file.
37      *
38      * @throws Throwable
39      */
40     public function exportHtml(int $id)
41     {
42         $chapter = Chapter::visible()->findOrFail($id);
43         $htmlContent = $this->exportFormatter->chapterToContainedHtml($chapter);
44
45         return $this->downloadResponse($htmlContent, $chapter->slug . '.html');
46     }
47
48     /**
49      * Export a chapter as a plain text file.
50      */
51     public function exportPlainText(int $id)
52     {
53         $chapter = Chapter::visible()->findOrFail($id);
54         $textContent = $this->exportFormatter->chapterToPlainText($chapter);
55
56         return $this->downloadResponse($textContent, $chapter->slug . '.txt');
57     }
58
59     /**
60      * Export a chapter as a markdown file.
61      */
62     public function exportMarkdown(int $id)
63     {
64         $chapter = Chapter::visible()->findOrFail($id);
65         $markdown = $this->exportFormatter->chapterToMarkdown($chapter);
66
67         return $this->downloadResponse($markdown, $chapter->slug . '.md');
68     }
69 }