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