2021-06-26 15:23:15 +00:00
|
|
|
<?php
|
|
|
|
|
2024-10-15 16:14:11 +01:00
|
|
|
namespace BookStack\Exports\Controllers;
|
2019-09-15 22:33:27 +01:00
|
|
|
|
2024-02-05 15:59:20 +00:00
|
|
|
use BookStack\Entities\Queries\ChapterQueries;
|
2019-09-15 22:33:27 +01:00
|
|
|
use BookStack\Exceptions\NotFoundException;
|
2024-10-15 16:14:11 +01:00
|
|
|
use BookStack\Exports\ExportFormatter;
|
2024-10-23 15:59:58 +01:00
|
|
|
use BookStack\Exports\ZipExports\ZipExportBuilder;
|
2023-05-18 20:53:39 +01:00
|
|
|
use BookStack\Http\Controller;
|
2019-09-15 22:33:27 +01:00
|
|
|
use Throwable;
|
|
|
|
|
|
|
|
class ChapterExportController extends Controller
|
|
|
|
{
|
2024-02-05 15:59:20 +00:00
|
|
|
public function __construct(
|
|
|
|
protected ChapterQueries $queries,
|
|
|
|
protected ExportFormatter $exportFormatter,
|
|
|
|
) {
|
2021-08-28 21:48:17 +01:00
|
|
|
$this->middleware('can:content-export');
|
2019-09-15 22:33:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-05 12:55:01 +01:00
|
|
|
* Exports a chapter to pdf.
|
2021-06-26 15:23:15 +00:00
|
|
|
*
|
2019-09-15 22:33:27 +01:00
|
|
|
* @throws NotFoundException
|
|
|
|
* @throws Throwable
|
|
|
|
*/
|
|
|
|
public function pdf(string $bookSlug, string $chapterSlug)
|
|
|
|
{
|
2024-02-05 17:35:49 +00:00
|
|
|
$chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
|
2020-11-22 01:26:14 +00:00
|
|
|
$pdfContent = $this->exportFormatter->chapterToPdf($chapter);
|
2021-06-26 15:23:15 +00:00
|
|
|
|
2022-06-08 23:50:42 +01:00
|
|
|
return $this->download()->directly($pdfContent, $chapterSlug . '.pdf');
|
2019-09-15 22:33:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Export a chapter to a self-contained HTML file.
|
2021-06-26 15:23:15 +00:00
|
|
|
*
|
2019-09-15 22:33:27 +01:00
|
|
|
* @throws NotFoundException
|
|
|
|
* @throws Throwable
|
|
|
|
*/
|
|
|
|
public function html(string $bookSlug, string $chapterSlug)
|
|
|
|
{
|
2024-02-05 17:35:49 +00:00
|
|
|
$chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
|
2020-11-22 01:26:14 +00:00
|
|
|
$containedHtml = $this->exportFormatter->chapterToContainedHtml($chapter);
|
2021-06-26 15:23:15 +00:00
|
|
|
|
2022-06-08 23:50:42 +01:00
|
|
|
return $this->download()->directly($containedHtml, $chapterSlug . '.html');
|
2019-09-15 22:33:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Export a chapter to a simple plaintext .txt file.
|
2021-06-26 15:23:15 +00:00
|
|
|
*
|
2019-09-15 22:33:27 +01:00
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
|
|
|
public function plainText(string $bookSlug, string $chapterSlug)
|
|
|
|
{
|
2024-02-05 17:35:49 +00:00
|
|
|
$chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
|
2020-11-22 01:26:14 +00:00
|
|
|
$chapterText = $this->exportFormatter->chapterToPlainText($chapter);
|
2021-06-26 15:23:15 +00:00
|
|
|
|
2022-06-08 23:50:42 +01:00
|
|
|
return $this->download()->directly($chapterText, $chapterSlug . '.txt');
|
2019-09-15 22:33:27 +01:00
|
|
|
}
|
2020-05-12 21:12:26 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Export a chapter to a simple markdown file.
|
2021-06-26 15:23:15 +00:00
|
|
|
*
|
2020-05-12 21:12:26 -07:00
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
|
|
|
public function markdown(string $bookSlug, string $chapterSlug)
|
|
|
|
{
|
2024-02-05 17:35:49 +00:00
|
|
|
$chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
|
2021-06-22 21:02:18 +01:00
|
|
|
$chapterText = $this->exportFormatter->chapterToMarkdown($chapter);
|
2021-06-26 15:23:15 +00:00
|
|
|
|
2022-06-08 23:50:42 +01:00
|
|
|
return $this->download()->directly($chapterText, $chapterSlug . '.md');
|
2020-05-12 21:12:26 -07:00
|
|
|
}
|
2024-10-23 15:59:58 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Export a book to a contained ZIP export file.
|
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
|
|
|
public function zip(string $bookSlug, string $chapterSlug, ZipExportBuilder $builder)
|
|
|
|
{
|
|
|
|
$chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
|
|
|
|
$zip = $builder->buildForChapter($chapter);
|
|
|
|
|
|
|
|
return $this->download()->streamedDirectly(fopen($zip, 'r'), $chapterSlug . '.zip', filesize($zip));
|
|
|
|
}
|
2019-09-15 22:33:27 +01:00
|
|
|
}
|