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

132 lines
5.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 Pterodactyl\Models\Egg;
2017-10-04 04:31:04 +00:00
use Illuminate\Http\UploadedFile;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
use Pterodactyl\Contracts\Repository\NestRepositoryInterface;
use Pterodactyl\Exceptions\Service\Egg\BadJsonFormatException;
use Pterodactyl\Exceptions\Service\InvalidFileUploadException;
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
*
2019-09-06 04:32:57 +00:00
* @param \Illuminate\Database\ConnectionInterface $connection
* @param \Pterodactyl\Contracts\Repository\EggRepositoryInterface $repository
* @param \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface $eggVariableRepository
2019-09-06 04:32:57 +00:00
* @param \Pterodactyl\Contracts\Repository\NestRepositoryInterface $nestRepository
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
*
* @param \Illuminate\Http\UploadedFile $file
2019-09-06 04:32:57 +00:00
* @param int $nest
* @return \Pterodactyl\Models\Egg
2017-10-04 04:31:04 +00:00
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Pterodactyl\Exceptions\Service\Egg\BadJsonFormatException
* @throws \Pterodactyl\Exceptions\Service\InvalidFileUploadException
2017-10-04 04:31:04 +00:00
*/
public function handle(UploadedFile $file, int $nest): Egg
2017-10-04 04:31:04 +00:00
{
2017-11-04 04:07:18 +00:00
if ($file->getError() !== UPLOAD_ERR_OK || ! $file->isFile()) {
2020-06-26 04:16:59 +00:00
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
}
$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(),
]));
}
2017-10-04 04:31:04 +00:00
if (object_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
}
2017-10-08 04:29:08 +00:00
$nest = $this->nestRepository->getWithEggs($nest);
2017-10-04 04:31:04 +00:00
$this->connection->beginTransaction();
$egg = $this->repository->create([
2017-10-04 04:31:04 +00:00
'uuid' => Uuid::uuid4()->toString(),
'nest_id' => $nest->id,
2017-10-09 02:36:22 +00:00
'author' => object_get($parsed, 'author'),
2017-10-04 04:31:04 +00:00
'name' => object_get($parsed, 'name'),
'description' => object_get($parsed, 'description'),
'features' => object_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' => object_get($parsed, 'images') ?? [object_get($parsed, 'image')],
'file_denylist' => implode(PHP_EOL, object_get($parsed, 'file_denylist') ?? []),
'update_url' => object_get($parsed, 'meta.update_url'),
2017-10-04 04:31:04 +00:00
'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'),
'copy_script_from' => null,
], true, true);
collect($parsed->variables)->each(function ($variable) use ($egg) {
$this->eggVariableRepository->create(array_merge((array)$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
}
}