Skip to content

Commit d15eb12

Browse files
committed
API: Initial review pass of zip import/export endpoints
Review of #5592
1 parent 3626a22 commit d15eb12

File tree

5 files changed

+55
-69
lines changed

5 files changed

+55
-69
lines changed

app/Exports/Controllers/BookExportApiController.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,14 @@ public function exportMarkdown(int $id)
6565
return $this->download()->directly($markdown, $book->slug . '.md');
6666
}
6767

68-
6968
/**
7069
* Export a book to a contained ZIP export file.
71-
* @throws NotFoundException
7270
*/
7371
public function exportZip(int $id, ZipExportBuilder $builder)
7472
{
7573
$book = $this->queries->findVisibleByIdOrFail($id);
76-
$bookName= $book->getShortName();
77-
7874
$zip = $builder->buildForBook($book);
7975

80-
return $this->download()->streamedFileDirectly($zip, $bookName . '.zip', filesize($zip), true);
76+
return $this->download()->streamedFileDirectly($zip, $book->slug . '.zip', true);
8177
}
82-
}
78+
}

app/Exports/Controllers/ChapterExportApiController.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,8 @@ public function exportMarkdown(int $id)
6868
public function exportZip(int $id, ZipExportBuilder $builder)
6969
{
7070
$chapter = $this->queries->findVisibleByIdOrFail($id);
71-
$chapterName= $chapter->getShortName();
7271
$zip = $builder->buildForChapter($chapter);
7372

74-
return $this->download()->streamedFileDirectly($zip, $chapterName . '.zip', filesize($zip), true);
73+
return $this->download()->streamedFileDirectly($zip, $chapter->slug . '.zip', true);
7574
}
76-
}
75+
}

app/Exports/Controllers/ImportApiController.php

Lines changed: 43 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
use BookStack\Exceptions\ZipImportException;
88
use BookStack\Exceptions\ZipValidationException;
99
use BookStack\Exports\ImportRepo;
10-
use BookStack\Http\Controller;
10+
use BookStack\Http\ApiController;
1111
use BookStack\Uploads\AttachmentService;
1212
use Illuminate\Http\Request;
1313
use Illuminate\Http\JsonResponse;
14+
use Illuminate\Http\Response;
1415

15-
class ImportApiController extends Controller
16+
class ImportApiController extends ApiController
1617
{
1718
public function __construct(
1819
protected ImportRepo $imports,
@@ -21,101 +22,94 @@ public function __construct(
2122
}
2223

2324
/**
24-
* List existing imports visible to the user.
25+
* List existing ZIP imports visible to the user.
2526
*/
2627
public function list(): JsonResponse
2728
{
28-
$imports = $this->imports->getVisibleImports();
29+
$imports = $this->imports->getVisibleImports()->all();
2930

30-
return response()->json([
31-
'status' => 'success',
32-
'imports' => $imports,
33-
]);
31+
return response()->json($imports);
3432
}
3533

3634
/**
37-
* Upload, validate and store an import file.
35+
* Upload, validate and store a ZIP import file.
36+
* This does not run the import. That is performed via a separate endpoint.
3837
*/
3938
public function upload(Request $request): JsonResponse
4039
{
41-
$this->validate($request, [
42-
'file' => ['required', ...AttachmentService::getFileValidationRules()]
43-
]);
40+
$this->validate($request, $this->rules()['upload']);
4441

4542
$file = $request->file('file');
4643

4744
try {
4845
$import = $this->imports->storeFromUpload($file);
4946
} catch (ZipValidationException $exception) {
50-
return response()->json([
51-
'status' => 'error',
52-
'message' => 'Validation failed',
53-
'errors' => $exception->errors,
54-
], 422);
47+
$message = "ZIP upload failed with the following validation errors: \n" . implode("\n", $exception->errors);
48+
return $this->jsonError($message, 422);
5549
}
5650

57-
return response()->json([
58-
'status' => 'success',
59-
'import' => $import,
60-
], 201);
51+
return response()->json($import);
6152
}
6253

6354
/**
64-
* Show details of a pending import.
55+
* Read details of a pending ZIP import.
6556
*/
6657
public function read(int $id): JsonResponse
6758
{
6859
$import = $this->imports->findVisible($id);
6960

70-
return response()->json([
71-
'status' => 'success',
72-
'import' => $import,
73-
'data' => $import->decodeMetadata(),
74-
]);
61+
return response()->json($import);
7562
}
7663

7764
/**
78-
* Run the import process.
65+
* Run the import process for an uploaded ZIP import.
66+
* The parent_id and parent_type parameters are required when the import type is 'chapter' or 'page'.
67+
* On success, returns the imported item.
7968
*/
80-
public function create(int $id, Request $request): JsonResponse
69+
public function run(int $id, Request $request): JsonResponse
8170
{
8271
$import = $this->imports->findVisible($id);
8372
$parent = null;
73+
$rules = $this->rules()['run'];
8474

8575
if ($import->type === 'page' || $import->type === 'chapter') {
86-
$data = $this->validate($request, [
87-
'parent' => ['required', 'string'],
88-
]);
89-
$parent = $data['parent'];
76+
$rules['parent_type'][] = 'required';
77+
$rules['parent_id'][] = 'required';
78+
$data = $this->validate($request, $rules);
79+
$parent = "{$data['parent_type']}:{$data['parent_id']}";
9080
}
9181

9282
try {
9383
$entity = $this->imports->runImport($import, $parent);
9484
} catch (ZipImportException $exception) {
95-
return response()->json([
96-
'status' => 'error',
97-
'message' => 'Import failed',
98-
'errors' => $exception->errors,
99-
], 500);
85+
$message = "ZIP import failed with the following errors: \n" . implode("\n", $exception->errors);
86+
return $this->jsonError($message);
10087
}
10188

102-
return response()->json([
103-
'status' => 'success',
104-
'entity' => $entity,
105-
]);
89+
return response()->json($entity);
10690
}
10791

10892
/**
109-
* Delete a pending import.
93+
* Delete a pending ZIP import.
11094
*/
111-
public function delete(int $id): JsonResponse
95+
public function delete(int $id): Response
11296
{
11397
$import = $this->imports->findVisible($id);
11498
$this->imports->deleteImport($import);
11599

116-
return response()->json([
117-
'status' => 'success',
118-
'message' => 'Import deleted successfully',
119-
]);
100+
return response('', 204);
120101
}
121-
}
102+
103+
protected function rules(): array
104+
{
105+
return [
106+
'upload' => [
107+
'file' => ['required', ...AttachmentService::getFileValidationRules()],
108+
],
109+
'run' => [
110+
'parent_type' => ['string', 'in:book,chapter'],
111+
'parent_id' => ['int'],
112+
],
113+
];
114+
}
115+
}

