Fixes incorrect 'Content-Disposition' header value.
Fixes #581
}
$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());
}
/**
{
$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');
}
/**
{
$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');
}
/**
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');
}
}
{
$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');
}
/**
{
$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');
}
/**
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');
}
}
/**
* Create the response for when a request fails validation.
- *
* @param \Illuminate\Http\Request $request
* @param array $errors
* @return \Symfony\Component\HttpFoundation\Response
->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 . '"'
+ ]);
+ }
}
$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');
}
/**
$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');
}
/**
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');
}
/**