NOW MANAGED ON CODEBERG
Extracted download response logic into controller method
Dan Brown5c2e3f4e565b
diff --git a/app/Http/Controllers/AttachmentController.php b/app/Http/Controllers/AttachmentController.phpindex 54e14bfb6..74644aa2f 100644--- a/app/Http/Controllers/AttachmentController.php+++ b/app/Http/Controllers/AttachmentController.php@@ -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());}/**diff --git a/app/Http/Controllers/BookController.php b/app/Http/Controllers/BookController.phpindex 2c3946239..ea39a771e 100644--- a/app/Http/Controllers/BookController.php+++ b/app/Http/Controllers/BookController.php@@ -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 Controllerpublic 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');}}diff --git a/app/Http/Controllers/ChapterController.php b/app/Http/Controllers/ChapterController.phpindex b737afc6d..1fe231a65 100644--- a/app/Http/Controllers/ChapterController.php+++ b/app/Http/Controllers/ChapterController.php@@ -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 Controllerpublic 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');}}diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.phpindex a51ff5d77..33b57b7d9 100644--- a/app/Http/Controllers/Controller.php+++ b/app/Http/Controllers/Controller.php@@ -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 . '"'+ ]);+ }}diff --git a/app/Http/Controllers/PageController.php b/app/Http/Controllers/PageController.phpindex e3079047c..80bbe56c1 100644--- a/app/Http/Controllers/PageController.php+++ b/app/Http/Controllers/PageController.php@@ -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 Controllerpublic 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');}/**