app/Exports/Controllers/PageExportApiController.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,11 @@ public function exportMarkdown(int $id)
6565
return $this->download()->directly($markdown, $page->slug . '.md');
6666
}
6767

68-
69-
7068
public function exportZip(int $id, ZipExportBuilder $builder)
7169
{
7270
$page = $this->queries->findVisibleByIdOrFail($id);
73-
$pageSlug = $page->slug;
7471
$zip = $builder->buildForPage($page);
7572

76-
return $this->download()->streamedFileDirectly($zip, $pageSlug . '.zip', filesize($zip), true);
73+
return $this->download()->streamedFileDirectly($zip, $page->slug . '.zip', true);
7774
}
78-
}
75+
}

routes/api.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@
8888
Route::put('roles/{id}', [RoleApiController::class, 'update']);
8989
Route::delete('roles/{id}', [RoleApiController::class, 'delete']);
9090

91+
Route::get('import', [ExportControllers\ImportApiController::class, 'list']);
92+
Route::post('import', [ExportControllers\ImportApiController::class, 'upload']);
93+
Route::get('import/{id}', [ExportControllers\ImportApiController::class, 'read']);
94+
Route::post('import/{id}', [ExportControllers\ImportApiController::class, 'run']);
95+
Route::delete('import/{id}', [ExportControllers\ImportApiController::class, 'delete']);
96+
9197
Route::get('recycle-bin', [EntityControllers\RecycleBinApiController::class, 'list']);
9298
Route::put('recycle-bin/{deletionId}', [EntityControllers\RecycleBinApiController::class, 'restore']);
9399
Route::delete('recycle-bin/{deletionId}', [EntityControllers\RecycleBinApiController::class, 'destroy']);
@@ -98,9 +104,3 @@
98104
Route::get('audit-log', [AuditLogApiController::class, 'list']);
99105

100106
Route::get('system', [SystemApiController::class, 'read']);
101-
102-
Route::get('import', [ExportControllers\ImportApiController::class, 'list']);
103-
Route::post('import', [ExportControllers\ImportApiController::class, 'upload']);
104-
Route::get('import/{id}', [ExportControllers\ImportApiController::class, 'read']);
105-
Route::post('import/{id}/create', [ExportControllers\ImportApiController::class, 'create']);
106-
Route::delete('import/{id}', [ExportControllers\ImportApiController::class, 'destroy']);

0 commit comments

Comments
 (0)