Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Services/Eggs/Sharing/EggImporterService.php
10322 views
1
<?php
2
3
namespace Pterodactyl\Services\Eggs\Sharing;
4
5
use Ramsey\Uuid\Uuid;
6
use Illuminate\Support\Arr;
7
use Pterodactyl\Models\Egg;
8
use Pterodactyl\Models\Nest;
9
use Illuminate\Http\UploadedFile;
10
use Pterodactyl\Models\EggVariable;
11
use Illuminate\Database\ConnectionInterface;
12
use Pterodactyl\Services\Eggs\EggParserService;
13
14
class EggImporterService
15
{
16
public function __construct(protected ConnectionInterface $connection, protected EggParserService $parser)
17
{
18
}
19
20
/**
21
* Take an uploaded JSON file and parse it into a new egg.
22
*
23
* @throws \Pterodactyl\Exceptions\Service\InvalidFileUploadException|\Throwable
24
*/
25
public function handle(UploadedFile $file, int $nest): Egg
26
{
27
$parsed = $this->parser->handle($file);
28
29
/** @var Nest $nest */
30
$nest = Nest::query()->with('eggs', 'eggs.variables')->findOrFail($nest);
31
32
return $this->connection->transaction(function () use ($nest, $parsed) {
33
$egg = (new Egg())->forceFill([
34
'uuid' => Uuid::uuid4()->toString(),
35
'nest_id' => $nest->id,
36
'author' => Arr::get($parsed, 'author'),
37
'copy_script_from' => null,
38
]);
39
40
$egg = $this->parser->fillFromParsed($egg, $parsed);
41
$egg->save();
42
43
foreach ($parsed['variables'] ?? [] as $variable) {
44
EggVariable::query()->forceCreate(array_merge($variable, ['egg_id' => $egg->id]));
45
}
46
47
return $egg;
48
});
49
}
50
}
51
52