mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-08-06 12:02:45 +03:00
API: Initial review pass of zip import/export endpoints
Review of #5592
This commit is contained in:
@@ -65,18 +65,14 @@ class BookExportApiController extends ApiController
|
|||||||
return $this->download()->directly($markdown, $book->slug . '.md');
|
return $this->download()->directly($markdown, $book->slug . '.md');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Export a book to a contained ZIP export file.
|
* Export a book to a contained ZIP export file.
|
||||||
* @throws NotFoundException
|
|
||||||
*/
|
*/
|
||||||
public function exportZip(int $id, ZipExportBuilder $builder)
|
public function exportZip(int $id, ZipExportBuilder $builder)
|
||||||
{
|
{
|
||||||
$book = $this->queries->findVisibleByIdOrFail($id);
|
$book = $this->queries->findVisibleByIdOrFail($id);
|
||||||
$bookName= $book->getShortName();
|
|
||||||
|
|
||||||
$zip = $builder->buildForBook($book);
|
$zip = $builder->buildForBook($book);
|
||||||
|
|
||||||
return $this->download()->streamedFileDirectly($zip, $bookName . '.zip', filesize($zip), true);
|
return $this->download()->streamedFileDirectly($zip, $book->slug . '.zip', true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -68,9 +68,8 @@ class ChapterExportApiController extends ApiController
|
|||||||
public function exportZip(int $id, ZipExportBuilder $builder)
|
public function exportZip(int $id, ZipExportBuilder $builder)
|
||||||
{
|
{
|
||||||
$chapter = $this->queries->findVisibleByIdOrFail($id);
|
$chapter = $this->queries->findVisibleByIdOrFail($id);
|
||||||
$chapterName= $chapter->getShortName();
|
|
||||||
$zip = $builder->buildForChapter($chapter);
|
$zip = $builder->buildForChapter($chapter);
|
||||||
|
|
||||||
return $this->download()->streamedFileDirectly($zip, $chapterName . '.zip', filesize($zip), true);
|
return $this->download()->streamedFileDirectly($zip, $chapter->slug . '.zip', true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -7,12 +7,13 @@ namespace BookStack\Exports\Controllers;
|
|||||||
use BookStack\Exceptions\ZipImportException;
|
use BookStack\Exceptions\ZipImportException;
|
||||||
use BookStack\Exceptions\ZipValidationException;
|
use BookStack\Exceptions\ZipValidationException;
|
||||||
use BookStack\Exports\ImportRepo;
|
use BookStack\Exports\ImportRepo;
|
||||||
use BookStack\Http\Controller;
|
use BookStack\Http\ApiController;
|
||||||
use BookStack\Uploads\AttachmentService;
|
use BookStack\Uploads\AttachmentService;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
|
use Illuminate\Http\Response;
|
||||||
|
|
||||||
class ImportApiController extends Controller
|
class ImportApiController extends ApiController
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
protected ImportRepo $imports,
|
protected ImportRepo $imports,
|
||||||
@@ -21,101 +22,94 @@ class ImportApiController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* List existing imports visible to the user.
|
* List existing ZIP imports visible to the user.
|
||||||
*/
|
*/
|
||||||
public function list(): JsonResponse
|
public function list(): JsonResponse
|
||||||
{
|
{
|
||||||
$imports = $this->imports->getVisibleImports();
|
$imports = $this->imports->getVisibleImports()->all();
|
||||||
|
|
||||||
return response()->json([
|
return response()->json($imports);
|
||||||
'status' => 'success',
|
|
||||||
'imports' => $imports,
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Upload, validate and store an import file.
|
* Upload, validate and store a ZIP import file.
|
||||||
|
* This does not run the import. That is performed via a separate endpoint.
|
||||||
*/
|
*/
|
||||||
public function upload(Request $request): JsonResponse
|
public function upload(Request $request): JsonResponse
|
||||||
{
|
{
|
||||||
$this->validate($request, [
|
$this->validate($request, $this->rules()['upload']);
|
||||||
'file' => ['required', ...AttachmentService::getFileValidationRules()]
|
|
||||||
]);
|
|
||||||
|
|
||||||
$file = $request->file('file');
|
$file = $request->file('file');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$import = $this->imports->storeFromUpload($file);
|
$import = $this->imports->storeFromUpload($file);
|
||||||
} catch (ZipValidationException $exception) {
|
} catch (ZipValidationException $exception) {
|
||||||
return response()->json([
|
$message = "ZIP upload failed with the following validation errors: \n" . implode("\n", $exception->errors);
|
||||||
'status' => 'error',
|
return $this->jsonError($message, 422);
|
||||||
'message' => 'Validation failed',
|
|
||||||
'errors' => $exception->errors,
|
|
||||||
], 422);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return response()->json([
|
return response()->json($import);
|
||||||
'status' => 'success',
|
|
||||||
'import' => $import,
|
|
||||||
], 201);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Show details of a pending import.
|
* Read details of a pending ZIP import.
|
||||||
*/
|
*/
|
||||||
public function read(int $id): JsonResponse
|
public function read(int $id): JsonResponse
|
||||||
{
|
{
|
||||||
$import = $this->imports->findVisible($id);
|
$import = $this->imports->findVisible($id);
|
||||||
|
|
||||||
return response()->json([
|
return response()->json($import);
|
||||||
'status' => 'success',
|
|
||||||
'import' => $import,
|
|
||||||
'data' => $import->decodeMetadata(),
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run the import process.
|
* Run the import process for an uploaded ZIP import.
|
||||||
|
* The parent_id and parent_type parameters are required when the import type is 'chapter' or 'page'.
|
||||||
|
* On success, returns the imported item.
|
||||||
*/
|
*/
|
||||||
public function create(int $id, Request $request): JsonResponse
|
public function run(int $id, Request $request): JsonResponse
|
||||||
{
|
{
|
||||||
$import = $this->imports->findVisible($id);
|
$import = $this->imports->findVisible($id);
|
||||||
$parent = null;
|
$parent = null;
|
||||||
|
$rules = $this->rules()['run'];
|
||||||
|
|
||||||
if ($import->type === 'page' || $import->type === 'chapter') {
|
if ($import->type === 'page' || $import->type === 'chapter') {
|
||||||
$data = $this->validate($request, [
|
$rules['parent_type'][] = 'required';
|
||||||
'parent' => ['required', 'string'],
|
$rules['parent_id'][] = 'required';
|
||||||
]);
|
$data = $this->validate($request, $rules);
|
||||||
$parent = $data['parent'];
|
$parent = "{$data['parent_type']}:{$data['parent_id']}";
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$entity = $this->imports->runImport($import, $parent);
|
$entity = $this->imports->runImport($import, $parent);
|
||||||
} catch (ZipImportException $exception) {
|
} catch (ZipImportException $exception) {
|
||||||
return response()->json([
|
$message = "ZIP import failed with the following errors: \n" . implode("\n", $exception->errors);
|
||||||
'status' => 'error',
|
return $this->jsonError($message);
|
||||||
'message' => 'Import failed',
|
|
||||||
'errors' => $exception->errors,
|
|
||||||
], 500);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return response()->json([
|
return response()->json($entity);
|
||||||
'status' => 'success',
|
|
||||||
'entity' => $entity,
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete a pending import.
|
* Delete a pending ZIP import.
|
||||||
*/
|
*/
|
||||||
public function delete(int $id): JsonResponse
|
public function delete(int $id): Response
|
||||||
{
|
{
|
||||||
$import = $this->imports->findVisible($id);
|
$import = $this->imports->findVisible($id);
|
||||||
$this->imports->deleteImport($import);
|
$this->imports->deleteImport($import);
|
||||||
|
|
||||||
return response()->json([
|
return response('', 204);
|
||||||
'status' => 'success',
|
|
||||||
'message' => 'Import deleted successfully',
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
protected function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'upload' => [
|
||||||
|
'file' => ['required', ...AttachmentService::getFileValidationRules()],
|
||||||
|
],
|
||||||
|
'run' => [
|
||||||
|
'parent_type' => ['string', 'in:book,chapter'],
|
||||||
|
'parent_id' => ['int'],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@@ -65,14 +65,11 @@ class PageExportApiController extends ApiController
|
|||||||
return $this->download()->directly($markdown, $page->slug . '.md');
|
return $this->download()->directly($markdown, $page->slug . '.md');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public function exportZip(int $id, ZipExportBuilder $builder)
|
public function exportZip(int $id, ZipExportBuilder $builder)
|
||||||
{
|
{
|
||||||
$page = $this->queries->findVisibleByIdOrFail($id);
|
$page = $this->queries->findVisibleByIdOrFail($id);
|
||||||
$pageSlug = $page->slug;
|
|
||||||
$zip = $builder->buildForPage($page);
|
$zip = $builder->buildForPage($page);
|
||||||
|
|
||||||
return $this->download()->streamedFileDirectly($zip, $pageSlug . '.zip', filesize($zip), true);
|
return $this->download()->streamedFileDirectly($zip, $page->slug . '.zip', true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -88,6 +88,12 @@ Route::get('roles/{id}', [RoleApiController::class, 'read']);
|
|||||||
Route::put('roles/{id}', [RoleApiController::class, 'update']);
|
Route::put('roles/{id}', [RoleApiController::class, 'update']);
|
||||||
Route::delete('roles/{id}', [RoleApiController::class, 'delete']);
|
Route::delete('roles/{id}', [RoleApiController::class, 'delete']);
|
||||||
|
|
||||||
|
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}', [ExportControllers\ImportApiController::class, 'run']);
|
||||||
|
Route::delete('import/{id}', [ExportControllers\ImportApiController::class, 'delete']);
|
||||||
|
|
||||||
Route::get('recycle-bin', [EntityControllers\RecycleBinApiController::class, 'list']);
|
Route::get('recycle-bin', [EntityControllers\RecycleBinApiController::class, 'list']);
|
||||||
Route::put('recycle-bin/{deletionId}', [EntityControllers\RecycleBinApiController::class, 'restore']);
|
Route::put('recycle-bin/{deletionId}', [EntityControllers\RecycleBinApiController::class, 'restore']);
|
||||||
Route::delete('recycle-bin/{deletionId}', [EntityControllers\RecycleBinApiController::class, 'destroy']);
|
Route::delete('recycle-bin/{deletionId}', [EntityControllers\RecycleBinApiController::class, 'destroy']);
|
||||||
@@ -98,9 +104,3 @@ Route::put('content-permissions/{contentType}/{contentId}', [ContentPermissionAp
|
|||||||
Route::get('audit-log', [AuditLogApiController::class, 'list']);
|
Route::get('audit-log', [AuditLogApiController::class, 'list']);
|
||||||
|
|
||||||
Route::get('system', [SystemApiController::class, 'read']);
|
Route::get('system', [SystemApiController::class, 'read']);
|
||||||
|
|
||||||
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']);
|
|
Reference in New Issue
Block a user