]> BookStack Code Mirror - bookstack/blob - app/Exports/Controllers/ImportController.php
ZIP Imports: Added listing, show view, delete, activity
[bookstack] / app / Exports / Controllers / ImportController.php
1 <?php
2
3 declare(strict_types=1);
4
5 namespace BookStack\Exports\Controllers;
6
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;
13
14 class ImportController extends Controller
15 {
16     public function __construct(
17         protected ImportRepo $imports,
18     ) {
19         $this->middleware('can:content-import');
20     }
21
22     /**
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.
25      */
26     public function start(Request $request)
27     {
28         // TODO - Test visibility access for listed items
29         $imports = $this->imports->getVisibleImports();
30
31         $this->setPageTitle(trans('entities.import'));
32
33         return view('exports.import', [
34             'imports' => $imports,
35             'zipErrors' => session()->pull('validation_errors') ?? [],
36         ]);
37     }
38
39     /**
40      * Upload, validate and store an import file.
41      */
42     public function upload(Request $request)
43     {
44         $this->validate($request, [
45             'file' => ['required', ...AttachmentService::getFileValidationRules()]
46         ]);
47
48         $file = $request->file('file');
49         try {
50             $import = $this->imports->storeFromUpload($file);
51         } catch (ZipValidationException $exception) {
52             session()->flash('validation_errors', $exception->errors);
53             return redirect('/import');
54         }
55
56         $this->logActivity(ActivityType::IMPORT_CREATE, $import);
57
58         return redirect($import->getUrl());
59     }
60
61     /**
62      * Show a pending import, with a form to allow progressing
63      * with the import process.
64      */
65     public function show(int $id)
66     {
67         // TODO - Test visibility access
68         $import = $this->imports->findVisible($id);
69
70         $this->setPageTitle(trans('entities.import_continue'));
71
72         return view('exports.import-show', [
73             'import' => $import,
74         ]);
75     }
76
77     /**
78      * Delete an active pending import from the filesystem and database.
79      */
80     public function delete(int $id)
81     {
82         // TODO - Test visibility access
83         $import = $this->imports->findVisible($id);
84         $this->imports->deleteImport($import);
85
86         $this->logActivity(ActivityType::IMPORT_DELETE, $import);
87
88         return redirect('/import');
89     }
90 }