0
0
Fork 0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-05-06 17:20:07 +00:00

Extracted download response logic into controller method

Fixes incorrect 'Content-Disposition' header value.
Fixes 
This commit is contained in:
Dan Brown 2018-09-22 11:34:09 +01:00
parent c47b578599
commit 5c2e3f4e56
No known key found for this signature in database
GPG key ID: 46D9F943C24A2EF9
5 changed files with 27 additions and 44 deletions

View file

@ -201,10 +201,7 @@ class AttachmentController extends Controller
} }
$attachmentContents = $this->attachmentService->getAttachmentFromStorage($attachment); $attachmentContents = $this->attachmentService->getAttachmentFromStorage($attachment);
return response($attachmentContents, 200, [ return $this->downloadResponse($attachmentContents, $attachment->getFileName());
'Content-Type' => 'application/octet-stream',
'Content-Disposition' => 'attachment; filename="'. $attachment->getFileName() .'"'
]);
} }
/** /**

View file

@ -299,10 +299,7 @@ class BookController extends Controller
{ {
$book = $this->entityRepo->getBySlug('book', $bookSlug); $book = $this->entityRepo->getBySlug('book', $bookSlug);
$pdfContent = $this->exportService->bookToPdf($book); $pdfContent = $this->exportService->bookToPdf($book);
return response()->make($pdfContent, 200, [ return $this->downloadResponse($pdfContent, $bookSlug . '.pdf');
'Content-Type' => 'application/octet-stream',
'Content-Disposition' => 'attachment; filename="' . $bookSlug . '.pdf'
]);
} }
/** /**
@ -314,10 +311,7 @@ class BookController extends Controller
{ {
$book = $this->entityRepo->getBySlug('book', $bookSlug); $book = $this->entityRepo->getBySlug('book', $bookSlug);
$htmlContent = $this->exportService->bookToContainedHtml($book); $htmlContent = $this->exportService->bookToContainedHtml($book);
return response()->make($htmlContent, 200, [ return $this->downloadResponse($htmlContent, $bookSlug . '.html');
'Content-Type' => 'application/octet-stream',
'Content-Disposition' => 'attachment; filename="' . $bookSlug . '.html'
]);
} }
/** /**
@ -328,10 +322,7 @@ class BookController extends Controller
public function exportPlainText($bookSlug) public function exportPlainText($bookSlug)
{ {
$book = $this->entityRepo->getBySlug('book', $bookSlug); $book = $this->entityRepo->getBySlug('book', $bookSlug);
$htmlContent = $this->exportService->bookToPlainText($book); $textContent = $this->exportService->bookToPlainText($book);
return response()->make($htmlContent, 200, [ return $this->downloadResponse($textContent, $bookSlug . '.txt');
'Content-Type' => 'application/octet-stream',
'Content-Disposition' => 'attachment; filename="' . $bookSlug . '.txt'
]);
} }
} }

View file

@ -250,10 +250,7 @@ class ChapterController extends Controller
{ {
$chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug); $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
$pdfContent = $this->exportService->chapterToPdf($chapter); $pdfContent = $this->exportService->chapterToPdf($chapter);
return response()->make($pdfContent, 200, [ return $this->downloadResponse($pdfContent, $chapterSlug . '.pdf');
'Content-Type' => 'application/octet-stream',
'Content-Disposition' => 'attachment; filename="' . $chapterSlug . '.pdf'
]);
} }
/** /**
@ -266,10 +263,7 @@ class ChapterController extends Controller
{ {
$chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug); $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
$containedHtml = $this->exportService->chapterToContainedHtml($chapter); $containedHtml = $this->exportService->chapterToContainedHtml($chapter);
return response()->make($containedHtml, 200, [ return $this->downloadResponse($containedHtml, $chapterSlug . '.html');
'Content-Type' => 'application/octet-stream',
'Content-Disposition' => 'attachment; filename="' . $chapterSlug . '.html'
]);
} }
/** /**
@ -281,10 +275,7 @@ class ChapterController extends Controller
public function exportPlainText($bookSlug, $chapterSlug) public function exportPlainText($bookSlug, $chapterSlug)
{ {
$chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug); $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
$containedHtml = $this->exportService->chapterToPlainText($chapter); $chapterText = $this->exportService->chapterToPlainText($chapter);
return response()->make($containedHtml, 200, [ return $this->downloadResponse($chapterText, $chapterSlug . '.txt');
'Content-Type' => 'application/octet-stream',
'Content-Disposition' => 'attachment; filename="' . $chapterSlug . '.txt'
]);
} }
} }

View file

@ -136,7 +136,6 @@ abstract class Controller extends BaseController
/** /**
* Create the response for when a request fails validation. * Create the response for when a request fails validation.
*
* @param \Illuminate\Http\Request $request * @param \Illuminate\Http\Request $request
* @param array $errors * @param array $errors
* @return \Symfony\Component\HttpFoundation\Response * @return \Symfony\Component\HttpFoundation\Response
@ -151,4 +150,18 @@ abstract class Controller extends BaseController
->withInput($request->input()) ->withInput($request->input())
->withErrors($errors, $this->errorBag()); ->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 . '"'
]);
}
} }

View file

@ -500,10 +500,7 @@ class PageController extends Controller
$page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug); $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
$page->html = $this->entityRepo->renderPage($page); $page->html = $this->entityRepo->renderPage($page);
$pdfContent = $this->exportService->pageToPdf($page); $pdfContent = $this->exportService->pageToPdf($page);
return response()->make($pdfContent, 200, [ return $this->downloadResponse($pdfContent, $pageSlug . '.pdf');
'Content-Type' => 'application/octet-stream',
'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.pdf'
]);
} }
/** /**
@ -517,10 +514,7 @@ class PageController extends Controller
$page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug); $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
$page->html = $this->entityRepo->renderPage($page); $page->html = $this->entityRepo->renderPage($page);
$containedHtml = $this->exportService->pageToContainedHtml($page); $containedHtml = $this->exportService->pageToContainedHtml($page);
return response()->make($containedHtml, 200, [ return $this->downloadResponse($containedHtml, $pageSlug . '.html');
'Content-Type' => 'application/octet-stream',
'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.html'
]);
} }
/** /**
@ -532,11 +526,8 @@ class PageController extends Controller
public function exportPlainText($bookSlug, $pageSlug) public function exportPlainText($bookSlug, $pageSlug)
{ {
$page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug); $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
$containedHtml = $this->exportService->pageToPlainText($page); $pageText = $this->exportService->pageToPlainText($page);
return response()->make($containedHtml, 200, [ return $this->downloadResponse($pageText, $pageSlug . '.txt');
'Content-Type' => 'application/octet-stream',
'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.txt'
]);
} }
/** /**