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-10-29 20:11:51 +08:00
|
|
|
public function start(Request $request)
|
|
|
|
{
|
2024-11-03 22:13:05 +08:00
|
|
|
// TODO - Test visibility access for listed items
|
|
|
|
$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)
|
|
|
|
{
|
|
|
|
// 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');
|
2024-10-29 20:11:51 +08:00
|
|
|
}
|
|
|
|
}
|