mirror of
https://github.com/pelican-dev/panel.git
synced 2025-05-19 21:04:44 +02:00
Add FileNotEditableException
(#1135)
* Add `FileNotEditableException` * Send `Notification` instead of Throwing * Remove useless `function` * Make them all `AlertBanner`
This commit is contained in:
parent
ed88ce9ae3
commit
636279c6eb
7
app/Exceptions/Repository/FileNotEditableException.php
Normal file
7
app/Exceptions/Repository/FileNotEditableException.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exceptions\Repository;
|
||||
|
||||
use Exception;
|
||||
|
||||
class FileNotEditableException extends Exception {}
|
@ -4,6 +4,8 @@ namespace App\Filament\Server\Resources\FileResource\Pages;
|
||||
|
||||
use AbdelhamidErrahmouni\FilamentMonacoEditor\MonacoEditor;
|
||||
use App\Enums\EditorLanguages;
|
||||
use App\Exceptions\Http\Server\FileSizeTooLargeException;
|
||||
use App\Exceptions\Repository\FileNotEditableException;
|
||||
use App\Facades\Activity;
|
||||
use App\Filament\Server\Resources\FileResource;
|
||||
use App\Livewire\AlertBanner;
|
||||
@ -45,6 +47,8 @@ class EditFiles extends Page
|
||||
#[Locked]
|
||||
public string $path;
|
||||
|
||||
private DaemonFileRepository $fileRepository;
|
||||
|
||||
/** @var array<mixed> */
|
||||
public ?array $data = [];
|
||||
|
||||
@ -66,11 +70,9 @@ class EditFiles extends Page
|
||||
->authorize(fn () => auth()->user()->can(Permission::ACTION_FILE_UPDATE, $server))
|
||||
->icon('tabler-device-floppy')
|
||||
->keyBindings('mod+shift+s')
|
||||
->action(function (DaemonFileRepository $fileRepository) use ($server) {
|
||||
$data = $this->form->getState();
|
||||
->action(function (array $data) {
|
||||
|
||||
$fileRepository
|
||||
->setServer($server)
|
||||
$this->getDaemonFileRepository()
|
||||
->putContent($this->path, $data['editor'] ?? '');
|
||||
|
||||
Activity::event('server:file.write')
|
||||
@ -90,11 +92,9 @@ class EditFiles extends Page
|
||||
->authorize(fn () => auth()->user()->can(Permission::ACTION_FILE_UPDATE, $server))
|
||||
->icon('tabler-device-floppy')
|
||||
->keyBindings('mod+s')
|
||||
->action(function (DaemonFileRepository $fileRepository) use ($server) {
|
||||
$data = $this->form->getState();
|
||||
->action(function (array $data) {
|
||||
|
||||
$fileRepository
|
||||
->setServer($server)
|
||||
$this->getDaemonFileRepository()
|
||||
->putContent($this->path, $data['editor'] ?? '');
|
||||
|
||||
Activity::event('server:file.write')
|
||||
@ -123,15 +123,36 @@ class EditFiles extends Page
|
||||
->afterStateUpdated(fn ($state) => $this->dispatch('setLanguage', lang: $state))
|
||||
->default(fn () => EditorLanguages::fromWithAlias(pathinfo($this->path, PATHINFO_EXTENSION))),
|
||||
MonacoEditor::make('editor')
|
||||
->label('')
|
||||
->placeholderText('')
|
||||
->default(function (DaemonFileRepository $fileRepository) use ($server) {
|
||||
->hiddenLabel()
|
||||
->showPlaceholder(false)
|
||||
->default(function () {
|
||||
try {
|
||||
return $fileRepository
|
||||
->setServer($server)
|
||||
return $this->getDaemonFileRepository()
|
||||
->getContent($this->path, config('panel.files.max_edit_size'));
|
||||
} catch (FileSizeTooLargeException) {
|
||||
AlertBanner::make()
|
||||
->title('File too large!')
|
||||
->body('<code>' . $this->path . '</code> Max is ' . convert_bytes_to_readable(config('panel.files.max_edit_size')))
|
||||
->danger()
|
||||
->closable()
|
||||
->send();
|
||||
$this->redirect(ListFiles::getUrl());
|
||||
} catch (FileNotFoundException) {
|
||||
abort(404, $this->path . ' not found.');
|
||||
AlertBanner::make()
|
||||
->title('File Not found!')
|
||||
->body('<code>' . $this->path . '</code>')
|
||||
->danger()
|
||||
->closable()
|
||||
->send();
|
||||
$this->redirect(ListFiles::getUrl());
|
||||
} catch (FileNotEditableException) {
|
||||
AlertBanner::make()
|
||||
->title('Could not edit directory!')
|
||||
->body('<code>' . $this->path . '</code>')
|
||||
->danger()
|
||||
->closable()
|
||||
->send();
|
||||
$this->redirect(ListFiles::getUrl());
|
||||
}
|
||||
})
|
||||
->language(fn (Get $get) => $get('lang'))
|
||||
@ -200,6 +221,15 @@ class EditFiles extends Page
|
||||
return $breadcrumbs;
|
||||
}
|
||||
|
||||
private function getDaemonFileRepository(): DaemonFileRepository
|
||||
{
|
||||
/** @var Server $server */
|
||||
$server = Filament::getTenant();
|
||||
$this->fileRepository ??= (new DaemonFileRepository())->setServer($server);
|
||||
|
||||
return $this->fileRepository;
|
||||
}
|
||||
|
||||
public static function route(string $path): PageRegistration
|
||||
{
|
||||
return new PageRegistration(
|
||||
|
@ -5,6 +5,7 @@ namespace App\Repositories\Daemon;
|
||||
use Illuminate\Contracts\Filesystem\FileNotFoundException;
|
||||
use Illuminate\Http\Client\Response;
|
||||
use App\Exceptions\Http\Server\FileSizeTooLargeException;
|
||||
use App\Exceptions\Repository\FileNotEditableException;
|
||||
use Illuminate\Http\Client\ConnectionException;
|
||||
|
||||
class DaemonFileRepository extends DaemonRepository
|
||||
@ -29,6 +30,10 @@ class DaemonFileRepository extends DaemonRepository
|
||||
throw new FileSizeTooLargeException();
|
||||
}
|
||||
|
||||
if ($response->getStatusCode() === 400) {
|
||||
throw new FileNotEditableException();
|
||||
}
|
||||
|
||||
if ($response->getStatusCode() === 404) {
|
||||
throw new FileNotFoundException();
|
||||
}
|
||||
|
8
resources/views/errors/400.blade.php
Normal file
8
resources/views/errors/400.blade.php
Normal file
@ -0,0 +1,8 @@
|
||||
@props([
|
||||
'code' => '400',
|
||||
'title' => 'Bad request',
|
||||
'subtitle' => $exception->getMessage(),
|
||||
'icon' => 'tabler-exclamation-circle'
|
||||
])
|
||||
|
||||
@extends('errors::layout')
|
Loading…
x
Reference in New Issue
Block a user