From 05c4610654bb24c6c2a671a8f22aff4daab349d9 Mon Sep 17 00:00:00 2001 From: Charles Date: Fri, 19 Apr 2024 12:59:29 -0400 Subject: [PATCH 1/3] Allow uploading multiple eggs I'm sure there is a cleaner way to do it, but this works :) Also ran pint... --- .../Resources/EggResource/Pages/ListEggs.php | 11 +++--- app/Filament/Resources/ServerResource.php | 3 +- app/Models/Node.php | 2 +- .../Eggs/Sharing/EggImporterService.php | 37 +++++++++++-------- .../views/filament/pages/dashboard.blade.php | 12 +----- 5 files changed, 31 insertions(+), 34 deletions(-) diff --git a/app/Filament/Resources/EggResource/Pages/ListEggs.php b/app/Filament/Resources/EggResource/Pages/ListEggs.php index 534b483892..036ec005e0 100644 --- a/app/Filament/Resources/EggResource/Pages/ListEggs.php +++ b/app/Filament/Resources/EggResource/Pages/ListEggs.php @@ -21,11 +21,12 @@ protected function getHeaderActions(): array Actions\CreateAction::make(), Actions\Action::make('import') - ->label('Import Egg') + ->label('Import') ->form([ Forms\Components\FileUpload::make('egg') ->acceptedFileTypes(['application/json']) - ->storeFiles(false), + ->storeFiles(false) + ->multiple(), ]) ->action(function (array $data): void { /** @var TemporaryUploadedFile $eggFile */ @@ -35,7 +36,7 @@ protected function getHeaderActions(): array $eggImportService = resolve(EggImporterService::class); try { - $newEgg = $eggImportService->handle($eggFile); + $eggImportService->handle($eggFile); } catch (Exception $exception) { Notification::make() ->title('Egg Import Failed') @@ -48,11 +49,9 @@ protected function getHeaderActions(): array } Notification::make() - ->title("Egg Import Success: $newEgg->name") + ->title('Egg Import Success') ->success() ->send(); - - redirect()->route('filament.admin.resources.eggs.edit', [$newEgg]); }), ]; } diff --git a/app/Filament/Resources/ServerResource.php b/app/Filament/Resources/ServerResource.php index f21a2d0b6a..9ce4e5a0f0 100644 --- a/app/Filament/Resources/ServerResource.php +++ b/app/Filament/Resources/ServerResource.php @@ -207,6 +207,7 @@ public static function form(Form $form): Form // Do not add non numerical ports $update = true; + continue; } @@ -217,7 +218,7 @@ public static function form(Form $form): Form } $start = max((int) $start, 0); - $end = min((int) $end, 2**16-1); + $end = min((int) $end, 2 ** 16 - 1); for ($i = $start; $i <= $end; $i++) { $ports->push($i); } diff --git a/app/Models/Node.php b/app/Models/Node.php index e421643573..9abf8aea22 100644 --- a/app/Models/Node.php +++ b/app/Models/Node.php @@ -307,7 +307,7 @@ public function ipAddresses(): array $ips = collect(); if (is_ip($this->fqdn)) { $ips->push($this->fqdn); - } else if ($dnsRecords = gethostbynamel($this->fqdn)) { + } elseif ($dnsRecords = gethostbynamel($this->fqdn)) { $ips->concat($dnsRecords); } diff --git a/app/Services/Eggs/Sharing/EggImporterService.php b/app/Services/Eggs/Sharing/EggImporterService.php index 0a949474f9..45648870a3 100644 --- a/app/Services/Eggs/Sharing/EggImporterService.php +++ b/app/Services/Eggs/Sharing/EggImporterService.php @@ -5,7 +5,6 @@ use Ramsey\Uuid\Uuid; use Illuminate\Support\Arr; use App\Models\Egg; -use Illuminate\Http\UploadedFile; use App\Models\EggVariable; use Illuminate\Database\ConnectionInterface; use App\Services\Eggs\EggParserService; @@ -21,25 +20,31 @@ public function __construct(protected ConnectionInterface $connection, protected * * @throws \App\Exceptions\Service\InvalidFileUploadException|\Throwable */ - public function handle(UploadedFile $file): Egg + public function handle(array $files): array { - $parsed = $this->parser->handle($file); + $eggs = []; - return $this->connection->transaction(function () use ($parsed) { - $egg = (new Egg())->forceFill([ - 'uuid' => Uuid::uuid4()->toString(), - 'author' => Arr::get($parsed, 'author'), - 'copy_script_from' => null, - ]); + foreach ($files as $file) { + $parsed = $this->parser->handle($file); - $egg = $this->parser->fillFromParsed($egg, $parsed); - $egg->save(); + $egg = $this->connection->transaction(function () use ($parsed) { + $egg = (new Egg())->forceFill([ + 'uuid' => Uuid::uuid4()->toString(), + 'author' => Arr::get($parsed, 'author'), + 'copy_script_from' => null, + ]); - foreach ($parsed['variables'] ?? [] as $variable) { - EggVariable::query()->forceCreate(array_merge($variable, ['egg_id' => $egg->id])); - } + $egg = $this->parser->fillFromParsed($egg, $parsed); + $egg->save(); - return $egg; - }); + foreach ($parsed['variables'] ?? [] as $variable) { + EggVariable::query()->forceCreate(array_merge($variable, ['egg_id' => $egg->id])); + } + + return $egg; + }); + } + + return $eggs; } } diff --git a/resources/views/filament/pages/dashboard.blade.php b/resources/views/filament/pages/dashboard.blade.php index c927f20d0c..46d601dad7 100644 --- a/resources/views/filament/pages/dashboard.blade.php +++ b/resources/views/filament/pages/dashboard.blade.php @@ -1,12 +1,10 @@ - - Panel's Resources: + + Overview: Nodes {{ $nodesCount }} @@ -14,8 +12,6 @@ Servers {{ $serversCount }} @@ -23,8 +19,6 @@ Eggs {{ $eggsCount }} @@ -32,8 +26,6 @@ Users {{ $usersCount }} From ac3a36e4891c3f24e03cbbbefebc25646eaf9729 Mon Sep 17 00:00:00 2001 From: notCharles Date: Sat, 20 Apr 2024 18:08:05 -0400 Subject: [PATCH 2/3] Updates --- .../Resources/EggResource/Pages/ListEggs.php | 24 ++++++------ .../Eggs/Sharing/EggImporterService.php | 37 ++++++++----------- 2 files changed, 29 insertions(+), 32 deletions(-) diff --git a/app/Filament/Resources/EggResource/Pages/ListEggs.php b/app/Filament/Resources/EggResource/Pages/ListEggs.php index 036ec005e0..b5db133a42 100644 --- a/app/Filament/Resources/EggResource/Pages/ListEggs.php +++ b/app/Filament/Resources/EggResource/Pages/ListEggs.php @@ -35,17 +35,19 @@ protected function getHeaderActions(): array /** @var EggImporterService $eggImportService */ $eggImportService = resolve(EggImporterService::class); - try { - $eggImportService->handle($eggFile); - } catch (Exception $exception) { - Notification::make() - ->title('Egg Import Failed') - ->danger() - ->send(); - - report($exception); - - return; + foreach ($eggFile as $file) { + try { + $eggImportService->handle($file); + } catch (Exception $exception) { + Notification::make() + ->title('Egg Import Failed') + ->danger() + ->send(); + + report($exception); + + return; + } } Notification::make() diff --git a/app/Services/Eggs/Sharing/EggImporterService.php b/app/Services/Eggs/Sharing/EggImporterService.php index 45648870a3..0a949474f9 100644 --- a/app/Services/Eggs/Sharing/EggImporterService.php +++ b/app/Services/Eggs/Sharing/EggImporterService.php @@ -5,6 +5,7 @@ use Ramsey\Uuid\Uuid; use Illuminate\Support\Arr; use App\Models\Egg; +use Illuminate\Http\UploadedFile; use App\Models\EggVariable; use Illuminate\Database\ConnectionInterface; use App\Services\Eggs\EggParserService; @@ -20,31 +21,25 @@ public function __construct(protected ConnectionInterface $connection, protected * * @throws \App\Exceptions\Service\InvalidFileUploadException|\Throwable */ - public function handle(array $files): array + public function handle(UploadedFile $file): Egg { - $eggs = []; + $parsed = $this->parser->handle($file); - foreach ($files as $file) { - $parsed = $this->parser->handle($file); + return $this->connection->transaction(function () use ($parsed) { + $egg = (new Egg())->forceFill([ + 'uuid' => Uuid::uuid4()->toString(), + 'author' => Arr::get($parsed, 'author'), + 'copy_script_from' => null, + ]); - $egg = $this->connection->transaction(function () use ($parsed) { - $egg = (new Egg())->forceFill([ - 'uuid' => Uuid::uuid4()->toString(), - 'author' => Arr::get($parsed, 'author'), - 'copy_script_from' => null, - ]); + $egg = $this->parser->fillFromParsed($egg, $parsed); + $egg->save(); - $egg = $this->parser->fillFromParsed($egg, $parsed); - $egg->save(); + foreach ($parsed['variables'] ?? [] as $variable) { + EggVariable::query()->forceCreate(array_merge($variable, ['egg_id' => $egg->id])); + } - foreach ($parsed['variables'] ?? [] as $variable) { - EggVariable::query()->forceCreate(array_merge($variable, ['egg_id' => $egg->id])); - } - - return $egg; - }); - } - - return $eggs; + return $egg; + }); } } From db67c64da0608cde5153db9cb5b4d5ba80975643 Mon Sep 17 00:00:00 2001 From: notCharles Date: Sun, 21 Apr 2024 09:52:08 -0400 Subject: [PATCH 3/3] We already know we're importing eggs... --- app/Filament/Resources/EggResource/Pages/ListEggs.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Filament/Resources/EggResource/Pages/ListEggs.php b/app/Filament/Resources/EggResource/Pages/ListEggs.php index b5db133a42..00be09020f 100644 --- a/app/Filament/Resources/EggResource/Pages/ListEggs.php +++ b/app/Filament/Resources/EggResource/Pages/ListEggs.php @@ -40,7 +40,7 @@ protected function getHeaderActions(): array $eggImportService->handle($file); } catch (Exception $exception) { Notification::make() - ->title('Egg Import Failed') + ->title('Import Failed') ->danger() ->send(); @@ -51,7 +51,7 @@ protected function getHeaderActions(): array } Notification::make() - ->title('Egg Import Success') + ->title('Import Success') ->success() ->send(); }),