2024-10-29 20:11:51 +08:00
|
|
|
<?php
|
|
|
|
|
2024-11-03 22:13:05 +08:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2024-10-29 20:11:51 +08:00
|
|
|
namespace BookStack\Exports\Controllers;
|
|
|
|
|
2024-11-03 22:13:05 +08:00
|
|
|
use BookStack\Activity\ActivityType;
|
2024-11-03 04:48:21 +08:00
|
|
|
use BookStack\Exceptions\ZipValidationException;
|
|
|
|
use BookStack\Exports\ImportRepo;
|
2024-10-29 20:11:51 +08:00
|
|
|
use BookStack\Http\Controller;
|
2024-11-03 04:48:21 +08:00
|
|
|
use BookStack\Uploads\AttachmentService;
|
2024-10-29 20:11:51 +08:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
class ImportController extends Controller
|
|
|
|
{
|
2024-11-03 04:48:21 +08:00
|
|
|
public function __construct(
|
|
|
|
protected ImportRepo $imports,
|
|
|
|
) {
|
2024-10-29 20:11:51 +08:00
|
|
|
$this->middleware('can:content-import');
|
|
|
|
}
|
|
|
|
|
2024-11-03 22:13:05 +08:00
|
|
|
/**
|
|
|
|
* Show the view to start a new import, and also list out the existing
|
|
|
|
* in progress imports that are visible to the user.
|
|
|
|
*/
|
2024-11-04 01:28:18 +08:00
|
|
|
public function start()
|
2024-10-29 20:11:51 +08:00
|
|
|
{
|
2024-11-03 22:13:05 +08:00
|
|
|
$imports = $this->imports->getVisibleImports();
|
|
|
|
|
|
|
|
$this->setPageTitle(trans('entities.import'));
|
2024-10-29 22:21:32 +08:00
|
|
|
|
2024-11-02 22:51:04 +08:00
|
|
|
return view('exports.import', [
|
2024-11-03 22:13:05 +08:00
|
|
|
'imports' => $imports,
|
2024-11-02 22:51:04 +08:00
|
|
|
'zipErrors' => session()->pull('validation_errors') ?? [],
|
|
|
|
]);
|
2024-10-29 20:11:51 +08:00
|
|
|
}
|
|
|
|
|
2024-11-03 22:13:05 +08:00
|
|
|
/**
|
|
|
|
* Upload, validate and store an import file.
|
|
|
|
*/
|
2024-10-29 20:11:51 +08:00
|
|
|
public function upload(Request $request)
|
|
|
|
{
|
2024-10-30 21:13:41 +08:00
|
|
|
$this->validate($request, [
|
2024-11-03 04:48:21 +08:00
|
|
|
'file' => ['required', ...AttachmentService::getFileValidationRules()]
|
2024-10-30 21:13:41 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
$file = $request->file('file');
|
2024-11-03 04:48:21 +08:00
|
|
|
try {
|
|
|
|
$import = $this->imports->storeFromUpload($file);
|
|
|
|
} catch (ZipValidationException $exception) {
|
|
|
|
session()->flash('validation_errors', $exception->errors);
|
2024-11-02 22:51:04 +08:00
|
|
|
return redirect('/import');
|
2024-10-30 23:26:23 +08:00
|
|
|
}
|
2024-11-02 22:51:04 +08:00
|
|
|
|
2024-11-03 22:13:05 +08:00
|
|
|
$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)
|
|
|
|
{
|
|
|
|
$import = $this->imports->findVisible($id);
|
|
|
|
|
|
|
|
$this->setPageTitle(trans('entities.import_continue'));
|
|
|
|
|
|
|
|
return view('exports.import-show', [
|
|
|
|
'import' => $import,
|
2024-11-05 21:17:31 +08:00
|
|
|
'data' => $import->decodeMetadata(),
|
2024-11-03 22:13:05 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2024-11-05 00:21:22 +08:00
|
|
|
public function run(int $id, Request $request)
|
2024-11-04 01:28:18 +08:00
|
|
|
{
|
|
|
|
// TODO - Test access/visibility
|
|
|
|
$import = $this->imports->findVisible($id);
|
2024-11-05 00:21:22 +08:00
|
|
|
$parent = null;
|
|
|
|
|
|
|
|
if ($import->getType() === 'page' || $import->getType() === 'chapter') {
|
|
|
|
$data = $this->validate($request, [
|
|
|
|
'parent' => ['required', 'string']
|
|
|
|
]);
|
|
|
|
$parent = $data['parent'];
|
|
|
|
}
|
2024-11-04 01:28:18 +08:00
|
|
|
|
|
|
|
// TODO - Run import
|
2024-11-05 00:21:22 +08:00
|
|
|
// TODO - Validate again before
|
|
|
|
// TODO - Check permissions before (create for main item, create for children, create for related items [image, attachments])
|
2024-11-04 01:28:18 +08:00
|
|
|
// TODO - Redirect to result
|
2024-11-05 21:17:31 +08:00
|
|
|
// TODO - Or redirect back with errors
|
2024-11-04 01:28:18 +08:00
|
|
|
}
|
|
|
|
|
2024-11-03 22:13:05 +08:00
|
|
|
/**
|
|
|
|
* Delete an active pending import from the filesystem and database.
|
|
|
|
*/
|
|
|
|
public function delete(int $id)
|
|
|
|
{
|
|
|
|
$import = $this->imports->findVisible($id);
|
|
|
|
$this->imports->deleteImport($import);
|
|
|
|
|
|
|
|
$this->logActivity(ActivityType::IMPORT_DELETE, $import);
|
|
|
|
|
|
|
|
return redirect('/import');
|
2024-10-29 20:11:51 +08:00
|
|
|
}
|
|
|
|
}
|