|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pterodactyl\Services\Eggs\Sharing; |
| 4 | + |
| 5 | +use Illuminate\Http\UploadedFile; |
| 6 | +use Illuminate\Database\ConnectionInterface; |
| 7 | +use Pterodactyl\Contracts\Repository\EggRepositoryInterface; |
| 8 | +use Pterodactyl\Exceptions\Service\Egg\BadJsonFormatException; |
| 9 | +use Pterodactyl\Exceptions\Service\InvalidFileUploadException; |
| 10 | +use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface; |
| 11 | + |
| 12 | +class EggUpdateImporterService |
| 13 | +{ |
| 14 | + /** |
| 15 | + * @var \Illuminate\Database\ConnectionInterface |
| 16 | + */ |
| 17 | + protected $connection; |
| 18 | + |
| 19 | + /** |
| 20 | + * @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface |
| 21 | + */ |
| 22 | + protected $repository; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface |
| 26 | + */ |
| 27 | + protected $variableRepository; |
| 28 | + |
| 29 | + /** |
| 30 | + * EggUpdateImporterService constructor. |
| 31 | + * |
| 32 | + * @param \Illuminate\Database\ConnectionInterface $connection |
| 33 | + * @param \Pterodactyl\Contracts\Repository\EggRepositoryInterface $repository |
| 34 | + * @param \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface $variableRepository |
| 35 | + */ |
| 36 | + public function __construct( |
| 37 | + ConnectionInterface $connection, |
| 38 | + EggRepositoryInterface $repository, |
| 39 | + EggVariableRepositoryInterface $variableRepository |
| 40 | + ) { |
| 41 | + $this->connection = $connection; |
| 42 | + $this->repository = $repository; |
| 43 | + $this->variableRepository = $variableRepository; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Update an existing Egg using an uploaded JSON file. |
| 48 | + * |
| 49 | + * @param int $egg |
| 50 | + * @param \Illuminate\Http\UploadedFile $file |
| 51 | + * |
| 52 | + * @throws \Pterodactyl\Exceptions\Model\DataValidationException |
| 53 | + * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException |
| 54 | + * @throws \Pterodactyl\Exceptions\Service\Egg\BadJsonFormatException |
| 55 | + * @throws \Pterodactyl\Exceptions\Service\InvalidFileUploadException |
| 56 | + */ |
| 57 | + public function handle(int $egg, UploadedFile $file) |
| 58 | + { |
| 59 | + if (! $file->isValid() || ! $file->isFile()) { |
| 60 | + throw new InvalidFileUploadException(trans('exceptions.nest.importer.file_error')); |
| 61 | + } |
| 62 | + |
| 63 | + $parsed = json_decode($file->openFile()->fread($file->getSize())); |
| 64 | + if (json_last_error() !== 0) { |
| 65 | + throw new BadJsonFormatException(trans('exceptions.nest.importer.json_error', [ |
| 66 | + 'error' => json_last_error_msg(), |
| 67 | + ])); |
| 68 | + } |
| 69 | + |
| 70 | + if (object_get($parsed, 'meta.version') !== 'PTDL_v1') { |
| 71 | + throw new InvalidFileUploadException(trans('exceptions.nest.importer.invalid_json_provided')); |
| 72 | + } |
| 73 | + |
| 74 | + $this->connection->beginTransaction(); |
| 75 | + $this->repository->update($egg, [ |
| 76 | + 'author' => object_get($parsed, 'author'), |
| 77 | + 'name' => object_get($parsed, 'name'), |
| 78 | + 'description' => object_get($parsed, 'description'), |
| 79 | + 'docker_image' => object_get($parsed, 'image'), |
| 80 | + 'config_files' => object_get($parsed, 'config.files'), |
| 81 | + 'config_startup' => object_get($parsed, 'config.startup'), |
| 82 | + 'config_logs' => object_get($parsed, 'config.logs'), |
| 83 | + 'config_stop' => object_get($parsed, 'config.stop'), |
| 84 | + 'startup' => object_get($parsed, 'startup'), |
| 85 | + 'script_install' => object_get($parsed, 'scripts.installation.script'), |
| 86 | + 'script_entry' => object_get($parsed, 'scripts.installation.entrypoint'), |
| 87 | + 'script_container' => object_get($parsed, 'scripts.installation.container'), |
| 88 | + ], true, true); |
| 89 | + |
| 90 | + // Update Existing Variables |
| 91 | + collect($parsed->variables)->each(function ($variable) use ($egg) { |
| 92 | + $this->variableRepository->withoutFresh()->updateOrCreate([ |
| 93 | + 'egg_id' => $egg, |
| 94 | + 'env_variable' => $variable->env_variable, |
| 95 | + ], collect($variable)->except(['egg_id', 'env_variable'])->toArray()); |
| 96 | + }); |
| 97 | + |
| 98 | + $imported = collect($parsed->variables)->pluck('env_variable')->toArray(); |
| 99 | + $existing = $this->variableRepository->withColumns(['id', 'env_variable'])->findWhere([['egg_id', '=', $egg]]); |
| 100 | + |
| 101 | + // Delete variables not present in the import. |
| 102 | + collect($existing)->each(function ($variable) use ($egg, $imported) { |
| 103 | + if (! in_array($variable->env_variable, $imported)) { |
| 104 | + $this->variableRepository->deleteWhere([ |
| 105 | + ['egg_id', '=', $egg], |
| 106 | + ['env_variable', '=', $variable->env_variable], |
| 107 | + ]); |
| 108 | + } |
| 109 | + }); |
| 110 | + |
| 111 | + $this->connection->commit(); |
| 112 | + } |
| 113 | +} |
0 commit comments