Path: blob/1.0-develop/app/Services/Eggs/Sharing/EggImporterService.php
10322 views
<?php12namespace Pterodactyl\Services\Eggs\Sharing;34use Ramsey\Uuid\Uuid;5use Illuminate\Support\Arr;6use Pterodactyl\Models\Egg;7use Pterodactyl\Models\Nest;8use Illuminate\Http\UploadedFile;9use Pterodactyl\Models\EggVariable;10use Illuminate\Database\ConnectionInterface;11use Pterodactyl\Services\Eggs\EggParserService;1213class EggImporterService14{15public function __construct(protected ConnectionInterface $connection, protected EggParserService $parser)16{17}1819/**20* Take an uploaded JSON file and parse it into a new egg.21*22* @throws \Pterodactyl\Exceptions\Service\InvalidFileUploadException|\Throwable23*/24public function handle(UploadedFile $file, int $nest): Egg25{26$parsed = $this->parser->handle($file);2728/** @var Nest $nest */29$nest = Nest::query()->with('eggs', 'eggs.variables')->findOrFail($nest);3031return $this->connection->transaction(function () use ($nest, $parsed) {32$egg = (new Egg())->forceFill([33'uuid' => Uuid::uuid4()->toString(),34'nest_id' => $nest->id,35'author' => Arr::get($parsed, 'author'),36'copy_script_from' => null,37]);3839$egg = $this->parser->fillFromParsed($egg, $parsed);40$egg->save();4142foreach ($parsed['variables'] ?? [] as $variable) {43EggVariable::query()->forceCreate(array_merge($variable, ['egg_id' => $egg->id]));44}4546return $egg;47});48}49}505152