]> BookStack Code Mirror - bookstack/blobdiff - app/Http/Controllers/Api/ChapterExportApiController.php
New translations activities.php (Portuguese)
[bookstack] / app / Http / Controllers / Api / ChapterExportApiController.php
index f19f29e9d2d752ee23182abbb0def5758e816714..faf5d812e207c667170fd8c361f4a976ee3d7f11 100644 (file)
@@ -1,45 +1,48 @@
-<?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);
-        return $this->downloadResponse($pdfContent, $chapter->slug . '.pdf');
+        $pdfContent = $this->exportFormatter->chapterToPdf($chapter);
+
+        return $this->download()->directly($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);
-        return $this->downloadResponse($htmlContent, $chapter->slug . '.html');
+        $htmlContent = $this->exportFormatter->chapterToContainedHtml($chapter);
+
+        return $this->download()->directly($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);
-        return $this->downloadResponse($textContent, $chapter->slug . '.txt');
+        $textContent = $this->exportFormatter->chapterToPlainText($chapter);
+
+        return $this->download()->directly($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->download()->directly($markdown, $chapter->slug . '.md');
     }
 }