Skip to content

Commit c61ce8d

Browse files
committed
Implement functionality to export a book, along with its pages and chapters, as a ZIP file.
1 parent fa566f1 commit c61ce8d

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

app/Exports/Controllers/BookExportApiController.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use BookStack\Entities\Queries\BookQueries;
66
use BookStack\Exports\ExportFormatter;
7+
use BookStack\Exports\ZipExports\ZipExportBuilder;
78
use BookStack\Http\ApiController;
89
use Throwable;
910

@@ -63,4 +64,19 @@ public function exportMarkdown(int $id)
6364

6465
return $this->download()->directly($markdown, $book->slug . '.md');
6566
}
66-
}
67+
68+
69+
/**
70+
* Export a book to a contained ZIP export file.
71+
* @throws NotFoundException
72+
*/
73+
public function exportZip(int $id, ZipExportBuilder $builder)
74+
{
75+
$book = $this->queries->findVisibleByIdOrFail($id);
76+
$bookName= $book->getShortName();
77+
78+
$zip = $builder->buildForBook($book);
79+
80+
return $this->download()->streamedFileDirectly($zip, $bookName . '.zip', filesize($zip), true);
81+
}
82+
}

app/Exports/Controllers/ChapterExportApiController.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use BookStack\Entities\Queries\ChapterQueries;
66
use BookStack\Exports\ExportFormatter;
7+
use BookStack\Exports\ZipExports\ZipExportBuilder;
78
use BookStack\Http\ApiController;
89
use Throwable;
910

@@ -63,4 +64,13 @@ public function exportMarkdown(int $id)
6364

6465
return $this->download()->directly($markdown, $chapter->slug . '.md');
6566
}
66-
}
67+
68+
public function exportZip(int $id, ZipExportBuilder $builder)
69+
{
70+
$chapter = $this->queries->findVisibleByIdOrFail($id);
71+
$chapterName= $chapter->getShortName();
72+
$zip = $builder->buildForChapter($chapter);
73+
74+
return $this->download()->streamedFileDirectly($zip, $chapterName . '.zip', filesize($zip), true);
75+
}
76+
}

app/Exports/Controllers/PageExportApiController.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use BookStack\Entities\Queries\PageQueries;
66
use BookStack\Exports\ExportFormatter;
7+
use BookStack\Exports\ZipExports\ZipExportBuilder;
78
use BookStack\Http\ApiController;
89
use Throwable;
910

@@ -63,4 +64,15 @@ public function exportMarkdown(int $id)
6364

6465
return $this->download()->directly($markdown, $page->slug . '.md');
6566
}
66-
}
67+
68+
69+
70+
public function exportZip(int $id, ZipExportBuilder $builder)
71+
{
72+
$page = $this->queries->findVisibleByIdOrFail($id);
73+
$pageSlug = $page->slug;
74+
$zip = $builder->buildForPage($page);
75+
76+
return $this->download()->streamedFileDirectly($zip, $pageSlug . '.zip', filesize($zip), true);
77+
}
78+
}

0 commit comments

Comments
 (0)