ui(admin): implement basic egg importing
This commit is contained in:
parent
107cf72269
commit
e8ddadc608
8 changed files with 163 additions and 22 deletions
9
app/Exceptions/Service/Egg/BadYamlFormatException.php
Normal file
9
app/Exceptions/Service/Egg/BadYamlFormatException.php
Normal file
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
namespace Pterodactyl\Exceptions\Service\Egg;
|
||||
|
||||
use Pterodactyl\Exceptions\DisplayException;
|
||||
|
||||
class BadYamlFormatException extends DisplayException
|
||||
{
|
||||
}
|
|
@ -8,11 +8,13 @@ use Spatie\QueryBuilder\QueryBuilder;
|
|||
use Pterodactyl\Services\Nests\NestUpdateService;
|
||||
use Pterodactyl\Services\Nests\NestCreationService;
|
||||
use Pterodactyl\Services\Nests\NestDeletionService;
|
||||
use Pterodactyl\Contracts\Repository\NestRepositoryInterface;
|
||||
use Pterodactyl\Services\Eggs\Sharing\EggImporterService;
|
||||
use Pterodactyl\Transformers\Api\Application\EggTransformer;
|
||||
use Pterodactyl\Transformers\Api\Application\NestTransformer;
|
||||
use Pterodactyl\Exceptions\Http\QueryValueOutOfRangeHttpException;
|
||||
use Pterodactyl\Http\Requests\Api\Application\Nests\GetNestRequest;
|
||||
use Pterodactyl\Http\Requests\Api\Application\Nests\GetNestsRequest;
|
||||
use Pterodactyl\Http\Requests\Api\Application\Eggs\ImportEggRequest;
|
||||
use Pterodactyl\Http\Requests\Api\Application\Nests\StoreNestRequest;
|
||||
use Pterodactyl\Http\Requests\Api\Application\Nests\DeleteNestRequest;
|
||||
use Pterodactyl\Http\Requests\Api\Application\Nests\UpdateNestRequest;
|
||||
|
@ -20,27 +22,26 @@ use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
|
|||
|
||||
class NestController extends ApplicationApiController
|
||||
{
|
||||
private NestRepositoryInterface $repository;
|
||||
protected NestCreationService $nestCreationService;
|
||||
protected NestDeletionService $nestDeletionService;
|
||||
protected NestUpdateService $nestUpdateService;
|
||||
private NestCreationService $nestCreationService;
|
||||
private NestDeletionService $nestDeletionService;
|
||||
private NestUpdateService $nestUpdateService;
|
||||
private EggImporterService $eggImporterService;
|
||||
|
||||
/**
|
||||
* NestController constructor.
|
||||
*/
|
||||
public function __construct(
|
||||
NestRepositoryInterface $repository,
|
||||
NestCreationService $nestCreationService,
|
||||
NestDeletionService $nestDeletionService,
|
||||
NestUpdateService $nestUpdateService
|
||||
NestUpdateService $nestUpdateService,
|
||||
EggImporterService $eggImporterService
|
||||
) {
|
||||
parent::__construct();
|
||||
|
||||
$this->repository = $repository;
|
||||
|
||||
$this->nestCreationService = $nestCreationService;
|
||||
$this->nestDeletionService = $nestDeletionService;
|
||||
$this->nestUpdateService = $nestUpdateService;
|
||||
$this->eggImporterService = $eggImporterService;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -94,10 +95,25 @@ class NestController extends ApplicationApiController
|
|||
->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Imports an egg.
|
||||
*/
|
||||
public function import(ImportEggRequest $request, Nest $nest): array
|
||||
{
|
||||
$egg = $this->eggImporterService->handleContent(
|
||||
$nest->id,
|
||||
$request->getContent(),
|
||||
$request->headers->get('Content-Type'),
|
||||
);
|
||||
|
||||
return $this->fractal->item($egg)
|
||||
->transformWith(EggTransformer::class)
|
||||
->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates an existing nest.
|
||||
*
|
||||
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
||||
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
namespace Pterodactyl\Http\Requests\Api\Application\Eggs;
|
||||
|
||||
use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest;
|
||||
|
||||
class ImportEggRequest extends ApplicationApiRequest
|
||||
{
|
||||
}
|
|
@ -5,13 +5,17 @@ namespace Pterodactyl\Services\Eggs\Sharing;
|
|||
use Ramsey\Uuid\Uuid;
|
||||
use Illuminate\Support\Arr;
|
||||
use Pterodactyl\Models\Egg;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Collection;
|
||||
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;
|
||||
|
||||
class EggImporterService
|
||||
|
@ -54,28 +58,80 @@ class EggImporterService
|
|||
/**
|
||||
* Take an uploaded JSON file and parse it into a new egg.
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||
* @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
|
||||
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||
* @throws \Pterodactyl\Exceptions\DisplayException
|
||||
*/
|
||||
public function handle(UploadedFile $file, int $nest): Egg
|
||||
public function handle(UploadedFile $file, int $nestId): Egg
|
||||
{
|
||||
return $this->handleFile($nestId, $file);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
public function handleFile(int $nestId, 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()));
|
||||
}
|
||||
|
||||
/** @var array $parsed */
|
||||
$parsed = json_decode($file->openFile()->fread($file->getSize()), true);
|
||||
if (json_last_error() !== 0) {
|
||||
throw new BadJsonFormatException(trans('exceptions.nest.importer.json_error', ['error' => json_last_error_msg()]));
|
||||
}
|
||||
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');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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') {
|
||||
throw new InvalidFileUploadException(trans('exceptions.nest.importer.invalid_json_provided'));
|
||||
}
|
||||
|
||||
$nest = $this->nestRepository->getWithEggs($nest);
|
||||
$nest = $this->nestRepository->getWithEggs($nestId);
|
||||
$this->connection->beginTransaction();
|
||||
|
||||
/** @var \Pterodactyl\Models\Egg $egg */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue