ui(admin): implement basic egg importing

This commit is contained in:
Matthew Penner 2021-09-17 13:48:20 -06:00
parent 107cf72269
commit e8ddadc608
No known key found for this signature in database
GPG key ID: 030E4AB751DC756F
8 changed files with 163 additions and 22 deletions

View file

@ -0,0 +1,9 @@
<?php
namespace Pterodactyl\Exceptions\Service\Egg;
use Pterodactyl\Exceptions\DisplayException;
class BadYamlFormatException extends DisplayException
{
}

View file

@ -8,11 +8,13 @@ use Spatie\QueryBuilder\QueryBuilder;
use Pterodactyl\Services\Nests\NestUpdateService; use Pterodactyl\Services\Nests\NestUpdateService;
use Pterodactyl\Services\Nests\NestCreationService; use Pterodactyl\Services\Nests\NestCreationService;
use Pterodactyl\Services\Nests\NestDeletionService; 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\Transformers\Api\Application\NestTransformer;
use Pterodactyl\Exceptions\Http\QueryValueOutOfRangeHttpException; use Pterodactyl\Exceptions\Http\QueryValueOutOfRangeHttpException;
use Pterodactyl\Http\Requests\Api\Application\Nests\GetNestRequest; use Pterodactyl\Http\Requests\Api\Application\Nests\GetNestRequest;
use Pterodactyl\Http\Requests\Api\Application\Nests\GetNestsRequest; 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\StoreNestRequest;
use Pterodactyl\Http\Requests\Api\Application\Nests\DeleteNestRequest; use Pterodactyl\Http\Requests\Api\Application\Nests\DeleteNestRequest;
use Pterodactyl\Http\Requests\Api\Application\Nests\UpdateNestRequest; use Pterodactyl\Http\Requests\Api\Application\Nests\UpdateNestRequest;
@ -20,27 +22,26 @@ use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
class NestController extends ApplicationApiController class NestController extends ApplicationApiController
{ {
private NestRepositoryInterface $repository; private NestCreationService $nestCreationService;
protected NestCreationService $nestCreationService; private NestDeletionService $nestDeletionService;
protected NestDeletionService $nestDeletionService; private NestUpdateService $nestUpdateService;
protected NestUpdateService $nestUpdateService; private EggImporterService $eggImporterService;
/** /**
* NestController constructor. * NestController constructor.
*/ */
public function __construct( public function __construct(
NestRepositoryInterface $repository,
NestCreationService $nestCreationService, NestCreationService $nestCreationService,
NestDeletionService $nestDeletionService, NestDeletionService $nestDeletionService,
NestUpdateService $nestUpdateService NestUpdateService $nestUpdateService,
EggImporterService $eggImporterService
) { ) {
parent::__construct(); parent::__construct();
$this->repository = $repository;
$this->nestCreationService = $nestCreationService; $this->nestCreationService = $nestCreationService;
$this->nestDeletionService = $nestDeletionService; $this->nestDeletionService = $nestDeletionService;
$this->nestUpdateService = $nestUpdateService; $this->nestUpdateService = $nestUpdateService;
$this->eggImporterService = $eggImporterService;
} }
/** /**
@ -94,10 +95,25 @@ class NestController extends ApplicationApiController
->toArray(); ->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. * Updates an existing nest.
* *
* @throws \Illuminate\Contracts\Container\BindingResolutionException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException * @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/ */

View file

@ -0,0 +1,9 @@
<?php
namespace Pterodactyl\Http\Requests\Api\Application\Eggs;
use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest;
class ImportEggRequest extends ApplicationApiRequest
{
}

View file

@ -5,13 +5,17 @@ namespace Pterodactyl\Services\Eggs\Sharing;
use Ramsey\Uuid\Uuid; use Ramsey\Uuid\Uuid;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Pterodactyl\Models\Egg; use Pterodactyl\Models\Egg;
use Symfony\Component\Yaml\Yaml;
use Illuminate\Http\UploadedFile; use Illuminate\Http\UploadedFile;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Database\ConnectionInterface; use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Exceptions\DisplayException;
use Symfony\Component\Yaml\Exception\ParseException;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface; use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
use Pterodactyl\Contracts\Repository\NestRepositoryInterface; use Pterodactyl\Contracts\Repository\NestRepositoryInterface;
use Pterodactyl\Exceptions\Service\Egg\BadJsonFormatException; use Pterodactyl\Exceptions\Service\Egg\BadJsonFormatException;
use Pterodactyl\Exceptions\Service\InvalidFileUploadException; use Pterodactyl\Exceptions\Service\InvalidFileUploadException;
use Pterodactyl\Exceptions\Service\Egg\BadYamlFormatException;
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface; use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
class EggImporterService class EggImporterService
@ -54,28 +58,80 @@ class EggImporterService
/** /**
* Take an uploaded JSON file and parse it into a new egg. * 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\Repository\RecordNotFoundException
* @throws \Pterodactyl\Exceptions\Service\Egg\BadJsonFormatException * @throws \Pterodactyl\Exceptions\Service\Egg\BadJsonFormatException
* @throws \Pterodactyl\Exceptions\Service\InvalidFileUploadException * @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()) { 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())); 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 */ return $this->handleContent($nestId, $file->openFile()->fread($file->getSize()), 'application/json');
$parsed = json_decode($file->openFile()->fread($file->getSize()), true); }
/**
* @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) { if (json_last_error() !== 0) {
throw new BadJsonFormatException(trans('exceptions.nest.importer.json_error', ['error' => json_last_error_msg()])); 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') { if (Arr::get($parsed, 'meta.version') !== 'PTDL_v1') {
throw new InvalidFileUploadException(trans('exceptions.nest.importer.invalid_json_provided')); throw new InvalidFileUploadException(trans('exceptions.nest.importer.invalid_json_provided'));
} }
$nest = $this->nestRepository->getWithEggs($nest); $nest = $this->nestRepository->getWithEggs($nestId);
$this->connection->beginTransaction(); $this->connection->beginTransaction();
/** @var \Pterodactyl\Models\Egg $egg */ /** @var \Pterodactyl\Models\Egg $egg */

View file

@ -123,7 +123,7 @@ class EggSeeder extends Seeder
$this->command->info('Updated ' . $decoded->name); $this->command->info('Updated ' . $decoded->name);
} catch (RecordNotFoundException $exception) { } catch (RecordNotFoundException $exception) {
$this->importerService->handle($file, $nest->id); $this->importerService->handleFile($nest->id, $file);
$this->command->comment('Created ' . $decoded->name); $this->command->comment('Created ' . $decoded->name);
} }

