1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-08-09 10:22:51 +03:00

ZIP Imports: Added listing, show view, delete, activity

This commit is contained in:
Dan Brown
2024-11-03 14:13:05 +00:00
parent 8ea3855e02
commit c6109c7087
11 changed files with 193 additions and 6 deletions

View File

@@ -67,6 +67,10 @@ class ActivityType
const WEBHOOK_UPDATE = 'webhook_update';
const WEBHOOK_DELETE = 'webhook_delete';
const IMPORT_CREATE = 'import_create';
const IMPORT_RUN = 'import_run';
const IMPORT_DELETE = 'import_delete';
/**
* Get all the possible values.
*/

View File

@@ -1,7 +1,10 @@
<?php
declare(strict_types=1);
namespace BookStack\Exports\Controllers;
use BookStack\Activity\ActivityType;
use BookStack\Exceptions\ZipValidationException;
use BookStack\Exports\ImportRepo;
use BookStack\Http\Controller;
@@ -16,15 +19,26 @@ class ImportController extends Controller
$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', [
'imports' => $imports,
'zipErrors' => session()->pull('validation_errors') ?? [],
]);
}
/**
* Upload, validate and store an import file.
*/
public function upload(Request $request)
{
$this->validate($request, [
@@ -39,6 +53,38 @@ class ImportController extends Controller
return redirect('/import');
}
return redirect("imports/{$import->id}");
$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');
}
}

View File

@@ -2,6 +2,7 @@
namespace BookStack\Exports;
use BookStack\Activity\Models\Loggable;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
@@ -17,7 +18,7 @@ use Illuminate\Database\Eloquent\Model;
* @property Carbon $created_at
* @property Carbon $updated_at
*/
class Import extends Model
class Import extends Model implements Loggable
{
use HasFactory;
@@ -38,4 +39,24 @@ class Import extends Model
return self::TYPE_PAGE;
}
public function getSizeString(): string
{
$mb = round($this->size / 1000000, 2);
return "{$mb} MB";
}
/**
* Get the URL to view/continue this import.
*/
public function getUrl(string $path = ''): string
{
$path = ltrim($path, '/');
return url("/import/{$this->id}" . ($path ? '/' . $path : ''));
}
public function logDescriptor(): string
{
return "({$this->id}) {$this->name}";
}
}

View File

@@ -6,6 +6,7 @@ use BookStack\Exceptions\ZipValidationException;
use BookStack\Exports\ZipExports\ZipExportReader;
use BookStack\Exports\ZipExports\ZipExportValidator;
use BookStack\Uploads\FileStorage;
use Illuminate\Database\Eloquent\Collection;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class ImportRepo
@@ -15,6 +16,31 @@ class ImportRepo
) {
}
/**
* @return Collection<Import>
*/
public function getVisibleImports(): Collection
{
$query = Import::query();
if (!userCan('settings-manage')) {
$query->where('created_by', user()->id);
}
return $query->get();
}
public function findVisible(int $id): Import
{
$query = Import::query();
if (!userCan('settings-manage')) {
$query->where('created_by', user()->id);
}
return $query->findOrFail($id);
}
public function storeFromUpload(UploadedFile $file): Import
{
$zipPath = $file->getRealPath();
@@ -45,4 +71,10 @@ class ImportRepo
return $import;
}
public function deleteImport(Import $import): void
{
$this->storage->delete($import->path);
$import->delete();
}
}

View File

@@ -152,10 +152,8 @@ abstract class Controller extends BaseController
/**
* Log an activity in the system.
*
* @param string|Loggable $detail
*/
protected function logActivity(string $type, $detail = ''): void
protected function logActivity(string $type, string|Loggable $detail = ''): void
{
Activity::add($type, $detail);
}