3 declare(strict_types=1);
5 namespace BookStack\Exports\Controllers;
7 use BookStack\Exceptions\ZipImportException;
8 use BookStack\Exceptions\ZipValidationException;
9 use BookStack\Exports\ImportRepo;
10 use BookStack\Http\Controller;
11 use BookStack\Uploads\AttachmentService;
12 use Illuminate\Http\Request;
14 class ImportController extends Controller
16 public function __construct(
17 protected ImportRepo $imports,
19 $this->middleware('can:content-import');
23 * Show the view to start a new import, and also list out the existing
24 * in progress imports that are visible to the user.
26 public function start()
28 $imports = $this->imports->getVisibleImports();
30 $this->setPageTitle(trans('entities.import'));
32 return view('exports.import', [
33 'imports' => $imports,
34 'zipErrors' => session()->pull('validation_errors') ?? [],
39 * Upload, validate and store an import file.
41 public function upload(Request $request)
43 $this->validate($request, [
44 'file' => ['required', ...AttachmentService::getFileValidationRules()]
47 $file = $request->file('file');
49 $import = $this->imports->storeFromUpload($file);
50 } catch (ZipValidationException $exception) {
51 return redirect('/import')->with('validation_errors', $exception->errors);
54 return redirect($import->getUrl());
58 * Show a pending import, with a form to allow progressing
59 * with the import process.
61 public function show(int $id)
63 $import = $this->imports->findVisible($id);
65 $this->setPageTitle(trans('entities.import_continue'));
67 return view('exports.import-show', [
69 'data' => $import->decodeMetadata(),
73 public function run(int $id, Request $request)
75 // TODO - Test access/visibility
76 $import = $this->imports->findVisible($id);
79 if ($import->type === 'page' || $import->type === 'chapter') {
80 session()->setPreviousUrl($import->getUrl());
81 $data = $this->validate($request, [
82 'parent' => ['required', 'string'],
84 $parent = $data['parent'];
88 $entity = $this->imports->runImport($import, $parent);
89 } catch (ZipImportException $exception) {
90 return redirect($import->getUrl())->with('import_errors', $exception->errors);
93 return redirect($entity->getUrl());
97 * Delete an active pending import from the filesystem and database.
99 public function delete(int $id)
101 $import = $this->imports->findVisible($id);
102 $this->imports->deleteImport($import);
104 return redirect('/import');