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

View File

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

View File

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