3 declare(strict_types=1);
5 namespace BookStack\Exports\Controllers;
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;
16 class ImportApiController extends ApiController
18 public function __construct(
19 protected ImportRepo $imports,
21 $this->middleware('can:content-import');
25 * List existing ZIP imports visible to the user.
26 * Requires permission to import content.
28 public function list(): JsonResponse
30 $query = $this->imports->queryVisible();
32 return $this->apiListingResponse($query, [
33 'id', 'name', 'size', 'type', 'created_by', 'created_at', 'updated_at'
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.
42 * This "file" parameter must be a BookStack-compatible ZIP file, and this must be
43 * sent via a 'multipart/form-data' type request.
45 * Requires permission to import content.
47 public function create(Request $request): JsonResponse
49 $this->validate($request, $this->rules()['create']);
51 $file = $request->file('file');
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);
60 return response()->json($import);
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.
69 public function read(int $id): JsonResponse
71 $import = $this->imports->findVisible($id);
73 $import->setAttribute('details', $import->decodeMetadata());
75 return response()->json($import);
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.
84 public function run(int $id, Request $request): JsonResponse
86 $import = $this->imports->findVisible($id);
88 $rules = $this->rules()['run'];
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']}";
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);
104 return response()->json($entity->withoutRelations());
108 * Delete a pending ZIP import from the system.
109 * Requires permission to import content.
111 public function delete(int $id): Response
113 $import = $this->imports->findVisible($id);
114 $this->imports->deleteImport($import);
116 return response('', 204);
119 protected function rules(): array
123 'file' => ['required', ...AttachmentService::getFileValidationRules()],
126 'parent_type' => ['string', 'in:book,chapter'],
127 'parent_id' => ['int'],
132 protected function formatErrors(array $errors): string
135 foreach ($errors as $key => $error) {
136 if (is_string($key)) {
137 $parts[] = "[{$key}] {$error}";
142 return implode("\n", $parts);