| 
									
										
										
										
											2024-10-29 20:11:51 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-03 22:13:05 +08:00
										 |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-29 20:11:51 +08:00
										 |  |  | namespace BookStack\Exports\Controllers; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-14 23:59:15 +08:00
										 |  |  | use BookStack\Exceptions\ZipImportException; | 
					
						
							| 
									
										
										
										
											2024-11-03 04:48:21 +08:00
										 |  |  | use BookStack\Exceptions\ZipValidationException; | 
					
						
							|  |  |  | use BookStack\Exports\ImportRepo; | 
					
						
							| 
									
										
										
										
											2024-10-29 20:11:51 +08:00
										 |  |  | use BookStack\Http\Controller; | 
					
						
							| 
									
										
										
										
											2024-11-03 04:48:21 +08:00
										 |  |  | use BookStack\Uploads\AttachmentService; | 
					
						
							| 
									
										
										
										
											2024-10-29 20:11:51 +08:00
										 |  |  | use Illuminate\Http\Request; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ImportController extends Controller | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-11-03 04:48:21 +08:00
										 |  |  |     public function __construct( | 
					
						
							|  |  |  |         protected ImportRepo $imports, | 
					
						
							|  |  |  |     ) { | 
					
						
							| 
									
										
										
										
											2024-10-29 20:11:51 +08:00
										 |  |  |         $this->middleware('can:content-import'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-03 22:13:05 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Show the view to start a new import, and also list out the existing | 
					
						
							|  |  |  |      * in progress imports that are visible to the user. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2024-11-04 01:28:18 +08:00
										 |  |  |     public function start() | 
					
						
							| 
									
										
										
										
											2024-10-29 20:11:51 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-11-03 22:13:05 +08:00
										 |  |  |         $imports = $this->imports->getVisibleImports(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->setPageTitle(trans('entities.import')); | 
					
						
							| 
									
										
										
										
											2024-10-29 22:21:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-02 22:51:04 +08:00
										 |  |  |         return view('exports.import', [ | 
					
						
							| 
									
										
										
										
											2024-11-03 22:13:05 +08:00
										 |  |  |             'imports' => $imports, | 
					
						
							| 
									
										
										
										
											2024-11-02 22:51:04 +08:00
										 |  |  |             'zipErrors' => session()->pull('validation_errors') ?? [], | 
					
						
							|  |  |  |         ]); | 
					
						
							| 
									
										
										
										
											2024-10-29 20:11:51 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-03 22:13:05 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Upload, validate and store an import file. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2024-10-29 20:11:51 +08:00
										 |  |  |     public function upload(Request $request) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-10-30 21:13:41 +08:00
										 |  |  |         $this->validate($request, [ | 
					
						
							| 
									
										
										
										
											2024-11-03 04:48:21 +08:00
										 |  |  |             'file' => ['required', ...AttachmentService::getFileValidationRules()] | 
					
						
							| 
									
										
										
										
											2024-10-30 21:13:41 +08:00
										 |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $file = $request->file('file'); | 
					
						
							| 
									
										
										
										
											2024-11-03 04:48:21 +08:00
										 |  |  |         try { | 
					
						
							|  |  |  |             $import = $this->imports->storeFromUpload($file); | 
					
						
							|  |  |  |         } catch (ZipValidationException $exception) { | 
					
						
							| 
									
										
										
										
											2024-11-14 23:59:15 +08:00
										 |  |  |             return redirect('/import')->with('validation_errors', $exception->errors); | 
					
						
							| 
									
										
										
										
											2024-10-30 23:26:23 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-11-02 22:51:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-03 22:13:05 +08:00
										 |  |  |         return redirect($import->getUrl()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Show a pending import, with a form to allow progressing | 
					
						
							|  |  |  |      * with the import process. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function show(int $id) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $import = $this->imports->findVisible($id); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->setPageTitle(trans('entities.import_continue')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return view('exports.import-show', [ | 
					
						
							|  |  |  |             'import' => $import, | 
					
						
							| 
									
										
										
										
											2024-11-05 21:17:31 +08:00
										 |  |  |             'data' => $import->decodeMetadata(), | 
					
						
							| 
									
										
										
										
											2024-11-03 22:13:05 +08:00
										 |  |  |         ]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-16 21:57:41 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Run the import process against an uploaded import ZIP. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2024-11-05 00:21:22 +08:00
										 |  |  |     public function run(int $id, Request $request) | 
					
						
							| 
									
										
										
										
											2024-11-04 01:28:18 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         $import = $this->imports->findVisible($id); | 
					
						
							| 
									
										
										
										
											2024-11-05 00:21:22 +08:00
										 |  |  |         $parent = null; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-11 23:06:46 +08:00
										 |  |  |         if ($import->type === 'page' || $import->type === 'chapter') { | 
					
						
							| 
									
										
										
										
											2024-11-14 23:59:15 +08:00
										 |  |  |             session()->setPreviousUrl($import->getUrl()); | 
					
						
							| 
									
										
										
										
											2024-11-05 00:21:22 +08:00
										 |  |  |             $data = $this->validate($request, [ | 
					
						
							| 
									
										
										
										
											2024-11-14 23:59:15 +08:00
										 |  |  |                 'parent' => ['required', 'string'], | 
					
						
							| 
									
										
										
										
											2024-11-05 00:21:22 +08:00
										 |  |  |             ]); | 
					
						
							|  |  |  |             $parent = $data['parent']; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-11-04 01:28:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-14 23:59:15 +08:00
										 |  |  |         try { | 
					
						
							|  |  |  |             $entity = $this->imports->runImport($import, $parent); | 
					
						
							|  |  |  |         } catch (ZipImportException $exception) { | 
					
						
							| 
									
										
										
										
											2024-11-25 23:54:15 +08:00
										 |  |  |             session()->flush(); | 
					
						
							|  |  |  |             $this->showErrorNotification(trans('errors.import_zip_failed_notification')); | 
					
						
							| 
									
										
										
										
											2024-11-14 23:59:15 +08:00
										 |  |  |             return redirect($import->getUrl())->with('import_errors', $exception->errors); | 
					
						
							| 
									
										
										
										
											2024-11-11 23:06:46 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-11-14 23:59:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return redirect($entity->getUrl()); | 
					
						
							| 
									
										
										
										
											2024-11-04 01:28:18 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-03 22:13:05 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Delete an active pending import from the filesystem and database. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function delete(int $id) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $import = $this->imports->findVisible($id); | 
					
						
							|  |  |  |         $this->imports->deleteImport($import); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return redirect('/import'); | 
					
						
							| 
									
										
										
										
											2024-10-29 20:11:51 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | } |