mirror of
https://github.com/pelican-dev/panel.git
synced 2025-05-20 00:34: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 AbdelhamidErrahmouni\FilamentMonacoEditor\MonacoEditor;
|
||||||
use App\Enums\EditorLanguages;
|
use App\Enums\EditorLanguages;
|
||||||
|
use App\Exceptions\Http\Server\FileSizeTooLargeException;
|
||||||
|
use App\Exceptions\Repository\FileNotEditableException;
|
||||||
use App\Facades\Activity;
|
use App\Facades\Activity;
|
||||||
use App\Filament\Server\Resources\FileResource;
|
use App\Filament\Server\Resources\FileResource;
|
||||||
use App\Livewire\AlertBanner;
|
use App\Livewire\AlertBanner;
|
||||||
@ -45,6 +47,8 @@ class EditFiles extends Page
|
|||||||
#[Locked]
|
#[Locked]
|
||||||
public string $path;
|
public string $path;
|
||||||
|
|
||||||
|
private DaemonFileRepository $fileRepository;
|
||||||
|
|
||||||
/** @var array<mixed> */
|
/** @var array<mixed> */
|
||||||
public ?array $data = [];
|
public ?array $data = [];
|
||||||
|
|
||||||
@ -66,11 +70,9 @@ class EditFiles extends Page
|
|||||||
->authorize(fn () => auth()->user()->can(Permission::ACTION_FILE_UPDATE, $server))
|
->authorize(fn () => auth()->user()->can(Permission::ACTION_FILE_UPDATE, $server))
|
||||||
->icon('tabler-device-floppy')
|
->icon('tabler-device-floppy')
|
||||||
->keyBindings('mod+shift+s')
|
->keyBindings('mod+shift+s')
|
||||||
->action(function (DaemonFileRepository $fileRepository) use ($server) {
|
->action(function (array $data) {
|
||||||
$data = $this->form->getState();
|
|
||||||
|
|
||||||
$fileRepository
|
$this->getDaemonFileRepository()
|
||||||
->setServer($server)
|
|
||||||
->putContent($this->path, $data['editor'] ?? '');
|
->putContent($this->path, $data['editor'] ?? '');
|
||||||
|
|
||||||
Activity::event('server:file.write')
|
Activity::event('server:file.write')
|
||||||
@ -90,11 +92,9 @@ class EditFiles extends Page
|
|||||||
->authorize(fn () => auth()->user()->can(Permission::ACTION_FILE_UPDATE, $server))
|
->authorize(fn () => auth()->user()->can(Permission::ACTION_FILE_UPDATE, $server))
|
||||||
->icon('tabler-device-floppy')
|
->icon('tabler-device-floppy')
|
||||||
->keyBindings('mod+s')
|
->keyBindings('mod+s')
|
||||||
->action(function (DaemonFileRepository $fileRepository) use ($server) {
|
->action(function (array $data) {
|
||||||
$data = $this->form->getState();
|
|
||||||
|
|
||||||
$fileRepository
|
$this->getDaemonFileRepository()
|
||||||
->setServer($server)
|
|
||||||
->putContent($this->path, $data['editor'] ?? '');
|
->putContent($this->path, $data['editor'] ?? '');
|
||||||
|
|
||||||
Activity::event('server:file.write')
|
Activity::event('server:file.write')
|
||||||
@ -123,15 +123,36 @@ class EditFiles extends Page
|
|||||||
->afterStateUpdated(fn ($state) => $this->dispatch('setLanguage', lang: $state))
|
->afterStateUpdated(fn ($state) => $this->dispatch('setLanguage', lang: $state))
|
||||||
->default(fn () => EditorLanguages::fromWithAlias(pathinfo($this->path, PATHINFO_EXTENSION))),
|
->default(fn () => EditorLanguages::fromWithAlias(pathinfo($this->path, PATHINFO_EXTENSION))),
|
||||||
MonacoEditor::make('editor')
|
MonacoEditor::make('editor')
|
||||||
->label('')
|
->hiddenLabel()
|
||||||
->placeholderText('')
|
->showPlaceholder(false)
|
||||||
->default(function (DaemonFileRepository $fileRepository) use ($server) {
|
->default(function () {
|
||||||
try {
|
try {
|
||||||
return $fileRepository
|
return $this->getDaemonFileRepository()
|
||||||
->setServer($server)
|
|
||||||
->getContent($this->path, config('panel.files.max_edit_size'));
|
->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) {
|
} 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'))
|
->language(fn (Get $get) => $get('lang'))
|
||||||
@ -200,6 +221,15 @@ class EditFiles extends Page
|
|||||||
return $breadcrumbs;
|
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
|
public static function route(string $path): PageRegistration
|
||||||
{
|
{
|
||||||
return new PageRegistration(
|
return new PageRegistration(
|
||||||
|
@ -5,6 +5,7 @@ namespace App\Repositories\Daemon;
|
|||||||
use Illuminate\Contracts\Filesystem\FileNotFoundException;
|
use Illuminate\Contracts\Filesystem\FileNotFoundException;
|
||||||
use Illuminate\Http\Client\Response;
|
use Illuminate\Http\Client\Response;
|
||||||
use App\Exceptions\Http\Server\FileSizeTooLargeException;
|
use App\Exceptions\Http\Server\FileSizeTooLargeException;
|
||||||
|
use App\Exceptions\Repository\FileNotEditableException;
|
||||||
use Illuminate\Http\Client\ConnectionException;
|
use Illuminate\Http\Client\ConnectionException;
|
||||||
|
|
||||||
class DaemonFileRepository extends DaemonRepository
|
class DaemonFileRepository extends DaemonRepository
|
||||||
@ -29,6 +30,10 @@ class DaemonFileRepository extends DaemonRepository
|
|||||||
throw new FileSizeTooLargeException();
|
throw new FileSizeTooLargeException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($response->getStatusCode() === 400) {
|
||||||
|
throw new FileNotEditableException();
|
||||||
|
}
|
||||||
|
|
||||||
if ($response->getStatusCode() === 404) {
|
if ($response->getStatusCode() === 404) {
|
||||||
throw new FileNotFoundException();
|
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