Split out export actions into own controllers

This commit is contained in:
Dan Brown 2019-09-15 22:33:27 +01:00
parent 3281925375
commit d28abf24d4
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
7 changed files with 272 additions and 161 deletions

View File

@ -16,7 +16,6 @@ class BookController extends Controller
protected $entityRepo;
protected $userRepo;
protected $exportService;
protected $entityContextManager;
protected $imageRepo;
@ -24,20 +23,17 @@ class BookController extends Controller
* BookController constructor.
* @param EntityRepo $entityRepo
* @param UserRepo $userRepo
* @param ExportService $exportService
* @param EntityContextManager $entityContextManager
* @param ImageRepo $imageRepo
*/
public function __construct(
EntityRepo $entityRepo,
UserRepo $userRepo,
ExportService $exportService,
EntityContextManager $entityContextManager,
ImageRepo $imageRepo
) {
$this->entityRepo = $entityRepo;
$this->userRepo = $userRepo;
$this->exportService = $exportService;
$this->entityContextManager = $entityContextManager;
$this->imageRepo = $imageRepo;
parent::__construct();
@ -369,42 +365,6 @@ class BookController extends Controller
return redirect($book->getUrl());
}
/**
* Export a book as a PDF file.
* @param string $bookSlug
* @return mixed
*/
public function exportPdf($bookSlug)
{
$book = $this->entityRepo->getBySlug('book', $bookSlug);
$pdfContent = $this->exportService->bookToPdf($book);
return $this->downloadResponse($pdfContent, $bookSlug . '.pdf');
}
/**
* Export a book as a contained HTML file.
* @param string $bookSlug
* @return mixed
*/
public function exportHtml($bookSlug)
{
$book = $this->entityRepo->getBySlug('book', $bookSlug);
$htmlContent = $this->exportService->bookToContainedHtml($book);
return $this->downloadResponse($htmlContent, $bookSlug . '.html');
}
/**
* Export a book as a plain text file.
* @param $bookSlug
* @return mixed
*/
public function exportPlainText($bookSlug)
{
$book = $this->entityRepo->getBySlug('book', $bookSlug);
$textContent = $this->exportService->bookToPlainText($book);
return $this->downloadResponse($textContent, $bookSlug . '.txt');
}
/**
* Common actions to run on book update.
* Handles updating the cover image.

View File

@ -0,0 +1,74 @@
<?php
namespace BookStack\Http\Controllers;
use BookStack\Entities\ExportService;
use BookStack\Entities\Repos\EntityRepo;
use BookStack\Exceptions\NotFoundException;
use Throwable;
class BookExportController extends Controller
{
/**
* @var EntityRepo
*/
protected $entityRepo;
/**
* @var ExportService
*/
protected $exportService;
/**
* BookExportController constructor.
* @param EntityRepo $entityRepo
* @param ExportService $exportService
*/
public function __construct(EntityRepo $entityRepo, ExportService $exportService)
{
$this->entityRepo = $entityRepo;
$this->exportService = $exportService;
parent::__construct();
}
/**
* Export a book as a PDF file.
* @param string $bookSlug
* @return mixed
* @throws NotFoundException
* @throws Throwable
*/
public function pdf(string $bookSlug)
{
$book = $this->entityRepo->getBySlug('book', $bookSlug);
$pdfContent = $this->exportService->bookToPdf($book);
return $this->downloadResponse($pdfContent, $bookSlug . '.pdf');
}
/**
* Export a book as a contained HTML file.
* @param string $bookSlug
* @return mixed
* @throws NotFoundException
* @throws Throwable
*/
public function html(string $bookSlug)
{
$book = $this->entityRepo->getBySlug('book', $bookSlug);
$htmlContent = $this->exportService->bookToContainedHtml($book);
return $this->downloadResponse($htmlContent, $bookSlug . '.html');
}
/**
* Export a book as a plain text file.
* @param $bookSlug
* @return mixed
* @throws NotFoundException
*/
public function plainText(string $bookSlug)
{
$book = $this->entityRepo->getBySlug('book', $bookSlug);
$textContent = $this->exportService->bookToPlainText($book);
return $this->downloadResponse($textContent, $bookSlug . '.txt');
}
}

View File

