Properly handle 404 for editing files (#816)

This commit is contained in:
Boy132 2024-12-12 18:26:01 +01:00 committed by GitHub
parent 026494c353
commit 771eece01e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 19 additions and 10 deletions

View File

@ -24,6 +24,7 @@ use Filament\Panel;
use Filament\Resources\Pages\Page; use Filament\Resources\Pages\Page;
use Filament\Resources\Pages\PageRegistration; use Filament\Resources\Pages\PageRegistration;
use Filament\Support\Enums\Alignment; use Filament\Support\Enums\Alignment;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Routing\Route; use Illuminate\Routing\Route;
use Illuminate\Support\Facades\Route as RouteFacade; use Illuminate\Support\Facades\Route as RouteFacade;
use Livewire\Attributes\Locked; use Livewire\Attributes\Locked;
@ -73,11 +74,10 @@ 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 () use ($server) { ->action(function (DaemonFileRepository $fileRepository) use ($server) {
$data = $this->form->getState(); $data = $this->form->getState();
// @phpstan-ignore-next-line $fileRepository
app(DaemonFileRepository::class)
->setServer($server) ->setServer($server)
->putContent($this->path, $data['editor'] ?? ''); ->putContent($this->path, $data['editor'] ?? '');
@ -105,11 +105,14 @@ class EditFiles extends Page
MonacoEditor::make('editor') MonacoEditor::make('editor')
->label('') ->label('')
->placeholderText('') ->placeholderText('')
->formatStateUsing(function () use ($server) { ->formatStateUsing(function (DaemonFileRepository $fileRepository) use ($server) {
// @phpstan-ignore-next-line try {
return app(DaemonFileRepository::class) return $fileRepository
->setServer($server) ->setServer($server)
->getContent($this->path, config('panel.files.max_edit_size')); ->getContent($this->path, config('panel.files.max_edit_size'));
} catch (FileNotFoundException) {
abort(404, $this->path . ' not found.');
}
}) })
->language(fn (Get $get) => $get('lang') ?? 'plaintext') ->language(fn (Get $get) => $get('lang') ?? 'plaintext')
->view('filament.plugins.monaco-editor'), ->view('filament.plugins.monaco-editor'),

View File

@ -3,6 +3,7 @@
namespace App\Repositories\Daemon; namespace App\Repositories\Daemon;
use Carbon\CarbonInterval; use Carbon\CarbonInterval;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Http\Client\Response; use Illuminate\Http\Client\Response;
use Webmozart\Assert\Assert; use Webmozart\Assert\Assert;
use App\Models\Server; use App\Models\Server;
@ -21,6 +22,7 @@ class DaemonFileRepository extends DaemonRepository
* @throws \GuzzleHttp\Exception\TransferException * @throws \GuzzleHttp\Exception\TransferException
* @throws \App\Exceptions\Http\Server\FileSizeTooLargeException * @throws \App\Exceptions\Http\Server\FileSizeTooLargeException
* @throws \App\Exceptions\Http\Connection\DaemonConnectionException * @throws \App\Exceptions\Http\Connection\DaemonConnectionException
* @throws FileNotFoundException
*/ */
public function getContent(string $path, ?int $notLargerThan = null): string public function getContent(string $path, ?int $notLargerThan = null): string
{ {
@ -40,6 +42,10 @@ class DaemonFileRepository extends DaemonRepository
throw new FileSizeTooLargeException(); throw new FileSizeTooLargeException();
} }
if ($response->getStatusCode() === 404) {
throw new FileNotFoundException();
}
return $response; return $response;
} }

View File

@ -1,8 +1,8 @@
@props([ @props([
'code' => '404', 'code' => '404',
'title' => 'Not found', 'title' => 'Not found',
'subtitle' => 'The requested resource was not found.', 'subtitle' => $exception->getMessage() ?? 'The requested resource was not found.',
'icon' => 'tabler-zoom-question' 'icon' => 'tabler-zoom-question'
]) ])
@extends('errors::layout') @extends('errors::layout')