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\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
*/

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 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);
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 */

View file

@ -123,7 +123,7 @@ class EggSeeder extends Seeder
$this->command->info('Updated ' . $decoded->name);
} catch (RecordNotFoundException $exception) {
$this->importerService->handle($file, $nest->id);
$this->importerService->handleFile($nest->id, $file);
$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 Editor from '@/components/elements/Editor';
import React, { useState } from 'react';
import Button from '@/components/elements/Button';
import Modal from '@/components/elements/Modal';
import FlashMessageRender from '@/components/FlashMessageRender';
import { useRouteMatch } from 'react-router-dom';
import tw from 'twin.macro';
export default ({ className }: { className?: string }) => {
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 (
<>
<Modal
@ -21,18 +44,28 @@ export default ({ className }: { className?: string }) => {
<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`}>
<Button
type={'button'}
isSecondary
css={tw`w-full sm:w-auto sm:mr-2`}
onClick={() => setVisible(false)}
isSecondary
>
Cancel
</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
</Button>
</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::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']);