@ -13,19 +13,16 @@ class ChapterController extends Controller
protected $userRepo;
protected $entityRepo;
protected $exportService;
/**
* ChapterController constructor.
* @param EntityRepo $entityRepo
* @param UserRepo $userRepo
* @param \BookStack\Entities\ExportService $exportService
*/
public function __construct(EntityRepo $entityRepo, UserRepo $userRepo, ExportService $exportService)
public function __construct(EntityRepo $entityRepo, UserRepo $userRepo)
{
$this->entityRepo = $entityRepo;
$this->userRepo = $userRepo;
$this->exportService = $exportService;
parent::__construct();
}
@ -246,43 +243,4 @@ class ChapterController extends Controller
session()->flash('success', trans('entities.chapters_permissions_success'));
return redirect($chapter->getUrl());
}
/**
* Exports a chapter to pdf .
* @param string $bookSlug
* @param string $chapterSlug
* @return \Illuminate\Http\Response
*/
public function exportPdf($bookSlug, $chapterSlug)
{
$chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
$pdfContent = $this->exportService->chapterToPdf($chapter);
return $this->downloadResponse($pdfContent, $chapterSlug . '.pdf');
}
/**
* Export a chapter to a self-contained HTML file.
* @param string $bookSlug
* @param string $chapterSlug
* @return \Illuminate\Http\Response
*/
public function exportHtml($bookSlug, $chapterSlug)
{
$chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
$containedHtml = $this->exportService->chapterToContainedHtml($chapter);
return $this->downloadResponse($containedHtml, $chapterSlug . '.html');
}
/**
* Export a chapter to a simple plaintext .txt file.
* @param string $bookSlug
* @param string $chapterSlug
* @return \Illuminate\Http\Response
*/
public function exportPlainText($bookSlug, $chapterSlug)
{
$chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
$chapterText = $this->exportService->chapterToPlainText($chapter);
return $this->downloadResponse($chapterText, $chapterSlug . '.txt');
}
}

View File

@ -0,0 +1,78 @@
<?php
namespace BookStack\Http\Controllers;
use BookStack\Entities\ExportService;
use BookStack\Entities\Repos\EntityRepo;
use BookStack\Exceptions\NotFoundException;
use Illuminate\Http\Response;
use Throwable;
class ChapterExportController extends Controller
{
/**
* @var EntityRepo
*/
protected $entityRepo;
/**
* @var ExportService
*/
protected $exportService;
/**
* ChapterExportController constructor.
* @param EntityRepo $entityRepo
* @param ExportService $exportService
*/
public function __construct(EntityRepo $entityRepo, ExportService $exportService)
{
$this->entityRepo = $entityRepo;
$this->exportService = $exportService;
parent::__construct();
}
/**
* Exports a chapter to pdf .
* @param string $bookSlug
* @param string $chapterSlug
* @return Response
* @throws NotFoundException
* @throws Throwable
*/
public function pdf(string $bookSlug, string $chapterSlug)
{
$chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
$pdfContent = $this->exportService->chapterToPdf($chapter);
return $this->downloadResponse($pdfContent, $chapterSlug . '.pdf');
}
/**
* Export a chapter to a self-contained HTML file.
* @param string $bookSlug
* @param string $chapterSlug
* @return Response
* @throws NotFoundException
* @throws Throwable
*/
public function html(string $bookSlug, string $chapterSlug)
{
$chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
$containedHtml = $this->exportService->chapterToContainedHtml($chapter);
return $this->downloadResponse($containedHtml, $chapterSlug . '.html');
}
/**
* Export a chapter to a simple plaintext .txt file.
* @param string $bookSlug
* @param string $chapterSlug
* @return Response
* @throws NotFoundException
*/
public function plainText(string $bookSlug, string $chapterSlug)
{
$chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
$chapterText = $this->exportService->chapterToPlainText($chapter);
return $this->downloadResponse($chapterText, $chapterSlug . '.txt');
}
}

View File

@ -2,32 +2,34 @@
use Activity;
use BookStack\Auth\UserRepo;
use BookStack\Entities\Repos\EntityRepo;
use BookStack\Entities\ExportService;
use BookStack\Entities\Repos\PageRepo;
use BookStack\Exceptions\NotFoundException;
use Exception;
use GatherContent\Htmldiff\Htmldiff;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Redirector;
use Illuminate\View\View;
use Throwable;
use Views;
class PageController extends Controller
{
protected $pageRepo;
protected $exportService;
protected $userRepo;
/**
* PageController constructor.
* @param \BookStack\Entities\Repos\PageRepo $pageRepo
* @param \BookStack\Entities\ExportService $exportService
* @param PageRepo $pageRepo
* @param UserRepo $userRepo
*/
public function __construct(PageRepo $pageRepo, ExportService $exportService, UserRepo $userRepo)
public function __construct(PageRepo $pageRepo, UserRepo $userRepo)
{
$this->pageRepo = $pageRepo;
$this->exportService = $exportService;
$this->userRepo = $userRepo;
parent::__construct();
}
@ -101,7 +103,7 @@ class PageController extends Controller
* Show form to continue editing a draft page.
* @param string $bookSlug
* @param int $pageId
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
* @return Factory|View
*/
public function editDraft($bookSlug, $pageId)
{
@ -199,7 +201,7 @@ class PageController extends Controller
/**
* Get page from an ajax request.
* @param int $pageId
* @return \Illuminate\Http\JsonResponse
* @return JsonResponse
*/
public function getPageAjax($pageId)
{
@ -276,7 +278,7 @@ class PageController extends Controller
* Save a draft update as a revision.
* @param Request $request
* @param int $pageId
* @return \Illuminate\Http\JsonResponse
* @return JsonResponse
*/
public function saveDraft(Request $request, $pageId)
{
@ -304,7 +306,7 @@ class PageController extends Controller
* Redirect from a special link url which
* uses the page id rather than the name.
* @param int $pageId
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
* @return RedirectResponse|Redirector
*/
public function redirectFromLink($pageId)
{
@ -316,7 +318,7 @@ class PageController extends Controller
* Show the deletion page for the specified page.
* @param string $bookSlug
* @param string $pageSlug
* @return \Illuminate\View\View
* @return View
*/
public function showDelete($bookSlug, $pageSlug)
{
@ -331,7 +333,7 @@ class PageController extends Controller
* Show the deletion page for the specified page.
* @param string $bookSlug
* @param int $pageId
* @return \Illuminate\View\View
* @return View
* @throws NotFoundException
*/
public function showDeleteDraft($bookSlug, $pageId)
@ -382,7 +384,7 @@ class PageController extends Controller
* Shows the last revisions for this page.
* @param string $bookSlug
* @param string $pageSlug
* @return \Illuminate\View\View
* @return View
* @throws NotFoundException
*/
public function showRevisions($bookSlug, $pageSlug)
@ -397,7 +399,7 @@ class PageController extends Controller
* @param string $bookSlug
* @param string $pageSlug
* @param int $revisionId
* @return \Illuminate\View\View
* @return View
*/
public function showRevision($bookSlug, $pageSlug, $revisionId)
{
@ -423,7 +425,7 @@ class PageController extends Controller
* @param string $bookSlug
* @param string $pageSlug
* @param int $revisionId
* @return \Illuminate\View\View
* @return View
*/
public function showRevisionChanges($bookSlug, $pageSlug, $revisionId)
{
@ -453,7 +455,7 @@ class PageController extends Controller
* @param string $bookSlug
* @param string $pageSlug
* @param int $revisionId
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
* @return RedirectResponse|Redirector
*/
public function restoreRevision($bookSlug, $pageSlug, $revisionId)
{
@ -470,9 +472,9 @@ class PageController extends Controller
* @param string $bookSlug
* @param string $pageSlug
* @param int $revId
* @throws NotFoundException
* @return RedirectResponse|Redirector
*@throws BadRequestException
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
* @throws NotFoundException
*/
public function destroyRevision($bookSlug, $pageSlug, $revId)
{
@ -498,51 +500,9 @@ class PageController extends Controller
return redirect($page->getUrl('/revisions'));
}
/**
* Exports a page to a PDF.
* https://github.com/barryvdh/laravel-dompdf
* @param string $bookSlug
* @param string $pageSlug
* @return \Illuminate\Http\Response
*/
public function exportPdf($bookSlug, $pageSlug)
{
$page = $this->pageRepo->getPageBySlug($pageSlug, $bookSlug);
$page->html = $this->pageRepo->renderPage($page);
$pdfContent = $this->exportService->pageToPdf($page);
return $this->downloadResponse($pdfContent, $pageSlug . '.pdf');
}
/**
* Export a page to a self-contained HTML file.
* @param string $bookSlug
* @param string $pageSlug
* @return \Illuminate\Http\Response
*/
public function exportHtml($bookSlug, $pageSlug)
{
$page = $this->pageRepo->getPageBySlug($pageSlug, $bookSlug);
$page->html = $this->pageRepo->renderPage($page);
$containedHtml = $this->exportService->pageToContainedHtml($page);
return $this->downloadResponse($containedHtml, $pageSlug . '.html');
}
/**
* Export a page to a simple plaintext .txt file.
* @param string $bookSlug
* @param string $pageSlug
* @return \Illuminate\Http\Response
*/
public function exportPlainText($bookSlug, $pageSlug)
{
$page = $this->pageRepo->getPageBySlug($pageSlug, $bookSlug);
$pageText = $this->exportService->pageToPlainText($page);
return $this->downloadResponse($pageText, $pageSlug . '.txt');
}
/**
* Show a listing of recently created pages
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
* @return Factory|View
*/
public function showRecentlyUpdated()
{
@ -579,7 +539,7 @@ class PageController extends Controller
* @param string $pageSlug
* @return mixed
* @throws NotFoundException
* @throws \Throwable
* @throws Throwable
*/
public function move(Request $request, string $bookSlug, string $pageSlug)
{
@ -599,7 +559,7 @@ class PageController extends Controller
try {
$parent = $this->pageRepo->getById($entityType, $entityId);
} catch (\Exception $e) {
} catch (Exception $e) {
session()->flash(trans('entities.selected_book_chapter_not_found'));
return redirect()->back();
}
@ -638,7 +598,7 @@ class PageController extends Controller
* @param string $pageSlug
* @return mixed
* @throws NotFoundException
* @throws \Throwable
* @throws Throwable
*/
public function copy(Request $request, string $bookSlug, string $pageSlug)
{
@ -655,7 +615,7 @@ class PageController extends Controller
try {
$parent = $this->pageRepo->getById($entityType, $entityId);
} catch (\Exception $e) {
} catch (Exception $e) {
session()->flash(trans('entities.selected_book_chapter_not_found'));
return redirect()->back();
}
@ -675,7 +635,7 @@ class PageController extends Controller
* Show the Permissions view.
* @param string $bookSlug
* @param string $pageSlug
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
* @return Factory|View
* @throws NotFoundException
*/
public function showPermissions($bookSlug, $pageSlug)
@ -694,9 +654,9 @@ class PageController extends Controller
* @param string $bookSlug
* @param string $pageSlug
* @param Request $request
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
* @return RedirectResponse|Redirector
* @throws NotFoundException
* @throws \Throwable
* @throws Throwable
*/
public function permissions(Request $request, string $bookSlug, string $pageSlug)
{

View File

@ -0,0 +1,81 @@
<?php
namespace BookStack\Http\Controllers;
use BookStack\Entities\ExportService;
use BookStack\Entities\Repos\PageRepo;
use BookStack\Exceptions\NotFoundException;
use Illuminate\Http\Response;
use Throwable;
class PageExportController extends Controller
{
/**
* @var PageRepo
*/
protected $pageRepo;
/**
* @var ExportService
*/
protected $exportService;
/**
* PageExportController constructor.
* @param PageRepo $pageRepo
* @param ExportService $exportService
*/
public function __construct(PageRepo $pageRepo, ExportService $exportService)
{
$this->pageRepo = $pageRepo;
$this->exportService = $exportService;
parent::__construct();
}
/**
* Exports a page to a PDF.
* https://github.com/barryvdh/laravel-dompdf
* @param string $bookSlug
* @param string $pageSlug
* @return Response
* @throws NotFoundException
* @throws Throwable
*/
public function pdf(string $bookSlug, string $pageSlug)
{
$page = $this->pageRepo->getPageBySlug($pageSlug, $bookSlug);
$page->html = $this->pageRepo->renderPage($page);
$pdfContent = $this->exportService->pageToPdf($page);
return $this->downloadResponse($pdfContent, $pageSlug . '.pdf');
}
/**
* Export a page to a self-contained HTML file.
* @param string $bookSlug
* @param string $pageSlug
* @return Response
* @throws NotFoundException
* @throws Throwable
*/
public function html(string $bookSlug, string $pageSlug)
{
$page = $this->pageRepo->getPageBySlug($pageSlug, $bookSlug);
$page->html = $this->pageRepo->renderPage($page);
$containedHtml = $this->exportService->pageToContainedHtml($page);
return $this->downloadResponse($containedHtml, $pageSlug . '.html');
}
/**
* Export a page to a simple plaintext .txt file.
* @param string $bookSlug
* @param string $pageSlug
* @return Response
* @throws NotFoundException
*/
public function plainText(string $bookSlug, string $pageSlug)
{
$page = $this->pageRepo->getPageBySlug($pageSlug, $bookSlug);
$pageText = $this->exportService->pageToPlainText($page);
return $this->downloadResponse($pageText, $pageSlug . '.txt');
}
}

View File

@ -47,9 +47,9 @@ Route::group(['middleware' => 'auth'], function () {
Route::get('/{slug}/delete', 'BookController@showDelete');
Route::get('/{bookSlug}/sort', 'BookController@sort');
Route::put('/{bookSlug}/sort', 'BookController@saveSort');
Route::get('/{bookSlug}/export/html', 'BookController@exportHtml');
Route::get('/{bookSlug}/export/pdf', 'BookController@exportPdf');
Route::get('/{bookSlug}/export/plaintext', 'BookController@exportPlainText');
Route::get('/{bookSlug}/export/html', 'BookExportController@html');
Route::get('/{bookSlug}/export/pdf', 'BookExportController@pdf');
Route::get('/{bookSlug}/export/plaintext', 'BookExportController@plainText');
// Pages
Route::get('/{bookSlug}/create-page', 'PageController@create');
@ -57,9 +57,9 @@ Route::group(['middleware' => 'auth'], function () {
Route::get('/{bookSlug}/draft/{pageId}', 'PageController@editDraft');
Route::post('/{bookSlug}/draft/{pageId}', 'PageController@store');
Route::get('/{bookSlug}/page/{pageSlug}', 'PageController@show');
Route::get('/{bookSlug}/page/{pageSlug}/export/pdf', 'PageController@exportPdf');
Route::get('/{bookSlug}/page/{pageSlug}/export/html', 'PageController@exportHtml');
Route::get('/{bookSlug}/page/{pageSlug}/export/plaintext', 'PageController@exportPlainText');
Route::get('/{bookSlug}/page/{pageSlug}/export/pdf', 'PageExportController@pdf');
Route::get('/{bookSlug}/page/{pageSlug}/export/html', 'PageExportController@html');
Route::get('/{bookSlug}/page/{pageSlug}/export/plaintext', 'PageExportController@plainText');
Route::get('/{bookSlug}/page/{pageSlug}/edit', 'PageController@edit');
Route::get('/{bookSlug}/page/{pageSlug}/move', 'PageController@showMove');
Route::put('/{bookSlug}/page/{pageSlug}/move', 'PageController@move');
@ -91,9 +91,9 @@ Route::group(['middleware' => 'auth'], function () {
Route::put('/{bookSlug}/chapter/{chapterSlug}/move', 'ChapterController@move');
Route::get('/{bookSlug}/chapter/{chapterSlug}/edit', 'ChapterController@edit');
Route::get('/{bookSlug}/chapter/{chapterSlug}/permissions', 'ChapterController@showPermissions');
Route::get('/{bookSlug}/chapter/{chapterSlug}/export/pdf', 'ChapterController@exportPdf');
Route::get('/{bookSlug}/chapter/{chapterSlug}/export/html', 'ChapterController@exportHtml');
Route::get('/{bookSlug}/chapter/{chapterSlug}/export/plaintext', 'ChapterController@exportPlainText');
Route::get('/{bookSlug}/chapter/{chapterSlug}/export/pdf', 'ChapterExportController@pdf');
Route::get('/{bookSlug}/chapter/{chapterSlug}/export/html', 'ChapterExportController@html');
Route::get('/{bookSlug}/chapter/{chapterSlug}/export/plaintext', 'ChapterExportController@plainText');
Route::put('/{bookSlug}/chapter/{chapterSlug}/permissions', 'ChapterController@permissions');
Route::get('/{bookSlug}/chapter/{chapterSlug}/delete', 'ChapterController@showDelete');
Route::delete('/{bookSlug}/chapter/{chapterSlug}', 'ChapterController@destroy');