Update egg import/update logic to all use the same pathwaus
This commit is contained in:
parent
6554164252
commit
cca0010a00
12 changed files with 271 additions and 334 deletions
89
app/Services/Eggs/EggParserService.php
Normal file
89
app/Services/Eggs/EggParserService.php
Normal file
|
@ -0,0 +1,89 @@
|
|||
<?php
|
||||
|
||||
namespace Pterodactyl\Services\Eggs;
|
||||
|
||||
use Illuminate\Support\Arr;
|
||||
use Pterodactyl\Models\Egg;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Collection;
|
||||
use Pterodactyl\Exceptions\Service\InvalidFileUploadException;
|
||||
|
||||
class EggParserService
|
||||
{
|
||||
/**
|
||||
* Takes an uploaded file and parses out the egg configuration from within.
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\Service\InvalidFileUploadException
|
||||
*/
|
||||
public function handle(UploadedFile $file): array
|
||||
{
|
||||
if ($file->getError() !== UPLOAD_ERR_OK || !$file->isFile()) {
|
||||
throw new InvalidFileUploadException('The selected file is not valid and cannot be imported.');
|
||||
}
|
||||
|
||||
/** @var array $parsed */
|
||||
$parsed = json_decode($file->openFile()->fread($file->getSize()), true, 512, JSON_THROW_ON_ERROR);
|
||||
if (!in_array(Arr::get($parsed, 'meta.version') ?? '', ['PTDL_v1', 'PTDL_v2'])) {
|
||||
throw new InvalidFileUploadException('The JSON file provided is not in a format that can be recognized.');
|
||||
}
|
||||
|
||||
return $this->convertToV2($parsed);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fills the provided model with the parsed JSON data.
|
||||
*/
|
||||
public function fillFromParsed(Egg $model, array $parsed): Egg
|
||||
{
|
||||
return $model->forceFill([
|
||||
'name' => Arr::get($parsed, 'name'),
|
||||
'description' => Arr::get($parsed, 'description'),
|
||||
'features' => Arr::get($parsed, 'features'),
|
||||
'docker_images' => Arr::get($parsed, 'docker_images'),
|
||||
'file_denylist' => Collection::make(Arr::get($parsed, 'file_denylist'))
|
||||
->filter(fn ($value) => !empty($value)),
|
||||
'update_url' => Arr::get($parsed, 'meta.update_url'),
|
||||
'config_files' => Arr::get($parsed, 'config.files'),
|
||||
'config_startup' => Arr::get($parsed, 'config.startup'),
|
||||
'config_logs' => Arr::get($parsed, 'config.logs'),
|
||||
'config_stop' => Arr::get($parsed, 'config.stop'),
|
||||
'startup' => Arr::get($parsed, 'startup'),
|
||||
'script_install' => Arr::get($parsed, 'scripts.installation.script'),
|
||||
'script_entry' => Arr::get($parsed, 'scripts.installation.entrypoint'),
|
||||
'script_container' => Arr::get($parsed, 'scripts.installation.container'),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a PTDL_V1 egg into the expected PTDL_V2 egg format. This just handles
|
||||
* the "docker_images" field potentially not being present, and not being in the
|
||||
* expected "key => value" format.
|
||||
*/
|
||||
protected function convertToV2(array $parsed): array
|
||||
{
|
||||
if (Arr::get($parsed, 'meta.version') === Egg::EXPORT_VERSION) {
|
||||
return $parsed;
|
||||
}
|
||||
|
||||
// Maintain backwards compatability for eggs that are still using the old single image
|
||||
// string format. New eggs can provide an array of Docker images that can be used.
|
||||
if (!isset($parsed['images'])) {
|
||||
$images = [Arr::get($parsed, 'image') ?? 'nil'];
|
||||
} else {
|
||||
$images = $parsed['images'];
|
||||
}
|
||||
|
||||
unset($parsed['images'], $parsed['image']);
|
||||
|
||||
$parsed['docker_images'] = [];
|
||||
foreach ($images as $image) {
|
||||
$parsed['docker_images'][$image] = $image;
|
||||
}
|
||||
|
||||
$parsed['variables'] = array_map(function ($value) {
|
||||
return array_merge($value, ['field_type' => 'text']);
|
||||
}, $parsed['variables']);
|
||||
|
||||
return $parsed;
|
||||
}
|
||||
}
|
|
@ -5,141 +5,52 @@ namespace Pterodactyl\Services\Eggs\Sharing;
|
|||
use Ramsey\Uuid\Uuid;
|
||||
use Illuminate\Support\Arr;
|
||||
use Pterodactyl\Models\Egg;
|
||||
use Pterodactyl\Models\Nest;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Collection;
|
||||
use Pterodactyl\Models\EggVariable;
|
||||
use Illuminate\Database\ConnectionInterface;
|
||||
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
|
||||
use Pterodactyl\Contracts\Repository\NestRepositoryInterface;
|
||||
use Pterodactyl\Exceptions\Service\InvalidFileUploadException;
|
||||
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
|
||||
use Pterodactyl\Services\Eggs\EggParserService;
|
||||
|
||||
class EggImporterService
|
||||
{
|
||||
/**
|
||||
* @var \Illuminate\Database\ConnectionInterface
|
||||
*/
|
||||
protected $connection;
|
||||
protected ConnectionInterface $connection;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface
|
||||
*/
|
||||
protected $eggVariableRepository;
|
||||
protected EggParserService $parser;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface
|
||||
*/
|
||||
protected $nestRepository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* EggImporterService constructor.
|
||||
*/
|
||||
public function __construct(
|
||||
ConnectionInterface $connection,
|
||||
EggRepositoryInterface $repository,
|
||||
EggVariableRepositoryInterface $eggVariableRepository,
|
||||
NestRepositoryInterface $nestRepository
|
||||
) {
|
||||
public function __construct(ConnectionInterface $connection, EggParserService $parser)
|
||||
{
|
||||
$this->connection = $connection;
|
||||
$this->eggVariableRepository = $eggVariableRepository;
|
||||
$this->repository = $repository;
|
||||
$this->nestRepository = $nestRepository;
|
||||
$this->parser = $parser;
|
||||
}
|
||||
|
||||
/**
|
||||
* Take an uploaded JSON file and parse it into a new egg.
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||
* @throws \Pterodactyl\Exceptions\Service\InvalidFileUploadException
|
||||
* @throws \JsonException
|
||||
* @throws \Pterodactyl\Exceptions\Service\InvalidFileUploadException|\Throwable
|
||||
*/
|
||||
public function handle(UploadedFile $file, int $nest): Egg
|
||||
{
|
||||
if ($file->getError() !== UPLOAD_ERR_OK || !$file->isFile()) {
|
||||
throw new InvalidFileUploadException(sprintf('The selected file ["%s"] was not in a valid format to import. (is_file: %s is_valid: %s err_code: %s err: %s)', $file->getFilename(), $file->isFile() ? 'true' : 'false', $file->isValid() ? 'true' : 'false', $file->getError(), $file->getErrorMessage()));
|
||||
}
|
||||
$parsed = $this->parser->handle($file);
|
||||
|
||||
/** @var array $parsed */
|
||||
$parsed = json_decode($file->openFile()->fread($file->getSize()), true, 512, JSON_THROW_ON_ERROR);
|
||||
if (!in_array(Arr::get($parsed, 'meta.version') ?? '', ['PTDL_v1', 'PTDL_v2'])) {
|
||||
throw new InvalidFileUploadException(trans('exceptions.nest.importer.invalid_json_provided'));
|
||||
}
|
||||
/** @var \Pterodactyl\Models\Nest $nest */
|
||||
$nest = Nest::query()->with('eggs', 'eggs.variables')->findOrFail($nest);
|
||||
|
||||
if ($parsed['meta']['version'] !== Egg::EXPORT_VERSION) {
|
||||
$parsed = $this->convertV1ToV2($parsed);
|
||||
}
|
||||
return $this->connection->transaction(function () use ($nest, $parsed) {
|
||||
$egg = (new Egg())->forceFill([
|
||||
'uuid' => Uuid::uuid4()->toString(),
|
||||
'nest_id' => $nest->id,
|
||||
'author' => Arr::get($parsed, 'author'),
|
||||
'copy_script_from' => null,
|
||||
]);
|
||||
|
||||
$nest = $this->nestRepository->getWithEggs($nest);
|
||||
$this->connection->beginTransaction();
|
||||
$egg = $this->parser->fillFromParsed($egg, $parsed);
|
||||
$egg->save();
|
||||
|
||||
/** @var \Pterodactyl\Models\Egg $egg */
|
||||
$egg = $this->repository->create([
|
||||
'uuid' => Uuid::uuid4()->toString(),
|
||||
'nest_id' => $nest->id,
|
||||
'author' => Arr::get($parsed, 'author'),
|
||||
'name' => Arr::get($parsed, 'name'),
|
||||
'description' => Arr::get($parsed, 'description'),
|
||||
'features' => Arr::get($parsed, 'features'),
|
||||
'docker_images' => Arr::get($parsed, 'docker_images'),
|
||||
'file_denylist' => Collection::make(Arr::get($parsed, 'file_denylist'))->filter(function ($value) {
|
||||
return !empty($value);
|
||||
}),
|
||||
'update_url' => Arr::get($parsed, 'meta.update_url'),
|
||||
'config_files' => Arr::get($parsed, 'config.files'),
|
||||
'config_startup' => Arr::get($parsed, 'config.startup'),
|
||||
'config_logs' => Arr::get($parsed, 'config.logs'),
|
||||
'config_stop' => Arr::get($parsed, 'config.stop'),
|
||||
'startup' => Arr::get($parsed, 'startup'),
|
||||
'script_install' => Arr::get($parsed, 'scripts.installation.script'),
|
||||
'script_entry' => Arr::get($parsed, 'scripts.installation.entrypoint'),
|
||||
'script_container' => Arr::get($parsed, 'scripts.installation.container'),
|
||||
'copy_script_from' => null,
|
||||
], true, true);
|
||||
foreach ($parsed['variables'] ?? [] as $variable) {
|
||||
EggVariable::query()->forceCreate(array_merge($variable, ['egg_id' => $egg->id]));
|
||||
}
|
||||
|
||||
Collection::make($parsed['variables'] ?? [])->each(function (array $variable) use ($egg) {
|
||||
unset($variable['field_type']);
|
||||
|
||||
$this->eggVariableRepository->create(array_merge($variable, [
|
||||
'egg_id' => $egg->id,
|
||||
]));
|
||||
return $egg;
|
||||
});
|
||||
|
||||
$this->connection->commit();
|
||||
|
||||
return $egg;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a PTDL_V1 egg into the expected PTDL_V2 egg format. This just handles
|
||||
* the "docker_images" field potentially not being present, and not being in the
|
||||
* expected "key => value" format.
|
||||
*/
|
||||
protected function convertV1ToV2(array $parsed): array
|
||||
{
|
||||
// Maintain backwards compatability for eggs that are still using the old single image
|
||||
// string format. New eggs can provide an array of Docker images that can be used.
|
||||
if (!isset($parsed['images'])) {
|
||||
$images = [Arr::get($parsed, 'image') ?? 'nil'];
|
||||
} else {
|
||||
$images = $parsed['images'];
|
||||
}
|
||||
|
||||
unset($parsed['images'], $parsed['image']);
|
||||
|
||||
$parsed['docker_images'] = [];
|
||||
foreach ($images as $image) {
|
||||
$parsed['docker_images'][$image] = $image;
|
||||
}
|
||||
|
||||
$parsed['variables'] = array_map(function ($value) {
|
||||
return array_merge($value, ['field_type' => 'text']);
|
||||
}, $parsed['variables']);
|
||||
|
||||
return $parsed;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,105 +4,53 @@ namespace Pterodactyl\Services\Eggs\Sharing;
|
|||
|
||||
use Pterodactyl\Models\Egg;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Collection;
|
||||
use Pterodactyl\Models\EggVariable;
|
||||
use Illuminate\Database\ConnectionInterface;
|
||||
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
|
||||
use Pterodactyl\Exceptions\Service\Egg\BadJsonFormatException;
|
||||
use Pterodactyl\Exceptions\Service\InvalidFileUploadException;
|
||||
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
|
||||
use Pterodactyl\Services\Eggs\EggParserService;
|
||||
|
||||
class EggUpdateImporterService
|
||||
{
|
||||
/**
|
||||
* @var \Illuminate\Database\ConnectionInterface
|
||||
*/
|
||||
protected $connection;
|
||||
protected ConnectionInterface $connection;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface
|
||||
*/
|
||||
protected $variableRepository;
|
||||
protected EggParserService $parser;
|
||||
|
||||
/**
|
||||
* EggUpdateImporterService constructor.
|
||||
*/
|
||||
public function __construct(
|
||||
ConnectionInterface $connection,
|
||||
EggRepositoryInterface $repository,
|
||||
EggVariableRepositoryInterface $variableRepository
|
||||
) {
|
||||
public function __construct(ConnectionInterface $connection, EggParserService $parser)
|
||||
{
|
||||
$this->connection = $connection;
|
||||
$this->repository = $repository;
|
||||
$this->variableRepository = $variableRepository;
|
||||
$this->parser = $parser;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an existing Egg using an uploaded JSON file.
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||
* @throws \Pterodactyl\Exceptions\Service\Egg\BadJsonFormatException
|
||||
* @throws \Pterodactyl\Exceptions\Service\InvalidFileUploadException
|
||||
* @throws \Pterodactyl\Exceptions\Service\InvalidFileUploadException|\Throwable
|
||||
*/
|
||||
public function handle(Egg $egg, UploadedFile $file)
|
||||
public function handle(Egg $egg, UploadedFile $file): Egg
|
||||
{
|
||||
if ($file->getError() !== UPLOAD_ERR_OK || !$file->isFile()) {
|
||||
throw new InvalidFileUploadException(sprintf('The selected file ["%s"] was not in a valid format to import. (is_file: %s is_valid: %s err_code: %s err: %s)', $file->getFilename(), $file->isFile() ? 'true' : 'false', $file->isValid() ? 'true' : 'false', $file->getError(), $file->getErrorMessage()));
|
||||
}
|
||||
$parsed = $this->parser->handle($file);
|
||||
|
||||
$parsed = json_decode($file->openFile()->fread($file->getSize()));
|
||||
if (json_last_error() !== 0) {
|
||||
throw new BadJsonFormatException(trans('exceptions.nest.importer.json_error', ['error' => json_last_error_msg()]));
|
||||
}
|
||||
return $this->connection->transaction(function () use ($egg, $parsed) {
|
||||
$egg = $this->parser->fillFromParsed($egg, $parsed);
|
||||
$egg->save();
|
||||
|
||||
if (object_get($parsed, 'meta.version') !== 'PTDL_v1') {
|
||||
throw new InvalidFileUploadException(trans('exceptions.nest.importer.invalid_json_provided'));
|
||||
}
|
||||
|
||||
$this->connection->beginTransaction();
|
||||
$this->repository->update($egg->id, [
|
||||
'author' => object_get($parsed, 'author'),
|
||||
'name' => object_get($parsed, 'name'),
|
||||
'description' => object_get($parsed, 'description'),
|
||||
'features' => object_get($parsed, 'features'),
|
||||
// Maintain backwards compatibility for eggs that are still using the old single image
|
||||
// string format. New eggs can provide an array of Docker images that can be used.
|
||||
'docker_images' => object_get($parsed, 'images') ?? [object_get($parsed, 'image')],
|
||||
'config_files' => object_get($parsed, 'config.files'),
|
||||
'config_startup' => object_get($parsed, 'config.startup'),
|
||||
'config_logs' => object_get($parsed, 'config.logs'),
|
||||
'config_stop' => object_get($parsed, 'config.stop'),
|
||||
'startup' => object_get($parsed, 'startup'),
|
||||
'script_install' => object_get($parsed, 'scripts.installation.script'),
|
||||
'script_entry' => object_get($parsed, 'scripts.installation.entrypoint'),
|
||||
'script_container' => object_get($parsed, 'scripts.installation.container'),
|
||||
], true, true);
|
||||
|
||||
// Update Existing Variables
|
||||
collect($parsed->variables)->each(function ($variable) use ($egg) {
|
||||
$this->variableRepository->withoutFreshModel()->updateOrCreate([
|
||||
'egg_id' => $egg->id,
|
||||
'env_variable' => $variable->env_variable,
|
||||
], collect($variable)->except(['egg_id', 'env_variable'])->toArray());
|
||||
});
|
||||
|
||||
$imported = collect($parsed->variables)->pluck('env_variable')->toArray();
|
||||
$existing = $this->variableRepository->setColumns(['id', 'env_variable'])->findWhere([['egg_id', '=', $egg->id]]);
|
||||
|
||||
// Delete variables not present in the import.
|
||||
collect($existing)->each(function ($variable) use ($egg, $imported) {
|
||||
if (!in_array($variable->env_variable, $imported)) {
|
||||
$this->variableRepository->deleteWhere([
|
||||
['egg_id', '=', $egg->id],
|
||||
['env_variable', '=', $variable->env_variable],
|
||||
]);
|
||||
// Update existing variables or create new ones.
|
||||
foreach ($parsed['variables'] ?? [] as $variable) {
|
||||
EggVariable::unguarded(function () use ($egg, $variable) {
|
||||
$egg->variables()->updateOrCreate([
|
||||
'env_variable' => $variable['env_variable'],
|
||||
], Collection::make($variable)->except('egg_id', 'env_variable')->toArray());
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
$this->connection->commit();
|
||||
$imported = array_map(fn ($value) => $value['env_variable'], $parsed['variables'] ?? []);
|
||||
|
||||
$egg->variables()->whereNotIn('env_variable', $imported)->delete();
|
||||
|
||||
return $egg->refresh();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue