]> BookStack Code Mirror - bookstack/blob - app/Exports/Controllers/ImportApiController.php
ZIP Imports: Added API test cases
[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         $query = $this->imports->queryVisible();
30
31         return $this->apiListingResponse($query, [
32             'id', 'name', 'size', 'type', 'created_by', 'created_at', 'updated_at'
33         ]);
34     }
35
36     /**
37      * Upload, validate and store a ZIP import file.
38      * This does not run the import. That is performed via a separate endpoint.
39      */
40     public function upload(Request $request): JsonResponse
41     {
42         $this->validate($request, $this->rules()['upload']);
43
44         $file = $request->file('file');
45
46         try {
47             $import = $this->imports->storeFromUpload($file);
48         } catch (ZipValidationException $exception) {
49             $message = "ZIP upload failed with the following validation errors: \n" . $this->formatErrors($exception->errors);
50             return $this->jsonError($message, 422);
51         }
52
53         return response()->json($import);
54     }
55
56     /**
57      * Read details of a pending ZIP import.
58      * The "details" property contains high-level metadata regarding the ZIP import content,
59      * and the structure of this will change depending on import "type".
60      */
61     public function read(int $id): JsonResponse
62     {
63         $import = $this->imports->findVisible($id);
64
65         $import->setAttribute('details', $import->decodeMetadata());
66
67         return response()->json($import);
68     }
69
70     /**
71      * Run the import process for an uploaded ZIP import.
72      * The parent_id and parent_type parameters are required when the import type is 'chapter' or 'page'.
73      * On success, returns the imported item.
74      */
75     public function run(int $id, Request $request): JsonResponse
76     {
77         $import = $this->imports->findVisible($id);
78         $parent = null;
79         $rules = $this->rules()['run'];
80
81         if ($import->type === 'page' || $import->type === 'chapter') {
82             $rules['parent_type'][] = 'required';
83             $rules['parent_id'][] = 'required';
84             $data = $this->validate($request, $rules);
85             $parent = "{$data['parent_type']}:{$data['parent_id']}";
86         }
87
88         try {
89             $entity = $this->imports->runImport($import, $parent);
90         } catch (ZipImportException $exception) {
91             $message = "ZIP import failed with the following errors: \n" . $this->formatErrors($exception->errors);
92             return $this->jsonError($message);
93         }
94
95         return response()->json($entity);
96     }
97
98     /**
99      * Delete a pending ZIP import.
100      */
101     public function delete(int $id): Response
102     {
103         $import = $this->imports->findVisible($id);
104         $this->imports->deleteImport($import);
105
106         return response('', 204);
107     }
108
109     protected function rules(): array
110     {
111         return [
112             'upload' => [
113                 'file' => ['required', ...AttachmentService::getFileValidationRules()],
114             ],
115             'run' => [
116                 'parent_type' => ['string', 'in:book,chapter'],
117                 'parent_id' => ['int'],
118             ],
119         ];
120     }
121
122     protected function formatErrors(array $errors): string
123     {
124         $parts = [];
125         foreach ($errors as $key => $error) {
126             if (is_string($key)) {
127                 $parts[] = "[{$key}] {$error}";
128             } else {
129                 $parts[] = $error;
130             }
131         }
132         return implode("\n", $parts);
133     }
134 }