diff --git a/app/Exceptions/Repository/FileExistsException.php b/app/Exceptions/Repository/FileExistsException.php new file mode 100644 index 000000000..b71649483 --- /dev/null +++ b/app/Exceptions/Repository/FileExistsException.php @@ -0,0 +1,7 @@ +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('' . $path . ' already exists!') + ->danger() + ->closable() + ->send(); + + $this->redirect(self::getUrl(['path' => dirname($path)])); + } }) ->schema([ TextInput::make('name') @@ -439,11 +452,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('' . $path . ' already exists!') + ->danger() + ->closable() + ->send(); + + $this->redirect(self::getUrl(['path' => dirname($path)])); + } }) ->schema([ TextInput::make('name') diff --git a/app/Filament/Server/Resources/ScheduleResource/RelationManagers/TasksRelationManager.php b/app/Filament/Server/Resources/ScheduleResource/RelationManagers/TasksRelationManager.php index 2bed60d08..93c05e30c 100644 --- a/app/Filament/Server/Resources/ScheduleResource/RelationManagers/TasksRelationManager.php +++ b/app/Filament/Server/Resources/ScheduleResource/RelationManagers/TasksRelationManager.php @@ -14,6 +14,7 @@ use Filament\Forms\Components\TextInput; use Filament\Forms\Components\Toggle; use Filament\Schemas\Components\Utilities\Get; use Filament\Actions\EditAction; +use Filament\Schemas\Components\Utilities\Set; use Filament\Tables\Table; use Filament\Resources\RelationManagers\RelationManager; use Filament\Actions\CreateAction; @@ -48,10 +49,11 @@ class TasksRelationManager extends RelationManager Select::make('action') ->required() ->live() - ->disableOptionWhen(fn (string $value): bool => $value === Task::ACTION_BACKUP && $schedule->server->backup_limit === 0) + ->disableOptionWhen(fn (string $value) => $value === Task::ACTION_BACKUP && $schedule->server->backup_limit === 0) ->options($this->getActionOptions()) ->selectablePlaceholder(false) - ->default(Task::ACTION_POWER), + ->default(Task::ACTION_POWER) + ->afterStateUpdated(fn ($state, Set $set) => $set('payload', $state === Task::ACTION_POWER ? 'restart' : null)), Textarea::make('payload') ->hidden(fn (Get $get) => $get('action') === Task::ACTION_POWER) ->label(fn (Get $get) => $this->getActionOptions(false)[$get('action')] ?? 'Payload'), @@ -87,7 +89,8 @@ class TasksRelationManager extends RelationManager $schedule = $this->getOwnerRecord(); return $table - ->reorderable('sequence_id', true) + ->reorderable('sequence_id') + ->defaultSort('sequence_id') ->columns([ TextColumn::make('action') ->state(fn (Task $task) => $this->getActionOptions()[$task->action] ?? $task->action), diff --git a/app/Repositories/Daemon/DaemonFileRepository.php b/app/Repositories/Daemon/DaemonFileRepository.php index f7d5c78e3..b1385fe92 100644 --- a/app/Repositories/Daemon/DaemonFileRepository.php +++ b/app/Repositories/Daemon/DaemonFileRepository.php @@ -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; } /** diff --git a/app/Repositories/Daemon/DaemonRepository.php b/app/Repositories/Daemon/DaemonRepository.php index 6b3b553d6..6d58e7646 100644 --- a/app/Repositories/Daemon/DaemonRepository.php +++ b/app/Repositories/Daemon/DaemonRepository.php @@ -57,6 +57,9 @@ abstract class DaemonRepository if (is_bool($condition)) { return $condition; } + if ($condition->clientError()) { + return false; + } $header = $condition->header('User-Agent'); if (