77use BookStack \Exceptions \ZipImportException ;
88use BookStack \Exceptions \ZipValidationException ;
99use BookStack \Exports \ImportRepo ;
10- use BookStack \Http \Controller ;
10+ use BookStack \Http \ApiController ;
1111use BookStack \Uploads \AttachmentService ;
1212use Illuminate \Http \Request ;
1313use Illuminate \Http \JsonResponse ;
14+ use Illuminate \Http \Response ;
1415
15- class ImportApiController extends Controller
16+ class ImportApiController extends ApiController
1617{
1718 public function __construct (
1819 protected ImportRepo $ imports ,
@@ -21,101 +22,94 @@ public function __construct(
2122 }
2223
2324 /**
24- * List existing imports visible to the user.
25+ * List existing ZIP imports visible to the user.
2526 */
2627 public function list (): JsonResponse
2728 {
28- $ imports = $ this ->imports ->getVisibleImports ();
29+ $ imports = $ this ->imports ->getVisibleImports ()-> all () ;
2930
30- return response ()->json ([
31- 'status ' => 'success ' ,
32- 'imports ' => $ imports ,
33- ]);
31+ return response ()->json ($ imports );
3432 }
3533
3634 /**
37- * Upload, validate and store an import file.
35+ * Upload, validate and store a ZIP import file.
36+ * This does not run the import. That is performed via a separate endpoint.
3837 */
3938 public function upload (Request $ request ): JsonResponse
4039 {
41- $ this ->validate ($ request , [
42- 'file ' => ['required ' , ...AttachmentService::getFileValidationRules ()]
43- ]);
40+ $ this ->validate ($ request , $ this ->rules ()['upload ' ]);
4441
4542 $ file = $ request ->file ('file ' );
4643
4744 try {
4845 $ import = $ this ->imports ->storeFromUpload ($ file );
4946 } catch (ZipValidationException $ exception ) {
50- return response ()->json ([
51- 'status ' => 'error ' ,
52- 'message ' => 'Validation failed ' ,
53- 'errors ' => $ exception ->errors ,
54- ], 422 );
47+ $ message = "ZIP upload failed with the following validation errors: \n" . implode ("\n" , $ exception ->errors );
48+ return $ this ->jsonError ($ message , 422 );
5549 }
5650
57- return response ()->json ([
58- 'status ' => 'success ' ,
59- 'import ' => $ import ,
60- ], 201 );
51+ return response ()->json ($ import );
6152 }
6253
6354 /**
64- * Show details of a pending import.
55+ * Read details of a pending ZIP import.
6556 */
6657 public function read (int $ id ): JsonResponse
6758 {
6859 $ import = $ this ->imports ->findVisible ($ id );
6960
70- return response ()->json ([
71- 'status ' => 'success ' ,
72- 'import ' => $ import ,
73- 'data ' => $ import ->decodeMetadata (),
74- ]);
61+ return response ()->json ($ import );
7562 }
7663
7764 /**
78- * Run the import process.
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.
7968 */
80- public function create (int $ id , Request $ request ): JsonResponse
69+ public function run (int $ id , Request $ request ): JsonResponse
8170 {
8271 $ import = $ this ->imports ->findVisible ($ id );
8372 $ parent = null ;
73+ $ rules = $ this ->rules ()['run ' ];
8474
8575 if ($ import ->type === 'page ' || $ import ->type === 'chapter ' ) {
86- $ data = $ this -> validate ( $ request , [
87- ' parent ' => [ 'required ' , ' string ' ],
88- ] );
89- $ parent = $ data ['parent ' ] ;
76+ $ rules [ ' parent_type ' ][] = ' required ' ;
77+ $ rules [ ' parent_id ' ][] = 'required ' ;
78+ $ data = $ this -> validate ( $ request , $ rules );
79+ $ parent = "{ $ data ['parent_type ' ]} : { $ data [ ' parent_id ' ]}" ;
9080 }
9181
9282 try {
9383 $ entity = $ this ->imports ->runImport ($ import , $ parent );
9484 } catch (ZipImportException $ exception ) {
95- return response ()->json ([
96- 'status ' => 'error ' ,
97- 'message ' => 'Import failed ' ,
98- 'errors ' => $ exception ->errors ,
99- ], 500 );
85+ $ message = "ZIP import failed with the following errors: \n" . implode ("\n" , $ exception ->errors );
86+ return $ this ->jsonError ($ message );
10087 }
10188
102- return response ()->json ([
103- 'status ' => 'success ' ,
104- 'entity ' => $ entity ,
105- ]);
89+ return response ()->json ($ entity );
10690 }
10791
10892 /**
109- * Delete a pending import.
93+ * Delete a pending ZIP import.
11094 */
111- public function delete (int $ id ): JsonResponse
95+ public function delete (int $ id ): Response
11296 {
11397 $ import = $ this ->imports ->findVisible ($ id );
11498 $ this ->imports ->deleteImport ($ import );
11599
116- return response ()->json ([
117- 'status ' => 'success ' ,
118- 'message ' => 'Import deleted successfully ' ,
119- ]);
100+ return response ('' , 204 );
120101 }
121- }
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+ }
0 commit comments