]> BookStack Code Mirror - bookstack/blobdiff - app/Http/Controllers/Api/ChapterExportApiController.php
New translations editor.php (Russian)
[bookstack] / app / Http / Controllers / Api / ChapterExportApiController.php
index 923956f5da4a3cb139593475f82a86b73e0a726d..faf5d812e207c667170fd8c361f4a976ee3d7f11 100644 (file)
@@ -1,44 +1,48 @@
-<?php namespace BookStack\Http\Controllers\Api;
+<?php
+
+namespace BookStack\Http\Controllers\Api;
 
 use BookStack\Entities\Models\Chapter;
 use BookStack\Entities\Tools\ExportFormatter;
-use BookStack\Entities\Repos\BookRepo;
 use Throwable;
 
 class ChapterExportApiController extends ApiController
 {
-    protected $chapterRepo;
-    protected $exportService;
+    protected $exportFormatter;
 
     /**
      * ChapterExportController constructor.
      */
-    public function __construct(BookRepo $chapterRepo, ExportFormatter $exportService)
+    public function __construct(ExportFormatter $exportFormatter)
     {
-        $this->chapterRepo = $chapterRepo;
-        $this->exportService = $exportService;
+        $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');
     }
 
     /**
@@ -47,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');
     }
 }