Path: blob/1.0-develop/app/Services/Eggs/EggParserService.php
10281 views
<?php12namespace Pterodactyl\Services\Eggs;34use Illuminate\Support\Arr;5use Pterodactyl\Models\Egg;6use Illuminate\Http\UploadedFile;7use Illuminate\Support\Collection;8use Pterodactyl\Exceptions\Service\InvalidFileUploadException;910class EggParserService11{12/**13* Takes an uploaded file and parses out the egg configuration from within.14*15* @throws \JsonException16* @throws InvalidFileUploadException17*/18public function handle(UploadedFile $file): array19{20if ($file->getError() !== UPLOAD_ERR_OK || !$file->isFile()) {21throw new InvalidFileUploadException('The selected file is not valid and cannot be imported.');22}2324/** @var array $parsed */25$parsed = json_decode($file->openFile()->fread($file->getSize()), true, 512, JSON_THROW_ON_ERROR);26if (!in_array(Arr::get($parsed, 'meta.version') ?? '', ['PTDL_v1', 'PTDL_v2'])) {27throw new InvalidFileUploadException('The JSON file provided is not in a format that can be recognized.');28}2930return $this->convertToV2($parsed);31}3233/**34* Fills the provided model with the parsed JSON data.35*/36public function fillFromParsed(Egg $model, array $parsed): Egg37{38return $model->forceFill([39'name' => Arr::get($parsed, 'name'),40'description' => Arr::get($parsed, 'description'),41'features' => Arr::get($parsed, 'features'),42'docker_images' => Arr::get($parsed, 'docker_images'),43'file_denylist' => Collection::make(Arr::get($parsed, 'file_denylist'))44->filter(fn ($value) => !empty($value)),45'update_url' => Arr::get($parsed, 'meta.update_url'),46'config_files' => Arr::get($parsed, 'config.files'),47'config_startup' => Arr::get($parsed, 'config.startup'),48'config_logs' => Arr::get($parsed, 'config.logs'),49'config_stop' => Arr::get($parsed, 'config.stop'),50'startup' => Arr::get($parsed, 'startup'),51'script_install' => Arr::get($parsed, 'scripts.installation.script'),52'script_entry' => Arr::get($parsed, 'scripts.installation.entrypoint'),53'script_container' => Arr::get($parsed, 'scripts.installation.container'),54]);55}5657/**58* Converts a PTDL_V1 egg into the expected PTDL_V2 egg format. This just handles59* the "docker_images" field potentially not being present, and not being in the60* expected "key => value" format.61*/62protected function convertToV2(array $parsed): array63{64if (Arr::get($parsed, 'meta.version') === Egg::EXPORT_VERSION) {65return $parsed;66}6768// Maintain backwards compatability for eggs that are still using the old single image69// string format. New eggs can provide an array of Docker images that can be used.70if (!isset($parsed['images'])) {71$images = [Arr::get($parsed, 'image') ?? 'nil'];72} else {73$images = $parsed['images'];74}7576unset($parsed['images'], $parsed['image']);7778$parsed['docker_images'] = [];79foreach ($images as $image) {80$parsed['docker_images'][$image] = $image;81}8283$parsed['variables'] = array_map(function ($value) {84return array_merge($value, ['field_type' => 'text']);85}, $parsed['variables']);8687return $parsed;88}89}909192