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.
27 public function list(): JsonResponse
29 $imports = $this->imports->getVisibleImports()->all();
31 return response()->json($imports);
35 * Upload, validate and store a ZIP import file.
36 * This does not run the import. That is performed via a separate endpoint.
38 public function upload(Request $request): JsonResponse
40 $this->validate($request, $this->rules()['upload']);
42 $file = $request->file('file');
45 $import = $this->imports->storeFromUpload($file);
46 } catch (ZipValidationException $exception) {
47 $message = "ZIP upload failed with the following validation errors: \n" . implode("\n", $exception->errors);
48 return $this->jsonError($message, 422);
51 return response()->json($import);
55 * Read details of a pending ZIP import.
57 public function read(int $id): JsonResponse
59 $import = $this->imports->findVisible($id);
61 return response()->json($import);
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.
69 public function run(int $id, Request $request): JsonResponse
71 $import = $this->imports->findVisible($id);
73 $rules = $this->rules()['run'];
75 if ($import->type === 'page' || $import->type === 'chapter') {
76 $rules['parent_type'][] = 'required';
77 $rules['parent_id'][] = 'required';
78 $data = $this->validate($request, $rules);
79 $parent = "{$data['parent_type']}:{$data['parent_id']}";
83 $entity = $this->imports->runImport($import, $parent);
84 } catch (ZipImportException $exception) {
85 $message = "ZIP import failed with the following errors: \n" . implode("\n", $exception->errors);
86 return $this->jsonError($message);
89 return response()->json($entity);
93 * Delete a pending ZIP import.
95 public function delete(int $id): Response
97 $import = $this->imports->findVisible($id);
98 $this->imports->deleteImport($import);
100 return response('', 204);
103 protected function rules(): array
107 'file' => ['required', ...AttachmentService::getFileValidationRules()],
110 'parent_type' => ['string', 'in:book,chapter'],
111 'parent_id' => ['int'],