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(Request $request)
28 // TODO - Test visibility access for listed items
29 $imports = $this->imports->getVisibleImports();
31 $this->setPageTitle(trans('entities.import'));
33 return view('exports.import', [
34 'imports' => $imports,
35 'zipErrors' => session()->pull('validation_errors') ?? [],
40 * Upload, validate and store an import file.
42 public function upload(Request $request)
44 $this->validate($request, [
45 'file' => ['required', ...AttachmentService::getFileValidationRules()]
48 $file = $request->file('file');
50 $import = $this->imports->storeFromUpload($file);
51 } catch (ZipValidationException $exception) {
52 session()->flash('validation_errors', $exception->errors);
53 return redirect('/import');
56 $this->logActivity(ActivityType::IMPORT_CREATE, $import);
58 return redirect($import->getUrl());
62 * Show a pending import, with a form to allow progressing
63 * with the import process.
65 public function show(int $id)
67 // TODO - Test visibility access
68 $import = $this->imports->findVisible($id);
70 $this->setPageTitle(trans('entities.import_continue'));
72 return view('exports.import-show', [
78 * Delete an active pending import from the filesystem and database.
80 public function delete(int $id)
82 // TODO - Test visibility access
83 $import = $this->imports->findVisible($id);
84 $this->imports->deleteImport($import);
86 $this->logActivity(ActivityType::IMPORT_DELETE, $import);
88 return redirect('/import');