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;
|
2021-01-27 05:08:53 +00:00
|
|
|
use Illuminate\Support\Arr;
|
2021-01-28 04:52:11 +00:00
|
|
|
use Pterodactyl\Models\Egg;
|
2022-05-15 18:40:19 +00:00
|
|
|
use Pterodactyl\Models\Nest;
|
2017-10-04 04:31:04 +00:00
|
|
|
use Illuminate\Http\UploadedFile;
|
2022-05-15 18:40:19 +00:00
|
|
|
use Pterodactyl\Models\EggVariable;
|
2017-10-04 04:31:04 +00:00
|
|
|
use Illuminate\Database\ConnectionInterface;
|
2022-05-15 18:40:19 +00:00
|
|
|
use Pterodactyl\Services\Eggs\EggParserService;
|
2017-10-04 04:31:04 +00:00
|
|
|
|
2017-10-07 04:57:53 +00:00
|
|
|
class EggImporterService
|
2017-10-04 04:31:04 +00:00
|
|
|
{
|
2022-10-14 16:59:20 +00:00
|
|
|
public function __construct(protected ConnectionInterface $connection, protected EggParserService $parser)
|
2022-05-15 18:40:19 +00:00
|
|
|
{
|
2017-10-04 04:31:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-07 04:57:53 +00:00
|
|
|
* Take an uploaded JSON file and parse it into a new egg.
|
2017-10-04 04:31:04 +00:00
|
|
|
*
|
2022-05-15 18:40:19 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Service\InvalidFileUploadException|\Throwable
|
2017-10-04 04:31:04 +00:00
|
|
|
*/
|
2017-10-07 04:57:53 +00:00
|
|
|
public function handle(UploadedFile $file, int $nest): Egg
|
2017-10-04 04:31:04 +00:00
|
|
|
{
|
2022-05-15 18:40:19 +00:00
|
|
|
$parsed = $this->parser->handle($file);
|
2017-10-04 04:31:04 +00:00
|
|
|
|
2022-05-15 18:40:19 +00:00
|
|
|
/** @var \Pterodactyl\Models\Nest $nest */
|
|
|
|
$nest = Nest::query()->with('eggs', 'eggs.variables')->findOrFail($nest);
|
2017-10-04 04:31:04 +00:00
|
|
|
|
2022-05-15 18:40:19 +00:00
|
|
|
return $this->connection->transaction(function () use ($nest, $parsed) {
|
|
|
|
$egg = (new Egg())->forceFill([
|
|
|
|
'uuid' => Uuid::uuid4()->toString(),
|
|
|
|
'nest_id' => $nest->id,
|
|
|
|
'author' => Arr::get($parsed, 'author'),
|
|
|
|
'copy_script_from' => null,
|
|
|
|
]);
|
2017-10-04 04:31:04 +00:00
|
|
|
|
2022-05-15 18:40:19 +00:00
|
|
|
$egg = $this->parser->fillFromParsed($egg, $parsed);
|
|
|
|
$egg->save();
|
2017-10-07 04:57:53 +00:00
|
|
|
|
2022-05-15 18:40:19 +00:00
|
|
|
foreach ($parsed['variables'] ?? [] as $variable) {
|
|
|
|
EggVariable::query()->forceCreate(array_merge($variable, ['egg_id' => $egg->id]));
|
|
|
|
}
|
2017-10-04 04:31:04 +00:00
|
|
|
|
2022-05-15 18:40:19 +00:00
|
|
|
return $egg;
|
2017-10-04 04:31:04 +00:00
|
|
|
});
|
2022-05-07 21:45:22 +00:00
|
|
|
}
|
2017-10-04 04:31:04 +00:00
|
|
|
}
|