3 declare(strict_types=1);
5 namespace BookStack\Exports\Controllers;
7 use BookStack\Activity\ActivityType;
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 session()->flash('validation_errors', $exception->errors);
52 return redirect('/import');
55 $this->logActivity(ActivityType::IMPORT_CREATE, $import);
57 return redirect($import->getUrl());
61 * Show a pending import, with a form to allow progressing
62 * with the import process.
64 public function show(int $id)
66 $import = $this->imports->findVisible($id);
68 $this->setPageTitle(trans('entities.import_continue'));
70 return view('exports.import-show', [
72 'data' => $import->decodeMetadata(),
76 public function run(int $id, Request $request)
78 // TODO - Test access/visibility
79 $import = $this->imports->findVisible($id);
82 if ($import->type === 'page' || $import->type === 'chapter') {
83 $data = $this->validate($request, [
84 'parent' => ['required', 'string']
86 $parent = $data['parent'];
89 $entity = $this->imports->runImport($import, $parent);
91 $this->logActivity(ActivityType::IMPORT_RUN, $import);
92 return redirect($entity->getUrl());
94 // TODO - Redirect to result
95 // TODO - Or redirect back with errors
100 * Delete an active pending import from the filesystem and database.
102 public function delete(int $id)
104 $import = $this->imports->findVisible($id);
105 $this->imports->deleteImport($import);
107 $this->logActivity(ActivityType::IMPORT_DELETE, $import);
109 return redirect('/import');