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

117 lines
4.2 KiB
PHP
Raw Normal View History

2017-10-04 04:31:04 +00:00
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Services\Services\Sharing;
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;
2017-10-04 04:31:04 +00:00
use Pterodactyl\Exceptions\Service\Pack\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
/**
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface
2017-10-04 04:31:04 +00:00
*/
protected $repository;
2017-10-04 04:31:04 +00:00
/**
* @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface
2017-10-04 04:31:04 +00:00
*/
protected $serviceRepository;
2017-10-04 04:31:04 +00:00
/**
* EggImporterService constructor.
2017-10-04 04:31:04 +00:00
*
* @param \Illuminate\Database\ConnectionInterface $connection
* @param \Pterodactyl\Contracts\Repository\EggRepositoryInterface $repository
* @param \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface $eggVariableRepository
* @param \Pterodactyl\Contracts\Repository\NestRepositoryInterface $serviceRepository
2017-10-04 04:31:04 +00:00
*/
public function __construct(
ConnectionInterface $connection,
EggRepositoryInterface $repository,
EggVariableRepositoryInterface $eggVariableRepository,
NestRepositoryInterface $serviceRepository
2017-10-04 04:31:04 +00:00
) {
$this->connection = $connection;
$this->repository = $repository;
$this->serviceRepository = $serviceRepository;
$this->eggVariableRepository = $eggVariableRepository;
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
* @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\Pack\InvalidFileUploadException
*/
public function handle(UploadedFile $file, int $nest): Egg
2017-10-04 04:31:04 +00:00
{
if (! $file->isValid() || ! $file->isFile()) {
throw new InvalidFileUploadException(trans('exceptions.egg.importer.file_error'));
2017-10-04 04:31:04 +00:00
}
$parsed = json_decode($file->openFile()->fread($file->getSize()));
if (object_get($parsed, 'meta.version') !== 'PTDL_v1') {
throw new InvalidFileUploadException(trans('exceptions.egg.importer.invalid_json_provided'));
2017-10-04 04:31:04 +00:00
}
$nest = $this->serviceRepository->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-04 04:31:04 +00:00
'name' => object_get($parsed, 'name'),
'description' => object_get($parsed, 'description'),
'docker_image' => object_get($parsed, 'image'),
'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
}
}