]> BookStack Code Mirror - bookstack/commitdiff
Extracted download response logic into controller method
authorDan Brown <redacted>
Sat, 22 Sep 2018 10:34:09 +0000 (11:34 +0100)
committerDan Brown <redacted>
Sat, 22 Sep 2018 10:34:09 +0000 (11:34 +0100)
Fixes incorrect 'Content-Disposition' header value.
Fixes #581

app/Http/Controllers/AttachmentController.php
app/Http/Controllers/BookController.php
app/Http/Controllers/ChapterController.php
app/Http/Controllers/Controller.php
app/Http/Controllers/PageController.php

index 54e14bfb6f4cd654c7bf7e9c2353432b373c6e6f..74644aa2fd848f0cde64171d3c1e2bd31f2c6c11 100644 (file)
@@ -201,10 +201,7 @@ class AttachmentController extends Controller
         }
 
         $attachmentContents = $this->attachmentService->getAttachmentFromStorage($attachment);
-        return response($attachmentContents, 200, [
-            'Content-Type' => 'application/octet-stream',
-            'Content-Disposition' => 'attachment; filename="'. $attachment->getFileName() .'"'
-        ]);
+        return $this->downloadResponse($attachmentContents, $attachment->getFileName());
     }
 
     /**
index 2c3946239cac3981e4abffab0fe5ae14b4ce2de9..ea39a771ec048529bab8995f2967306613132108 100644 (file)
@@ -299,10 +299,7 @@ class BookController extends Controller
     {
         $book = $this->entityRepo->getBySlug('book', $bookSlug);
         $pdfContent = $this->exportService->bookToPdf($book);
-        return response()->make($pdfContent, 200, [
-            'Content-Type'        => 'application/octet-stream',
-            'Content-Disposition' => 'attachment; filename="' . $bookSlug . '.pdf'
-        ]);
+        return $this->downloadResponse($pdfContent, $bookSlug . '.pdf');
     }
 
     /**
@@ -314,10 +311,7 @@ class BookController extends Controller
     {
         $book = $this->entityRepo->getBySlug('book', $bookSlug);
         $htmlContent = $this->exportService->bookToContainedHtml($book);
-        return response()->make($htmlContent, 200, [
-            'Content-Type'        => 'application/octet-stream',
-            'Content-Disposition' => 'attachment; filename="' . $bookSlug . '.html'
-        ]);
+        return $this->downloadResponse($htmlContent, $bookSlug . '.html');
     }
 
     /**
@@ -328,10 +322,7 @@ class BookController extends Controller
     public function exportPlainText($bookSlug)
     {
         $book = $this->entityRepo->getBySlug('book', $bookSlug);
-        $htmlContent = $this->exportService->bookToPlainText($book);
-        return response()->make($htmlContent, 200, [
-            'Content-Type'        => 'application/octet-stream',
-            'Content-Disposition' => 'attachment; filename="' . $bookSlug . '.txt'
-        ]);
+        $textContent = $this->exportService->bookToPlainText($book);
+        return $this->downloadResponse($textContent, $bookSlug . '.txt');
     }
 }
index b737afc6df21e5ff8697dd0b4fc681c2d7d71a23..1fe231a6597e909198f36fb0d4d1588a7a911836 100644 (file)
@@ -250,10 +250,7 @@ class ChapterController extends Controller
     {
         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
         $pdfContent = $this->exportService->chapterToPdf($chapter);
-        return response()->make($pdfContent, 200, [
-            'Content-Type'        => 'application/octet-stream',
-            'Content-Disposition' => 'attachment; filename="' . $chapterSlug . '.pdf'
-        ]);
+        return $this->downloadResponse($pdfContent, $chapterSlug . '.pdf');
     }
 
     /**
@@ -266,10 +263,7 @@ class ChapterController extends Controller
     {
         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
         $containedHtml = $this->exportService->chapterToContainedHtml($chapter);
-        return response()->make($containedHtml, 200, [
-            'Content-Type'        => 'application/octet-stream',
-            'Content-Disposition' => 'attachment; filename="' . $chapterSlug . '.html'
-        ]);
+        return $this->downloadResponse($containedHtml, $chapterSlug . '.html');
     }
 
     /**
@@ -281,10 +275,7 @@ class ChapterController extends Controller
     public function exportPlainText($bookSlug, $chapterSlug)
     {
         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
-        $containedHtml = $this->exportService->chapterToPlainText($chapter);
-        return response()->make($containedHtml, 200, [
-            'Content-Type'        => 'application/octet-stream',
-            'Content-Disposition' => 'attachment; filename="' . $chapterSlug . '.txt'
-        ]);
+        $chapterText = $this->exportService->chapterToPlainText($chapter);
+        return $this->downloadResponse($chapterText, $chapterSlug . '.txt');
     }
 }
index a51ff5d77f9cc305575677f8557fc52056dfd862..33b57b7d9a56b14f9f406c8566346e01767e0077 100644 (file)
@@ -136,7 +136,6 @@ abstract class Controller extends BaseController
 
     /**
      * Create the response for when a request fails validation.
-     *
      * @param  \Illuminate\Http\Request  $request
      * @param  array  $errors
      * @return \Symfony\Component\HttpFoundation\Response
@@ -151,4 +150,18 @@ abstract class Controller extends BaseController
             ->withInput($request->input())
             ->withErrors($errors, $this->errorBag());
     }
+
+    /**
+     * Create a response that forces a download in the browser.
+     * @param string $content
+     * @param string $fileName
+     * @return \Illuminate\Http\Response
+     */
+    protected function downloadResponse(string $content, string $fileName)
+    {
+        return response()->make($content, 200, [
+            'Content-Type'        => 'application/octet-stream',
+            'Content-Disposition' => 'attachment; filename="' . $fileName . '"'
+        ]);
+    }
 }
index e3079047cac436c13873cdf8876841d98a23f165..80bbe56c1afe179935e08abaa73cba137243b388 100644 (file)
@@ -500,10 +500,7 @@ class PageController extends Controller
         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
         $page->html = $this->entityRepo->renderPage($page);
         $pdfContent = $this->exportService->pageToPdf($page);
-        return response()->make($pdfContent, 200, [
-            'Content-Type'        => 'application/octet-stream',
-            'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.pdf'
-        ]);
+        return $this->downloadResponse($pdfContent, $pageSlug . '.pdf');
     }
 
     /**
@@ -517,10 +514,7 @@ class PageController extends Controller
         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
         $page->html = $this->entityRepo->renderPage($page);
         $containedHtml = $this->exportService->pageToContainedHtml($page);
-        return response()->make($containedHtml, 200, [
-            'Content-Type'        => 'application/octet-stream',
-            'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.html'
-        ]);
+        return $this->downloadResponse($containedHtml, $pageSlug . '.html');
     }
 
     /**
@@ -532,11 +526,8 @@ class PageController extends Controller
     public function exportPlainText($bookSlug, $pageSlug)
     {
         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
-        $containedHtml = $this->exportService->pageToPlainText($page);
-        return response()->make($containedHtml, 200, [
-            'Content-Type'        => 'application/octet-stream',
-            'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.txt'
-        ]);
+        $pageText = $this->exportService->pageToPlainText($page);
+        return $this->downloadResponse($pageText, $pageSlug . '.txt');
     }
 
     /**