]> BookStack Code Mirror - bookstack/blobdiff - app/Http/Controllers/Api/ChapterExportApiController.php
Added option to configure PDF export paper size
[bookstack] / app / Http / Controllers / Api / ChapterExportApiController.php
index f19f29e9d2d752ee23182abbb0def5758e816714..5715ab2e37c6c9f7e682ad69b407f27153e3934e 100644 (file)
@@ -1,44 +1,47 @@
-<?php namespace BookStack\Http\Controllers\Api;
+<?php
 
-use BookStack\Entities\Chapter;
-use BookStack\Entities\ExportService;
-use BookStack\Entities\Repos\BookRepo;
+namespace BookStack\Http\Controllers\Api;
+
+use BookStack\Entities\Models\Chapter;
+use BookStack\Entities\Tools\ExportFormatter;
 use Throwable;
 
 class ChapterExportApiController extends ApiController
 {
-    protected $chapterRepo;
-    protected $exportService;
+    protected $exportFormatter;
 
     /**
      * ChapterExportController constructor.
      */
-    public function __construct(BookRepo $chapterRepo, ExportService $exportService)
+    public function __construct(ExportFormatter $exportFormatter)
     {
-        $this->chapterRepo = $chapterRepo;
-        $this->exportService = $exportService;
-        parent::__construct();
+        $this->exportFormatter = $exportFormatter;
+        $this->middleware('can:content-export');
     }
 
     /**
      * Export a chapter as a PDF file.
+     *
      * @throws Throwable
      */
     public function exportPdf(int $id)
     {
         $chapter = Chapter::visible()->findOrFail($id);
-        $pdfContent = $this->exportService->chapterToPdf($chapter);
+        $pdfContent = $this->exportFormatter->chapterToPdf($chapter);
+
         return $this->downloadResponse($pdfContent, $chapter->slug . '.pdf');
     }
 
     /**
      * Export a chapter as a contained HTML file.
+     *
      * @throws Throwable
      */
     public function exportHtml(int $id)
     {
         $chapter = Chapter::visible()->findOrFail($id);
-        $htmlContent = $this->exportService->chapterToContainedHtml($chapter);
+        $htmlContent = $this->exportFormatter->chapterToContainedHtml($chapter);
+
         return $this->downloadResponse($htmlContent, $chapter->slug . '.html');
     }
 
@@ -48,7 +51,19 @@ class ChapterExportApiController extends ApiController
     public function exportPlainText(int $id)
     {
         $chapter = Chapter::visible()->findOrFail($id);
-        $textContent = $this->exportService->chapterToPlainText($chapter);
+        $textContent = $this->exportFormatter->chapterToPlainText($chapter);
+
         return $this->downloadResponse($textContent, $chapter->slug . '.txt');
     }
+
+    /**
+     * Export a chapter as a markdown file.
+     */
+    public function exportMarkdown(int $id)
+    {
+        $chapter = Chapter::visible()->findOrFail($id);
+        $markdown = $this->exportFormatter->chapterToMarkdown($chapter);
+
+        return $this->downloadResponse($markdown, $chapter->slug . '.md');
+    }
 }