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