misc_pterodactyl-panel/app/Services/Eggs/Sharing/EggImporterService.php

174 lines
7.2 KiB
PHP
Raw Normal View History

2017-10-04 04:31:04 +00:00
<?php
2017-10-07 21:19:07 +00:00
namespace Pterodactyl\Services\Eggs\Sharing;
2017-10-04 04:31:04 +00:00
use Ramsey\Uuid\Uuid;
use Illuminate\Support\Arr;
use Pterodactyl\Models\Egg;
use Symfony\Component\Yaml\Yaml;
2017-10-04 04:31:04 +00:00
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Collection;
2017-10-04 04:31:04 +00:00
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Exceptions\DisplayException;
use Symfony\Component\Yaml\Exception\ParseException;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
use Pterodactyl\Contracts\Repository\NestRepositoryInterface;
use Pterodactyl\Exceptions\Service\Egg\BadJsonFormatException;
use Pterodactyl\Exceptions\Service\InvalidFileUploadException;
use Pterodactyl\Exceptions\Service\Egg\BadYamlFormatException;
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
2017-10-04 04:31:04 +00:00
class EggImporterService
2017-10-04 04:31:04 +00:00
{
/**
* @var \Illuminate\Database\ConnectionInterface
*/
protected $connection;
/**
* @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface
2017-10-04 04:31:04 +00:00
*/
protected $eggVariableRepository;
2017-10-04 04:31:04 +00:00
/**
2017-10-08 04:29:08 +00:00
* @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface
2017-10-04 04:31:04 +00:00
*/
2017-10-08 04:29:08 +00:00
protected $nestRepository;
2017-10-04 04:31:04 +00:00
/**
2017-10-08 04:29:08 +00:00
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface
2017-10-04 04:31:04 +00:00
*/
2017-10-08 04:29:08 +00:00
protected $repository;
2017-10-04 04:31:04 +00:00
/**
* EggImporterService constructor.
2017-10-04 04:31:04 +00:00
*/
public function __construct(
ConnectionInterface $connection,
EggRepositoryInterface $repository,
EggVariableRepositoryInterface $eggVariableRepository,
2017-10-08 04:29:08 +00:00
NestRepositoryInterface $nestRepository
2017-10-04 04:31:04 +00:00
) {
$this->connection = $connection;
$this->eggVariableRepository = $eggVariableRepository;
2017-10-08 04:29:08 +00:00
$this->repository = $repository;
$this->nestRepository = $nestRepository;
2017-10-04 04:31:04 +00:00
}
/**
* Take an uploaded JSON file and parse it into a new egg.
2017-10-04 04:31:04 +00:00
*
* @deprecated Use `handleFile` or `handleContent` instead.
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Pterodactyl\Exceptions\Service\Egg\BadJsonFormatException
* @throws \Pterodactyl\Exceptions\Service\InvalidFileUploadException
* @throws \Pterodactyl\Exceptions\Service\Egg\BadYamlFormatException
2017-10-04 04:31:04 +00:00
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\DisplayException
*/
public function handle(UploadedFile $file, int $nestId): Egg
{
return $this->handleFile($nestId, $file);
}
/**
2017-10-04 04:31:04 +00:00
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Pterodactyl\Exceptions\Service\Egg\BadJsonFormatException
* @throws \Pterodactyl\Exceptions\Service\InvalidFileUploadException
* @throws \Pterodactyl\Exceptions\Service\Egg\BadYamlFormatException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\DisplayException
2017-10-04 04:31:04 +00:00
*/
public function handleFile(int $nestId, UploadedFile $file): Egg
2017-10-04 04:31:04 +00:00
{
2021-01-23 20:33:34 +00:00
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()));
2017-10-04 04:31:04 +00:00
}
return $this->handleContent($nestId, $file->openFile()->fread($file->getSize()), 'application/json');
}
/**
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Pterodactyl\Exceptions\Service\InvalidFileUploadException
* @throws \Pterodactyl\Exceptions\Service\Egg\BadYamlFormatException
* @throws \Pterodactyl\Exceptions\Service\Egg\BadJsonFormatException
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
*/
public function handleContent(int $nestId, string $content, string $contentType): Egg
{
switch (true) {
case strpos($contentType, 'application/json') === 0:
$parsed = json_decode($content, true);
if (json_last_error() !== 0) {
throw new BadJsonFormatException(trans('exceptions.nest.importer.json_error', ['error' => json_last_error_msg()]));
}
return $this->handleArray($nestId, $parsed);
case strpos($contentType, 'application/yaml') === 0:
try {
$parsed = Yaml::parse($content);
return $this->handleArray($nestId, $parsed);
} catch (ParseException $exception) {
throw new BadYamlFormatException('There was an error while attempting to parse the YAML: ' . $exception->getMessage() . '.');
}
default:
throw new DisplayException('unknown content type');
}
}
2017-10-04 04:31:04 +00:00
/**
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Pterodactyl\Exceptions\Service\InvalidFileUploadException
*/
private function handleArray(int $nestId, array $parsed): Egg
{
if (Arr::get($parsed, 'meta.version') !== 'PTDL_v1') {
2017-10-08 04:29:08 +00:00
throw new InvalidFileUploadException(trans('exceptions.nest.importer.invalid_json_provided'));
2017-10-04 04:31:04 +00:00
}
$nest = $this->nestRepository->getWithEggs($nestId);
2017-10-04 04:31:04 +00:00
$this->connection->beginTransaction();
/** @var \Pterodactyl\Models\Egg $egg */
$egg = $this->repository->create([
2017-10-04 04:31:04 +00:00
'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'),
// 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.
'docker_images' => Arr::get($parsed, 'images') ?? [Arr::get($parsed, 'image')],
'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'),
2017-10-04 04:31:04 +00:00
'copy_script_from' => null,
], true, true);
Collection::make($parsed['variables'] ?? [])->each(function (array $variable) use ($egg) {
$this->eggVariableRepository->create(array_merge($variable, [
'egg_id' => $egg->id,
2017-10-04 04:31:04 +00:00
]));
});
$this->connection->commit();
return $egg;
2017-10-04 04:31:04 +00:00
}
}