Skip to content

Commit e62f442

Browse files
authored
Merge pull request #5721 from BookStackApp/zip_export_api_endpoints
API: ZIP Import/Export
2 parents d13abc7 + 32ba3a5 commit e62f442

26 files changed

+733
-224
lines changed

app/Entities/Controllers/ChapterApiController.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99
use BookStack\Exceptions\PermissionsException;
1010
use BookStack\Http\ApiController;
1111
use Exception;
12-
use Illuminate\Database\Eloquent\Relations\HasMany;
1312
use Illuminate\Http\Request;
1413

1514
class ChapterApiController extends ApiController
1615
{
17-
protected $rules = [
16+
protected array $rules = [
1817
'create' => [
1918
'book_id' => ['required', 'integer'],
2019
'name' => ['required', 'string', 'max:255'],

app/Entities/Controllers/PageApiController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
class PageApiController extends ApiController
1414
{
15-
protected $rules = [
15+
protected array $rules = [
1616
'create' => [
1717
'book_id' => ['required_without:chapter_id', 'integer'],
1818
'chapter_id' => ['required_without:book_id', 'integer'],

app/Exports/Controllers/BookExportApiController.php

Lines changed: 12 additions & 0 deletions
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,15 @@ public function exportMarkdown(int $id)
6364

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

app/Exports/Controllers/ChapterExportApiController.php

Lines changed: 9 additions & 0 deletions
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,12 @@ public function exportMarkdown(int $id)
6364

6465
return $this->download()->directly($markdown, $chapter->slug . '.md');
6566
}
67+
68+
public function exportZip(int $id, ZipExportBuilder $builder)
69+
{
70+
$chapter = $this->queries->findVisibleByIdOrFail($id);
71+
$zip = $builder->buildForChapter($chapter);
72+
73+
return $this->download()->streamedFileDirectly($zip, $chapter->slug . '.zip', true);
74+
}
6675
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BookStack\Exports\Controllers;
6+
7+
use BookStack\Exceptions\ZipImportException;
8+
use BookStack\Exceptions\ZipValidationException;
9+
use BookStack\Exports\ImportRepo;
10+
use BookStack\Http\ApiController;
11+
use BookStack\Uploads\AttachmentService;
12+
use Illuminate\Http\Request;
13+
use Illuminate\Http\JsonResponse;
14+
use Illuminate\Http\Response;
15+
16+
class ImportApiController extends ApiController
17+
{
18+
public function __construct(
19+
protected ImportRepo $imports,
20+
) {
21+
$this->middleware('can:content-import');
22+
}
23+
24+
/**
25+
* List existing ZIP imports visible to the user.
26+
* Requires permission to import content.
27+
*/
28+
public function list(): JsonResponse
29+
{
30+
$query = $this->imports->queryVisible();
31+
32+
return $this->apiListingResponse($query, [
33+
'id', 'name', 'size', 'type', 'created_by', 'created_at', 'updated_at'
34+
]);
35+
}
36+
37+
/**
38+
* Start a new import from a ZIP file.
39+
* This does not actually run the import since that is performed via the "run" endpoint.
40+
* This uploads, validates and stores the ZIP file so it's ready to be imported.
41+
*
42+
* This "file" parameter must be a BookStack-compatible ZIP file, and this must be
43+
* sent via a 'multipart/form-data' type request.
44+
*
45+
* Requires permission to import content.
46+
*/
47+
public function create(Request $request): JsonResponse
48+
{
49+
$this->validate($request, $this->rules()['create']);
50+
51+
$file = $request->file('file');
52+
53+
try {
54+
$import = $this->imports->storeFromUpload($file);
55+
} catch (ZipValidationException $exception) {
56+
$message = "ZIP upload failed with the following validation errors: \n" . $this->formatErrors($exception->errors);
57+
return $this->jsonError($message, 422);
58+
}
59+
60+
return response()->json($import);
61+
}
62+
63+
/**
64+
* Read details of a pending ZIP import.
65+
* The "details" property contains high-level metadata regarding the ZIP import content,
66+
* and the structure of this will change depending on import "type".
67+
* Requires permission to import content.
68+
*/
69+
public function read(int $id): JsonResponse
70+
{
71+
$import = $this->imports->findVisible($id);
72+
73+
$import->setAttribute('details', $import->decodeMetadata());
74+
75+
return response()->json($import);
76+
}
77+
78+
/**
79+
* Run the import process for an uploaded ZIP import.
80+
* The "parent_id" and "parent_type" parameters are required when the import type is "chapter" or "page".
81+
* On success, this endpoint returns the imported item.
82+
* Requires permission to import content.
83+
*/
84+
public function run(int $id, Request $request): JsonResponse
85+
{
86+
$import = $this->imports->findVisible($id);
87+
$parent = null;
88+
$rules = $this->rules()['run'];
89+
90+
if ($import->type === 'page' || $import->type === 'chapter') {
91+
$rules['parent_type'][] = 'required';
92+
$rules['parent_id'][] = 'required';
93+
$data = $this->validate($request, $rules);
94+
$parent = "{$data['parent_type']}:{$data['parent_id']}";
95+
}
96+
97+
try {
98+
$entity = $this->imports->runImport($import, $parent);
99+
} catch (ZipImportException $exception) {
100+
$message = "ZIP import failed with the following errors: \n" . $this->formatErrors($exception->errors);
101+
return $this->jsonError($message);
102+
}
103+
104+
return response()->json($entity->withoutRelations());
105+
}
106+
107+
/**
108+
* Delete a pending ZIP import from the system.
109+
* Requires permission to import content.
110+
*/
111+
public function delete(int $id): Response
112+
{
113+
$import = $this->imports->findVisible($id);
114+
$this->imports->deleteImport($import);
115+
116+
return response('', 204);
117+
}
118+
119+
protected function rules(): array
120+
{
121+
return [
122+
'create' => [
123+
'file' => ['required', ...AttachmentService::getFileValidationRules()],
124+
],
125+
'run' => [
126+
'parent_type' => ['string', 'in:book,chapter'],
127+
'parent_id' => ['int'],
128+
],
129+
];
130+
}
131+
132+
protected function formatErrors(array $errors): string
133+
{
134+
$parts = [];
135+
foreach ($errors as $key => $error) {
136+
if (is_string($key)) {
137+
$parts[] = "[{$key}] {$error}";
138+
} else {
139+
$parts[] = $error;
140+
}
141+
}
142+
return implode("\n", $parts);
143+
}
144+
}

app/Exports/Controllers/PageExportApiController.php

Lines changed: 9 additions & 0 deletions
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,12 @@ public function exportMarkdown(int $id)
6364

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

app/Exports/Import.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class Import extends Model implements Loggable
2828
{
2929
use HasFactory;
3030

31+
protected $hidden = ['metadata'];
32+
3133
public function getSizeString(): string
3234
{
3335
$mb = round($this->size / 1000000, 2);

app/Exports/ImportRepo.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use BookStack\Exports\ZipExports\ZipImportRunner;
1818
use BookStack\Facades\Activity;
1919
use BookStack\Uploads\FileStorage;
20+
use Illuminate\Database\Eloquent\Builder;
2021
use Illuminate\Database\Eloquent\Collection;
2122
use Illuminate\Support\Facades\DB;
2223
use Symfony\Component\HttpFoundation\File\UploadedFile;
@@ -34,14 +35,19 @@ public function __construct(
3435
* @return Collection<Import>
3536
*/
3637
public function getVisibleImports(): Collection
38+
{
39+
return $this->queryVisible()->get();
40+
}
41+
42+
public function queryVisible(): Builder
3743
{
3844
$query = Import::query();
3945

4046
if (!userCan('settings-manage')) {
4147
$query->where('created_by', user()->id);
4248
}
4349

44-
return $query->get();
50+
return $query;
4551
}
4652

4753
public function findVisible(int $id): Import

app/Http/ApiController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
abstract class ApiController extends Controller
1010
{
11-
protected $rules = [];
11+
protected array $rules = [];
1212

1313
/**
1414
* Provide a paginated listing JSON response in a standard format

app/Permissions/ContentPermissionApiController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function __construct(
1616
) {
1717
}
1818

19-
protected $rules = [
19+
protected array $rules = [
2020
'update' => [
2121
'owner_id' => ['int'],
2222

0 commit comments

Comments
 (0)