]> BookStack Code Mirror - bookstack/blob - app/Exports/Controllers/ImportApiController.php
API: Initial review pass of zip import/export endpoints
[bookstack] / app / Exports / Controllers / ImportApiController.php
1 <?php
2
3 declare(strict_types=1);
4
5 namespace BookStack\Exports\Controllers;
6
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;
15
16 class ImportApiController extends ApiController
17 {
18     public function __construct(
19         protected ImportRepo $imports,
20     ) {
21         $this->middleware('can:content-import');
22     }
23
24     /**
25      * List existing ZIP imports visible to the user.
26      */
27     public function list(): JsonResponse
28     {
29         $imports = $this->imports->getVisibleImports()->all();
30
31         return response()->json($imports);
32     }
33
34     /**
35      * Upload, validate and store a ZIP import file.
36      * This does not run the import. That is performed via a separate endpoint.
37      */
38     public function upload(Request $request): JsonResponse
39     {
40         $this->validate($request, $this->rules()['upload']);
41
42         $file = $request->file('file');
43
44         try {
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);
49         }
50
51         return response()->json($import);
52     }
53
54     /**
55      * Read details of a pending ZIP import.
56      */
57     public function read(int $id): JsonResponse
58     {
59         $import = $this->imports->findVisible($id);
60
61         return response()->json($import);
62     }
63
64     /**
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.
68      */
69     public function run(int $id, Request $request): JsonResponse
70     {
71         $import = $this->imports->findVisible($id);
72         $parent = null;
73         $rules = $this->rules()['run'];
74
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']}";
80         }
81
82         try {
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);
87         }
88
89         return response()->json($entity);
90     }
91
92     /**
93      * Delete a pending ZIP import.
94      */
95     public function delete(int $id): Response
96     {
97         $import = $this->imports->findVisible($id);
98         $this->imports->deleteImport($import);
99
100         return response('', 204);
101     }
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 }