<?php
+declare(strict_types=1);
+
namespace BookStack\Exports\Controllers;
-use BookStack\Exports\Import;
-use BookStack\Exports\ZipExports\ZipExportReader;
-use BookStack\Exports\ZipExports\ZipExportValidator;
+use BookStack\Exceptions\ZipImportException;
+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');
}
- public function start(Request $request)
+ /**
+ * 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()
{
- // TODO - Show existing imports for user (or for all users if admin-level user)
+ $imports = $this->imports->getVisibleImports();
+
+ $this->setPageTitle(trans('entities.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();
+ try {
+ $import = $this->imports->storeFromUpload($file);
+ } catch (ZipValidationException $exception) {
+ return redirect('/import')->with('validation_errors', $exception->errors);
+ }
+
+ return redirect($import->getUrl());
+ }
+
+ /**
+ * Show a pending import, with a form to allow progressing
+ * with the import process.
+ */
+ public function show(int $id)
+ {
+ $import = $this->imports->findVisible($id);
+
+ $this->setPageTitle(trans('entities.import_continue'));
+
+ return view('exports.import-show', [
+ 'import' => $import,
+ 'data' => $import->decodeMetadata(),
+ ]);
+ }
+
+ /**
+ * Run the import process against an uploaded import ZIP.
+ */
+ public function run(int $id, Request $request)
+ {
+ $import = $this->imports->findVisible($id);
+ $parent = null;
+
+ if ($import->type === 'page' || $import->type === 'chapter') {
+ session()->setPreviousUrl($import->getUrl());
+ $data = $this->validate($request, [
+ 'parent' => ['required', 'string'],
+ ]);
+ $parent = $data['parent'];
+ }
- $errors = (new ZipExportValidator($zipPath))->validate();
- if ($errors) {
- session()->flash('validation_errors', $errors);
- return redirect('/import');
+ try {
+ $entity = $this->imports->runImport($import, $parent);
+ } catch (ZipImportException $exception) {
+ session()->flush();
+ $this->showErrorNotification(trans('errors.import_zip_failed_notification'));
+ return redirect($import->getUrl())->with('import_errors', $exception->errors);
}
- $zipEntityInfo = (new ZipExportReader($zipPath))->getEntityInfo();
- $import = new Import();
- $import->name = $zipEntityInfo['name'];
- $import->book_count = $zipEntityInfo['book_count'];
- $import->chapter_count = $zipEntityInfo['chapter_count'];
- $import->page_count = $zipEntityInfo['page_count'];
- $import->created_by = user()->id;
- $import->size = filesize($zipPath);
- // TODO - Set path
- // TODO - Save
-
- // TODO - Split out attachment service to separate out core filesystem/disk stuff
- // To reuse for import handling
-
- dd('passed');
- // TODO - Upload to storage
- // TODO - Store info/results for display:
- // TODO - Send user to next import stage
+ return redirect($entity->getUrl());
+ }
+
+ /**
+ * Delete an active pending import from the filesystem and database.
+ */
+ public function delete(int $id)
+ {
+ $import = $this->imports->findVisible($id);
+ $this->imports->deleteImport($import);
+
+ return redirect('/import');
}
}