Allow uploading multiple eggs

I'm sure there is a cleaner way to do it, but this works :)

Also ran pint...
This commit is contained in:
Charles 2024-04-19 12:59:29 -04:00
parent 82c294ab63
commit 05c4610654
5 changed files with 31 additions and 34 deletions

View File

@ -21,11 +21,12 @@ class ListEggs extends ListRecords
Actions\CreateAction::make(), Actions\CreateAction::make(),
Actions\Action::make('import') Actions\Action::make('import')
->label('Import Egg') ->label('Import')
->form([ ->form([
Forms\Components\FileUpload::make('egg') Forms\Components\FileUpload::make('egg')
->acceptedFileTypes(['application/json']) ->acceptedFileTypes(['application/json'])
->storeFiles(false), ->storeFiles(false)
->multiple(),
]) ])
->action(function (array $data): void { ->action(function (array $data): void {
/** @var TemporaryUploadedFile $eggFile */ /** @var TemporaryUploadedFile $eggFile */
@ -35,7 +36,7 @@ class ListEggs extends ListRecords
$eggImportService = resolve(EggImporterService::class); $eggImportService = resolve(EggImporterService::class);
try { try {
$newEgg = $eggImportService->handle($eggFile); $eggImportService->handle($eggFile);
} catch (Exception $exception) { } catch (Exception $exception) {
Notification::make() Notification::make()
->title('Egg Import Failed') ->title('Egg Import Failed')
@ -48,11 +49,9 @@ class ListEggs extends ListRecords
} }
Notification::make() Notification::make()
->title("Egg Import Success: $newEgg->name") ->title('Egg Import Success')
->success() ->success()
->send(); ->send();
redirect()->route('filament.admin.resources.eggs.edit', [$newEgg]);
}), }),
]; ];
} }

View File

@ -207,6 +207,7 @@ class ServerResource extends Resource
// Do not add non numerical ports // Do not add non numerical ports
$update = true; $update = true;
continue; continue;
} }
@ -217,7 +218,7 @@ class ServerResource extends Resource
} }
$start = max((int) $start, 0); $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++) { for ($i = $start; $i <= $end; $i++) {
$ports->push($i); $ports->push($i);
} }

View File

@ -307,7 +307,7 @@ class Node extends Model
$ips = collect(); $ips = collect();
if (is_ip($this->fqdn)) { if (is_ip($this->fqdn)) {
$ips->push($this->fqdn); $ips->push($this->fqdn);
} else if ($dnsRecords = gethostbynamel($this->fqdn)) { } elseif ($dnsRecords = gethostbynamel($this->fqdn)) {
$ips->concat($dnsRecords); $ips->concat($dnsRecords);
} }

View File

@ -5,7 +5,6 @@ namespace App\Services\Eggs\Sharing;
use Ramsey\Uuid\Uuid; use Ramsey\Uuid\Uuid;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use App\Models\Egg; use App\Models\Egg;
use Illuminate\Http\UploadedFile;
use App\Models\EggVariable; use App\Models\EggVariable;
use Illuminate\Database\ConnectionInterface; use Illuminate\Database\ConnectionInterface;
use App\Services\Eggs\EggParserService; use App\Services\Eggs\EggParserService;
@ -21,25 +20,31 @@ class EggImporterService
* *
* @throws \App\Exceptions\Service\InvalidFileUploadException|\Throwable * @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) { foreach ($files as $file) {
$egg = (new Egg())->forceFill([ $parsed = $this->parser->handle($file);
'uuid' => Uuid::uuid4()->toString(),
'author' => Arr::get($parsed, 'author'),
'copy_script_from' => null,
]);
$egg = $this->parser->fillFromParsed($egg, $parsed); $egg = $this->connection->transaction(function () use ($parsed) {
$egg->save(); $egg = (new Egg())->forceFill([
'uuid' => Uuid::uuid4()->toString(),
'author' => Arr::get($parsed, 'author'),
'copy_script_from' => null,
]);
foreach ($parsed['variables'] ?? [] as $variable) { $egg = $this->parser->fillFromParsed($egg, $parsed);
EggVariable::query()->forceCreate(array_merge($variable, ['egg_id' => $egg->id])); $egg->save();
}
return $egg; foreach ($parsed['variables'] ?? [] as $variable) {
}); EggVariable::query()->forceCreate(array_merge($variable, ['egg_id' => $egg->id]));
}
return $egg;
});
}
return $eggs;
} }
} }

View File

@ -1,12 +1,10 @@
<x-filament-panels::page> <x-filament-panels::page>
<x-filament::tabs label="Content tabs"> <x-filament::tabs disabled>
<x-filament::tabs.item disabled>Panel's Resources: </x-filament::tabs.item> <x-filament::tabs.item disabled>Overview: </x-filament::tabs.item>
<x-filament::tabs.item <x-filament::tabs.item
icon="tabler-server-2" icon="tabler-server-2"
:active="$activeTab === 'nodes'"
wire:click="$set('activeTab', 'nodes')"
> >
Nodes Nodes
<x-slot name="badge">{{ $nodesCount }}</x-slot> <x-slot name="badge">{{ $nodesCount }}</x-slot>
@ -14,8 +12,6 @@
<x-filament::tabs.item <x-filament::tabs.item
icon="tabler-brand-docker" icon="tabler-brand-docker"
:active="$activeTab === 'servers'"
wire:click="$set('activeTab', 'servers')"
> >
Servers Servers
<x-slot name="badge">{{ $serversCount }}</x-slot> <x-slot name="badge">{{ $serversCount }}</x-slot>
@ -23,8 +19,6 @@
<x-filament::tabs.item <x-filament::tabs.item
icon="tabler-eggs" icon="tabler-eggs"
:active="$activeTab === 'eggs'"
wire:click="$set('activeTab', 'eggs')"
> >
Eggs Eggs
<x-slot name="badge">{{ $eggsCount }}</x-slot> <x-slot name="badge">{{ $eggsCount }}</x-slot>
@ -32,8 +26,6 @@
<x-filament::tabs.item <x-filament::tabs.item
icon="tabler-users" icon="tabler-users"
:active="$activeTab === 'users'"
wire:click="$set('activeTab', 'users')"
> >
Users Users
<x-slot name="badge">{{ $usersCount }}</x-slot> <x-slot name="badge">{{ $usersCount }}</x-slot>