7
7
use BookStack \Exceptions \ZipImportException ;
8
8
use BookStack \Exceptions \ZipValidationException ;
9
9
use BookStack \Exports \ImportRepo ;
10
- use BookStack \Http \Controller ;
10
+ use BookStack \Http \ApiController ;
11
11
use BookStack \Uploads \AttachmentService ;
12
12
use Illuminate \Http \Request ;
13
13
use Illuminate \Http \JsonResponse ;
14
+ use Illuminate \Http \Response ;
14
15
15
- class ImportApiController extends Controller
16
+ class ImportApiController extends ApiController
16
17
{
17
18
public function __construct (
18
19
protected ImportRepo $ imports ,
@@ -21,101 +22,94 @@ public function __construct(
21
22
}
22
23
23
24
/**
24
- * List existing imports visible to the user.
25
+ * List existing ZIP imports visible to the user.
25
26
*/
26
27
public function list (): JsonResponse
27
28
{
28
- $ imports = $ this ->imports ->getVisibleImports ();
29
+ $ imports = $ this ->imports ->getVisibleImports ()-> all () ;
29
30
30
- return response ()->json ([
31
- 'status ' => 'success ' ,
32
- 'imports ' => $ imports ,
33
- ]);
31
+ return response ()->json ($ imports );
34
32
}
35
33
36
34
/**
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.
38
37
*/
39
38
public function upload (Request $ request ): JsonResponse
40
39
{
41
- $ this ->validate ($ request , [
42
- 'file ' => ['required ' , ...AttachmentService::getFileValidationRules ()]
43
- ]);
40
+ $ this ->validate ($ request , $ this ->rules ()['upload ' ]);
44
41
45
42
$ file = $ request ->file ('file ' );
46
43
47
44
try {
48
45
$ import = $ this ->imports ->storeFromUpload ($ file );
49
46
} 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 );
55
49
}
56
50
57
- return response ()->json ([
58
- 'status ' => 'success ' ,
59
- 'import ' => $ import ,
60
- ], 201 );
51
+ return response ()->json ($ import );
61
52
}
62
53
63
54
/**
64
- * Show details of a pending import.
55
+ * Read details of a pending ZIP import.
65
56
*/
66
57
public function read (int $ id ): JsonResponse
67
58
{
68
59
$ import = $ this ->imports ->findVisible ($ id );
69
60
70
- return response ()->json ([
71
- 'status ' => 'success ' ,
72
- 'import ' => $ import ,
73
- 'data ' => $ import ->decodeMetadata (),
74
- ]);
61
+ return response ()->json ($ import );
75
62
}
76
63
77
64
/**
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.
79
68
*/
80
- public function create (int $ id , Request $ request ): JsonResponse
69
+ public function run (int $ id , Request $ request ): JsonResponse
81
70
{
82
71
$ import = $ this ->imports ->findVisible ($ id );
83
72
$ parent = null ;
73
+ $ rules = $ this ->rules ()['run ' ];
84
74
85
75
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 ' ]}" ;
90
80
}
91
81
92
82
try {
93
83
$ entity = $ this ->imports ->runImport ($ import , $ parent );
94
84
} 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 );
100
87
}
101
88
102
- return response ()->json ([
103
- 'status ' => 'success ' ,
104
- 'entity ' => $ entity ,
105
- ]);
89
+ return response ()->json ($ entity );
106
90
}
107
91
108
92
/**
109
- * Delete a pending import.
93
+ * Delete a pending ZIP import.
110
94
*/
111
- public function delete (int $ id ): JsonResponse
95
+ public function delete (int $ id ): Response
112
96
{
113
97
$ import = $ this ->imports ->findVisible ($ id );
114
98
$ this ->imports ->deleteImport ($ import );
115
99
116
- return response ()->json ([
117
- 'status ' => 'success ' ,
118
- 'message ' => 'Import deleted successfully ' ,
119
- ]);
100
+ return response ('' , 204 );
120
101
}
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