2024-10-29 20:11:51 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Exports\Controllers;
|
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function start(Request $request)
|
|
|
|
{
|
2024-10-29 22:21:32 +08:00
|
|
|
// TODO - Show existing imports for user (or for all users if admin-level user)
|
|
|
|
|
2024-11-02 22:51:04 +08:00
|
|
|
return view('exports.import', [
|
|
|
|
'zipErrors' => session()->pull('validation_errors') ?? [],
|
|
|
|
]);
|
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 04:48:21 +08:00
|
|
|
return redirect("imports/{$import->id}");
|
2024-10-29 20:11:51 +08:00
|
|
|
}
|
|
|
|
}
|