]> BookStack Code Mirror - bookstack/blob - app/Exports/Controllers/ImportController.php
3a56ed0345647a67c8979576115437275ea330a6
[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()
27     {
28         $imports = $this->imports->getVisibleImports();
29
30         $this->setPageTitle(trans('entities.import'));
31
32         return view('exports.import', [
33             'imports' => $imports,
34             'zipErrors' => session()->pull('validation_errors') ?? [],
35         ]);
36     }
37
38     /**
39      * Upload, validate and store an import file.
40      */
41     public function upload(Request $request)
42     {
43         $this->validate($request, [
44             'file' => ['required', ...AttachmentService::getFileValidationRules()]
45         ]);
46
47         $file = $request->file('file');
48         try {
49             $import = $this->imports->storeFromUpload($file);
50         } catch (ZipValidationException $exception) {
51             session()->flash('validation_errors', $exception->errors);
52             return redirect('/import');
53         }
54
55         $this->logActivity(ActivityType::IMPORT_CREATE, $import);
56
57         return redirect($import->getUrl());
58     }
59
60     /**
61      * Show a pending import, with a form to allow progressing
62      * with the import process.
63      */
64     public function show(int $id)
65     {
66         $import = $this->imports->findVisible($id);
67
68 //        dd($import->decodeMetadata());
69
70         $this->setPageTitle(trans('entities.import_continue'));
71
72         return view('exports.import-show', [
73             'import' => $import,
74             'data' => $import->decodeMetadata(),
75         ]);
76     }
77
78     public function run(int $id, Request $request)
79     {
80         // TODO - Test access/visibility
81         $import = $this->imports->findVisible($id);
82         $parent = null;
83
84         if ($import->getType() === 'page' || $import->getType() === 'chapter') {
85             $data = $this->validate($request, [
86                 'parent' => ['required', 'string']
87             ]);
88             $parent = $data['parent'];
89         }
90
91         // TODO  - Run import
92            // TODO - Validate again before
93            // TODO - Check permissions before (create for main item, create for children, create for related items [image, attachments])
94         // TODO - Redirect to result
95         // TODO - Or redirect back with errors
96     }
97
98     /**
99      * Delete an active pending import from the filesystem and database.
100      */
101     public function delete(int $id)
102     {
103         $import = $this->imports->findVisible($id);
104         $this->imports->deleteImport($import);
105
106         $this->logActivity(ActivityType::IMPORT_DELETE, $import);
107
108         return redirect('/import');
109     }
110 }