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)])); + } }) ->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('' . $path . ' already exists!') + ->danger() + ->closable() + ->send(); + + $this->redirect(self::getUrl(['path' => dirname($path)])); + } }) ->form([ TextInput::make('name') 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 (