]> BookStack Code Mirror - bookstack/blob - app/Entities/Controllers/BookExportController.php
Cleaned up namespacing in routes
[bookstack] / app / Entities / Controllers / BookExportController.php
1 <?php
2
3 namespace BookStack\Entities\Controllers;
4
5 use BookStack\Entities\Repos\BookRepo;
6 use BookStack\Entities\Tools\ExportFormatter;
7 use BookStack\Http\Controller;
8 use Throwable;
9
10 class BookExportController extends Controller
11 {
12     protected $bookRepo;
13     protected $exportFormatter;
14
15     /**
16      * BookExportController constructor.
17      */
18     public function __construct(BookRepo $bookRepo, ExportFormatter $exportFormatter)
19     {
20         $this->bookRepo = $bookRepo;
21         $this->exportFormatter = $exportFormatter;
22         $this->middleware('can:content-export');
23     }
24
25     /**
26      * Export a book as a PDF file.
27      *
28      * @throws Throwable
29      */
30     public function pdf(string $bookSlug)
31     {
32         $book = $this->bookRepo->getBySlug($bookSlug);
33         $pdfContent = $this->exportFormatter->bookToPdf($book);
34
35         return $this->download()->directly($pdfContent, $bookSlug . '.pdf');
36     }
37
38     /**
39      * Export a book as a contained HTML file.
40      *
41      * @throws Throwable
42      */
43     public function html(string $bookSlug)
44     {
45         $book = $this->bookRepo->getBySlug($bookSlug);
46         $htmlContent = $this->exportFormatter->bookToContainedHtml($book);
47
48         return $this->download()->directly($htmlContent, $bookSlug . '.html');
49     }
50
51     /**
52      * Export a book as a plain text file.
53      */
54     public function plainText(string $bookSlug)
55     {
56         $book = $this->bookRepo->getBySlug($bookSlug);
57         $textContent = $this->exportFormatter->bookToPlainText($book);
58
59         return $this->download()->directly($textContent, $bookSlug . '.txt');
60     }
61
62     /**
63      * Export a book as a markdown file.
64      */
65     public function markdown(string $bookSlug)
66     {
67         $book = $this->bookRepo->getBySlug($bookSlug);
68         $textContent = $this->exportFormatter->bookToMarkdown($book);
69
70         return $this->download()->directly($textContent, $bookSlug . '.md');
71     }
72 }