Skip to content

API: Add endpoints for import/export #5592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion app/Exports/Controllers/BookExportApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use BookStack\Entities\Queries\BookQueries;
use BookStack\Exports\ExportFormatter;
use BookStack\Exports\ZipExports\ZipExportBuilder;
use BookStack\Http\ApiController;
use Throwable;

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

return $this->download()->directly($markdown, $book->slug . '.md');
}
}


/**
* Export a book to a contained ZIP export file.
* @throws NotFoundException
*/
public function exportZip(int $id, ZipExportBuilder $builder)
{
$book = $this->queries->findVisibleByIdOrFail($id);
$bookName= $book->getShortName();

$zip = $builder->buildForBook($book);

return $this->download()->streamedFileDirectly($zip, $bookName . '.zip', filesize($zip), true);
}
}
12 changes: 11 additions & 1 deletion app/Exports/Controllers/ChapterExportApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use BookStack\Entities\Queries\ChapterQueries;
use BookStack\Exports\ExportFormatter;
use BookStack\Exports\ZipExports\ZipExportBuilder;
use BookStack\Http\ApiController;
use Throwable;

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

return $this->download()->directly($markdown, $chapter->slug . '.md');
}
}

public function exportZip(int $id, ZipExportBuilder $builder)
{
$chapter = $this->queries->findVisibleByIdOrFail($id);
$chapterName= $chapter->getShortName();
$zip = $builder->buildForChapter($chapter);

return $this->download()->streamedFileDirectly($zip, $chapterName . '.zip', filesize($zip), true);
}
}
121 changes: 121 additions & 0 deletions app/Exports/Controllers/ImportApiController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

declare(strict_types=1);

namespace BookStack\Exports\Controllers;

use BookStack\Exceptions\ZipImportException;
use BookStack\Exceptions\ZipValidationException;
use BookStack\Exports\ImportRepo;
use BookStack\Http\Controller;
use BookStack\Uploads\AttachmentService;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;

class ImportApiController extends Controller
{
public function __construct(
protected ImportRepo $imports,
) {
$this->middleware('can:content-import');
}

/**
* List existing imports visible to the user.
*/
public function list(): JsonResponse
{
$imports = $this->imports->getVisibleImports();

return response()->json([
'status' => 'success',
'imports' => $imports,
]);
}

/**
* Upload, validate and store an import file.
*/
public function upload(Request $request): JsonResponse
{
$this->validate($request, [
'file' => ['required', ...AttachmentService::getFileValidationRules()]
]);

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

try {
$import = $this->imports->storeFromUpload($file);
} catch (ZipValidationException $exception) {
return response()->json([
'status' => 'error',
'message' => 'Validation failed',
'errors' => $exception->errors,
], 422);
}

return response()->json([
'status' => 'success',
'import' => $import,
], 201);
}

/**
* Show details of a pending import.
*/
public function read(int $id): JsonResponse
{
$import = $this->imports->findVisible($id);

return response()->json([
'status' => 'success',
'import' => $import,
'data' => $import->decodeMetadata(),
]);
}

/**
* Run the import process.
*/
public function create(int $id, Request $request): JsonResponse
{
$import = $this->imports->findVisible($id);
$parent = null;

if ($import->type === 'page' || $import->type === 'chapter') {
$data = $this->validate($request, [
'parent' => ['required', 'string'],
]);
$parent = $data['parent'];
}

try {
$entity = $this->imports->runImport($import, $parent);
} catch (ZipImportException $exception) {
return response()->json([
'status' => 'error',
'message' => 'Import failed',
'errors' => $exception->errors,
], 500);
}

return response()->json([
'status' => 'success',
'entity' => $entity,
]);
}

/**
* Delete a pending import.
*/
public function delete(int $id): JsonResponse
{
$import = $this->imports->findVisible($id);
$this->imports->deleteImport($import);

return response()->json([
'status' => 'success',
'message' => 'Import deleted successfully',
]);
}
}
14 changes: 13 additions & 1 deletion app/Exports/Controllers/PageExportApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use BookStack\Entities\Queries\PageQueries;
use BookStack\Exports\ExportFormatter;
use BookStack\Exports\ZipExports\ZipExportBuilder;
use BookStack\Http\ApiController;
use Throwable;

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

return $this->download()->directly($markdown, $page->slug . '.md');
}
}



public function exportZip(int $id, ZipExportBuilder $builder)
{
$page = $this->queries->findVisibleByIdOrFail($id);
$pageSlug = $page->slug;
$zip = $builder->buildForPage($page);

return $this->download()->streamedFileDirectly($zip, $pageSlug . '.zip', filesize($zip), true);
}
}
9 changes: 9 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
Route::get('books/{id}/export/pdf', [ExportControllers\BookExportApiController::class, 'exportPdf']);
Route::get('books/{id}/export/plaintext', [ExportControllers\BookExportApiController::class, 'exportPlainText']);
Route::get('books/{id}/export/markdown', [ExportControllers\BookExportApiController::class, 'exportMarkdown']);
Route::get('books/{id}/export/zip', [ExportControllers\BookExportApiController::class, 'exportZip']);

Route::get('chapters', [EntityControllers\ChapterApiController::class, 'list']);
Route::post('chapters', [EntityControllers\ChapterApiController::class, 'create']);
Expand All @@ -46,6 +47,7 @@
Route::get('chapters/{id}/export/pdf', [ExportControllers\ChapterExportApiController::class, 'exportPdf']);
Route::get('chapters/{id}/export/plaintext', [ExportControllers\ChapterExportApiController::class, 'exportPlainText']);
Route::get('chapters/{id}/export/markdown', [ExportControllers\ChapterExportApiController::class, 'exportMarkdown']);
Route::get('chapters/{id}/export/zip', [ExportControllers\ChapterExportApiController::class, 'exportZip']);

Route::get('pages', [EntityControllers\PageApiController::class, 'list']);
Route::post('pages', [EntityControllers\PageApiController::class, 'create']);
Expand All @@ -57,6 +59,7 @@
Route::get('pages/{id}/export/pdf', [ExportControllers\PageExportApiController::class, 'exportPdf']);
Route::get('pages/{id}/export/plaintext', [ExportControllers\PageExportApiController::class, 'exportPlainText']);
Route::get('pages/{id}/export/markdown', [ExportControllers\PageExportApiController::class, 'exportMarkdown']);
Route::get('pages/{id}/export/zip', [ExportControllers\PageExportApiController::class, 'exportZip']);

Route::get('image-gallery', [ImageGalleryApiController::class, 'list']);
Route::post('image-gallery', [ImageGalleryApiController::class, 'create']);
Expand Down Expand Up @@ -92,3 +95,9 @@
Route::put('content-permissions/{contentType}/{contentId}', [ContentPermissionApiController::class, 'update']);

Route::get('audit-log', [AuditLogApiController::class, 'list']);

Route::get('import', [ExportControllers\ImportApiController::class, 'list']);
Route::post('import', [ExportControllers\ImportApiController::class, 'upload']);
Route::get('import/{id}', [ExportControllers\ImportApiController::class, 'read']);
Route::post('import/{id}/create', [ExportControllers\ImportApiController::class, 'create']);
Route::delete('import/{id}', [ExportControllers\ImportApiController::class, 'destroy']);