View file

@ -0,0 +1,17 @@
import http from '@/api/http';
import { Egg, rawDataToEgg } from '@/api/admin/eggs/getEgg';
export default (id: number, content: any, type = 'application/json', include: string[] = []): Promise<Egg> => {
return new Promise((resolve, reject) => {
http.post(`/api/application/nests/${id}/import`, content, {
headers: {
'Content-Type': type,
},
params: {
include: include.join(','),
},
})
.then(({ data }) => resolve(rawDataToEgg(data)))
.catch(reject);
});
};

View file

@ -1,14 +1,37 @@
import getEggs from '@/api/admin/nests/getEggs';
import importEgg from '@/api/admin/nests/importEgg';
import useFlash from '@/plugins/useFlash';
import { jsonLanguage } from '@codemirror/lang-json'; import { jsonLanguage } from '@codemirror/lang-json';
import Editor from '@/components/elements/Editor'; import Editor from '@/components/elements/Editor';
import React, { useState } from 'react'; import React, { useState } from 'react';
import Button from '@/components/elements/Button'; import Button from '@/components/elements/Button';
import Modal from '@/components/elements/Modal'; import Modal from '@/components/elements/Modal';
import FlashMessageRender from '@/components/FlashMessageRender'; import FlashMessageRender from '@/components/FlashMessageRender';
import { useRouteMatch } from 'react-router-dom';
import tw from 'twin.macro'; import tw from 'twin.macro';
export default ({ className }: { className?: string }) => { export default ({ className }: { className?: string }) => {
const [ visible, setVisible ] = useState(false); const [ visible, setVisible ] = useState(false);
const { clearFlashes } = useFlash();
const match = useRouteMatch<{ nestId: string }>();
const { mutate } = getEggs(Number(match.params.nestId));
let fetchFileContent: null | (() => Promise<string>) = null;
const submit = async () => {
clearFlashes('egg:import');
if (fetchFileContent === null) {
return;
}
const egg = await importEgg(Number(match.params.nestId), await fetchFileContent());
await mutate(data => ({ ...data!, items: [ ...data!.items!, egg ] }));
setVisible(false);
};
return ( return (
<> <>
<Modal <Modal
@ -21,18 +44,28 @@ export default ({ className }: { className?: string }) => {
<h2 css={tw`mb-6 text-2xl text-neutral-100`}>Import Egg</h2> <h2 css={tw`mb-6 text-2xl text-neutral-100`}>Import Egg</h2>
<Editor overrides={tw`h-64 rounded`} initialContent={''} mode={jsonLanguage}/> <Editor
overrides={tw`h-64 rounded`}
initialContent={''}
mode={jsonLanguage}
fetchContent={value => {
fetchFileContent = value;
}}
/>
<div css={tw`flex flex-wrap justify-end mt-4 sm:mt-6`}> <div css={tw`flex flex-wrap justify-end mt-4 sm:mt-6`}>
<Button <Button
type={'button'} type={'button'}
isSecondary
css={tw`w-full sm:w-auto sm:mr-2`} css={tw`w-full sm:w-auto sm:mr-2`}
onClick={() => setVisible(false)} onClick={() => setVisible(false)}
isSecondary
> >
Cancel Cancel
</Button> </Button>
<Button css={tw`w-full sm:w-auto mt-4 sm:mt-0`}> <Button
css={tw`w-full sm:w-auto mt-4 sm:mt-0`}
onClick={submit}
>
Import Egg Import Egg
</Button> </Button>
</div> </div>

View file

@ -98,6 +98,7 @@ Route::group(['prefix' => '/nests'], function () {
Route::get('/{nest}/eggs', [\Pterodactyl\Http\Controllers\Api\Application\Eggs\EggController::class, 'index']); Route::get('/{nest}/eggs', [\Pterodactyl\Http\Controllers\Api\Application\Eggs\EggController::class, 'index']);
Route::post('/', [\Pterodactyl\Http\Controllers\Api\Application\Nests\NestController::class, 'store']); Route::post('/', [\Pterodactyl\Http\Controllers\Api\Application\Nests\NestController::class, 'store']);
Route::post('/{nest}/import', [\Pterodactyl\Http\Controllers\Api\Application\Nests\NestController::class, 'import']);
Route::patch('/{nest}', [\Pterodactyl\Http\Controllers\Api\Application\Nests\NestController::class, 'update']); Route::patch('/{nest}', [\Pterodactyl\Http\Controllers\Api\Application\Nests\NestController::class, 'update']);