]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookExportController.php
support exporting books as zip files
[bookstack] / app / Http / Controllers / BookExportController.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
5 use BookStack\Entities\Managers\BookContents;
6 use BookStack\Entities\ExportService;
7 use BookStack\Entities\Repos\BookRepo;
8 use Throwable;
9 use ZipArchive;
10
11 class BookExportController extends Controller
12 {
13
14     protected $bookRepo;
15     protected $exportService;
16
17     /**
18      * BookExportController constructor.
19      */
20     public function __construct(BookRepo $bookRepo, ExportService $exportService)
21     {
22         $this->bookRepo = $bookRepo;
23         $this->exportService = $exportService;
24         parent::__construct();
25     }
26
27     /**
28      * Export a book as a PDF file.
29      * @throws Throwable
30      */
31     public function pdf(string $bookSlug)
32     {
33         $book = $this->bookRepo->getBySlug($bookSlug);
34         $pdfContent = $this->exportService->bookToPdf($book);
35         return $this->downloadResponse($pdfContent, $bookSlug . '.pdf');
36     }
37
38     /**
39      * Export a book as a contained HTML file.
40      * @throws Throwable
41      */
42     public function html(string $bookSlug)
43     {
44         $book = $this->bookRepo->getBySlug($bookSlug);
45         $htmlContent = $this->exportService->bookToContainedHtml($book);
46         return $this->downloadResponse($htmlContent, $bookSlug . '.html');
47     }
48
49     /**
50      * Export a book as a plain text file.
51      */
52     public function plainText(string $bookSlug)
53     {
54         $book = $this->bookRepo->getBySlug($bookSlug);
55         $textContent = $this->exportService->bookToPlainText($book);
56         return $this->downloadResponse($textContent, $bookSlug . '.txt');
57     }
58
59     /**
60      * Export a book as a markdown file.
61      */
62     public function markdown(string $bookSlug)
63     {
64         $book = $this->bookRepo->getBySlug($bookSlug);
65         $textContent = $this->exportService->bookToMarkdown($book);
66         return $this->downloadResponse($textContent, $bookSlug . '.md');
67     }
68
69     /**
70      * Export a book as a zip file, made of markdown files.
71      */
72     public function zip(string $bookSlug)
73     {
74         $book = $this->bookRepo->getBySlug($bookSlug);
75         $z = new ZipArchive();
76         $z->open("book.zip", \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
77         $bookTree = (new BookContents($book))->getTree(false, true);
78         foreach ($bookTree as $bookChild) {
79             if ($bookChild->isA('chapter')) {
80                 $z->addEmptyDir($bookChild->name);
81                 foreach ($bookChild->pages as $page) {
82                     $z->addFromString($bookChild->name . "/" . $page->name . ".md", $this->exportService->pageToMarkdown($page));
83                 }
84             } else {
85                 $z->addFromString($bookChild->name . ".md", $this->exportService->pageToMarkdown($bookChild));
86             }
87         }
88         return response()->download('book.zip');
89         // TODO: Is not unlinking it a security issue?
90     }
91 }