Add FileExistsException & Fix error reporting (#1417)

This commit is contained in:
MartinOscar 2025-06-26 21:04:33 +02:00 committed by GitHub
parent 4d0aabe91e
commit cdcd1c521e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 59 additions and 10 deletions

View File

@ -0,0 +1,7 @@
<?php
namespace App\Exceptions\Repository;
use Exception;
class FileExistsException extends Exception {}

View File

@ -4,6 +4,7 @@ namespace App\Filament\Server\Resources\FileResource\Pages;
use AbdelhamidErrahmouni\FilamentMonacoEditor\MonacoEditor;
use App\Enums\EditorLanguages;
use App\Exceptions\Repository\FileExistsException;
use App\Facades\Activity;
use App\Filament\Server\Resources\FileResource;
use App\Models\File;
@ -12,6 +13,7 @@ use App\Models\Server;
use App\Repositories\Daemon\DaemonFileRepository;
use App\Filament\Components\Tables\Columns\BytesColumn;
use App\Filament\Components\Tables\Columns\DateTimeColumn;
use App\Livewire\AlertBanner;
use App\Traits\Filament\CanCustomizeHeaderActions;
use App\Traits\Filament\CanCustomizeHeaderWidgets;
use Filament\Actions\Action as HeaderAction;
@ -419,11 +421,22 @@ class ListFiles extends ListRecords
->keyBindings('')
->modalSubmitActionLabel('Create')
->action(function ($data) {
$this->getDaemonFileRepository()->putContent(join_paths($this->path, $data['name']), $data['editor'] ?? '');
$path = join_paths($this->path, $data['name']);
try {
$this->getDaemonFileRepository()->putContent($path, $data['editor'] ?? '');
Activity::event('server:file.write')
->property('file', join_paths($this->path, $data['name']))
->log();
Activity::event('server:file.write')
->property('file', join_paths($path, $data['name']))
->log();
} catch (FileExistsException) {
AlertBanner::make()
->title('<code>' . $path . '</code> already exists!')
->danger()
->closable()
->send();
$this->redirect(self::getUrl(['path' => dirname($path)]));
}
})
->form([
TextInput::make('name')
@ -448,11 +461,22 @@ class ListFiles extends ListRecords
->label('New Folder')
->color('gray')
->action(function ($data) {
$this->getDaemonFileRepository()->createDirectory($data['name'], $this->path);
try {
$this->getDaemonFileRepository()->createDirectory($data['name'], $this->path);
Activity::event('server:file.create-directory')
->property(['directory' => $this->path, 'name' => $data['name']])
->log();
Activity::event('server:file.create-directory')
->property(['directory' => $this->path, 'name' => $data['name']])
->log();
} catch (FileExistsException) {
$path = join_paths($this->path, $data['name']);
AlertBanner::make()
->title('<code>' . $path . '</code> already exists!')
->danger()
->closable()
->send();
$this->redirect(self::getUrl(['path' => dirname($path)]));
}
})
->form([
TextInput::make('name')

View File

@ -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\FileExistsException;
use App\Exceptions\Repository\FileNotEditableException;
use Illuminate\Http\Client\ConnectionException;
@ -46,13 +47,20 @@ class DaemonFileRepository extends DaemonRepository
* a file.
*
* @throws ConnectionException
* @throws FileExistsException
*/
public function putContent(string $path, string $content): Response
{
return $this->getHttpClient()
$response = $this->getHttpClient()
->withQueryParameters(['file' => $path])
->withBody($content)
->post("/api/servers/{$this->server->uuid}/files/write");
if ($response->getStatusCode() === 400) {
throw new FileExistsException();
}
return $response;
}
/**
@ -73,15 +81,22 @@ class DaemonFileRepository extends DaemonRepository
* Creates a new directory for the server in the given $path.
*
* @throws ConnectionException
* @throws FileExistsException
*/
public function createDirectory(string $name, string $path): Response
{
return $this->getHttpClient()->post("/api/servers/{$this->server->uuid}/files/create-directory",
$response = $this->getHttpClient()->post("/api/servers/{$this->server->uuid}/files/create-directory",
[
'name' => $name,
'path' => $path,
]
);
if ($response->getStatusCode() === 400) {
throw new FileExistsException();
}
return $response;
}
/**

View File

@ -57,6 +57,9 @@ abstract class DaemonRepository
if (is_bool($condition)) {
return $condition;
}
if ($condition->clientError()) {
return false;
}
$header = $condition->header('User-Agent');
if (