]> BookStack Code Mirror - bookstack/blob - app/Exports/Controllers/BookExportController.php
Merge branch 'rashadkhan359/development' into development
[bookstack] / app / Exports / Controllers / BookExportController.php
1 <?php
2
3 namespace BookStack\Exports\Controllers;
4
5 use BookStack\Entities\Queries\BookQueries;
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 BookExportController extends Controller
13 {
14     public function __construct(
15         protected BookQueries $queries,
16         protected ExportFormatter $exportFormatter,
17     ) {
18         $this->middleware('can:content-export');
19     }
20
21     /**
22      * Export a book as a PDF file.
23      *
24      * @throws Throwable
25      */
26     public function pdf(string $bookSlug)
27     {
28         $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
29         $pdfContent = $this->exportFormatter->bookToPdf($book);
30
31         return $this->download()->directly($pdfContent, $bookSlug . '.pdf');
32     }
33
34     /**
35      * Export a book as a contained HTML file.
36      *
37      * @throws Throwable
38      */
39     public function html(string $bookSlug)
40     {
41         $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
42         $htmlContent = $this->exportFormatter->bookToContainedHtml($book);
43
44         return $this->download()->directly($htmlContent, $bookSlug . '.html');
45     }
46
47     /**
48      * Export a book as a plain text file.
49      */
50     public function plainText(string $bookSlug)
51     {
52         $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
53         $textContent = $this->exportFormatter->bookToPlainText($book);
54
55         return $this->download()->directly($textContent, $bookSlug . '.txt');
56     }
57
58     /**
59      * Export a book as a markdown file.
60      */
61     public function markdown(string $bookSlug)
62     {
63         $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
64         $textContent = $this->exportFormatter->bookToMarkdown($book);
65
66         return $this->download()->directly($textContent, $bookSlug . '.md');
67     }
68
69     /**
70      * Export a book to a contained ZIP export file.
71      * @throws NotFoundException
72      */
73     public function zip(string $bookSlug, ZipExportBuilder $builder)
74     {
75         $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
76         $zip = $builder->buildForBook($book);
77
78         return $this->download()->streamedDirectly(fopen($zip, 'r'), $bookSlug . '.zip', filesize($zip));
79     }
80 }