]> BookStack Code Mirror - bookstack/blobdiff - app/Exports/Controllers/ImportController.php
ZIP Imports: Added listing, show view, delete, activity
[bookstack] / app / Exports / Controllers / ImportController.php
index 5885f7991cd014211e71600b879686db3fbc3c63..582fff975d2880a4928e3f884fd320d18071e74d 100644 (file)
@@ -1,43 +1,90 @@
 <?php
 
+declare(strict_types=1);
+
 namespace BookStack\Exports\Controllers;
 
-use BookStack\Exports\ZipExports\ZipExportValidator;
+use BookStack\Activity\ActivityType;
+use BookStack\Exceptions\ZipValidationException;
+use BookStack\Exports\ImportRepo;
 use BookStack\Http\Controller;
+use BookStack\Uploads\AttachmentService;
 use Illuminate\Http\Request;
 
 class ImportController extends Controller
 {
-    public function __construct()
-    {
+    public function __construct(
+        protected ImportRepo $imports,
+    ) {
         $this->middleware('can:content-import');
     }
 
+    /**
+     * Show the view to start a new import, and also list out the existing
+     * in progress imports that are visible to the user.
+     */
     public function start(Request $request)
     {
-        // TODO - Show existing imports for user (or for all users if admin-level user)
+        // TODO - Test visibility access for listed items
+        $imports = $this->imports->getVisibleImports();
+
+        $this->setPageTitle(trans('entities.import'));
 
-        return view('exports.import');
+        return view('exports.import', [
+            'imports' => $imports,
+            'zipErrors' => session()->pull('validation_errors') ?? [],
+        ]);
     }
 
+    /**
+     * Upload, validate and store an import file.
+     */
     public function upload(Request $request)
     {
         $this->validate($request, [
-            'file' => ['required', 'file']
+            'file' => ['required', ...AttachmentService::getFileValidationRules()]
         ]);
 
         $file = $request->file('file');
-        $zipPath = $file->getRealPath();
-
-        $errors = (new ZipExportValidator($zipPath))->validate();
-        if ($errors) {
-            dd($errors);
+        try {
+            $import = $this->imports->storeFromUpload($file);
+        } catch (ZipValidationException $exception) {
+            session()->flash('validation_errors', $exception->errors);
+            return redirect('/import');
         }
-        dd('passed');
-        // TODO - Read existing ZIP upload and send through validator
-            // TODO - If invalid, return user with errors
-        // TODO - Upload to storage
-        // TODO - Store info/results from validator
-        // TODO - Send user to next import stage
+
+        $this->logActivity(ActivityType::IMPORT_CREATE, $import);
+
+        return redirect($import->getUrl());
+    }
+
+    /**
+     * Show a pending import, with a form to allow progressing
+     * with the import process.
+     */
+    public function show(int $id)
+    {
+        // TODO - Test visibility access
+        $import = $this->imports->findVisible($id);
+
+        $this->setPageTitle(trans('entities.import_continue'));
+
+        return view('exports.import-show', [
+            'import' => $import,
+        ]);
+    }
+
+    /**
+     * Delete an active pending import from the filesystem and database.
+     */
+    public function delete(int $id)
+    {
+        // TODO - Test visibility access
+        $import = $this->imports->findVisible($id);
+        $this->imports->deleteImport($import);
+
+        $this->logActivity(ActivityType::IMPORT_DELETE, $import);
+
+        return redirect('/import');
     }
 }