pelican-panel-mirror/app/Services/Eggs/Sharing/EggImporterService.php
Boy132 cc1ac1eba1
Allow importing eggs via url (#344)
* allow importing eggs via url

* refactor

* run pint

* turn back into one button

* fix empty check

* small cleanup

* removed container for tabs

* Update URL function

* Use sys temp

---------

Co-authored-by: notCharles <charles@pelican.dev>
2024-06-07 17:31:34 -04:00

66 lines
1.9 KiB
PHP

<?php
namespace App\Services\Eggs\Sharing;
use Ramsey\Uuid\Uuid;
use Illuminate\Support\Arr;
use App\Models\Egg;
use Illuminate\Http\UploadedFile;
use App\Models\EggVariable;
use Illuminate\Database\ConnectionInterface;
use App\Services\Eggs\EggParserService;
use Spatie\TemporaryDirectory\TemporaryDirectory;
class EggImporterService
{
public function __construct(protected ConnectionInterface $connection, protected EggParserService $parser)
{
}
/**
* Take an uploaded JSON file and parse it into a new egg.
*
* @throws \App\Exceptions\Service\InvalidFileUploadException|\Throwable
*/
public function fromFile(UploadedFile $file): Egg
{
$parsed = $this->parser->handle($file);
return $this->connection->transaction(function () use ($parsed) {
$uuid = $parsed['uuid'] ?? Uuid::uuid4()->toString();
$egg = Egg::where('uuid', $uuid)->first() ?? new Egg();
$egg = $egg->forceFill([
'uuid' => $uuid,
'author' => Arr::get($parsed, 'author'),
'copy_script_from' => null,
]);
$egg = $this->parser->fillFromParsed($egg, $parsed);
$egg->save();
foreach ($parsed['variables'] ?? [] as $variable) {
EggVariable::query()->forceCreate(array_merge($variable, ['egg_id' => $egg->id]));
}
return $egg;
});
}
/**
* Take an url and parse it into a new egg.
*
* @throws \App\Exceptions\Service\InvalidFileUploadException|\Throwable
*/
public function fromUrl(string $url): Egg
{
$info = pathinfo($url);
$tmpDir = TemporaryDirectory::make()->deleteWhenDestroyed();
$tmpPath = $tmpDir->path($info['basename']);
file_put_contents($tmpPath, file_get_contents($url));
return $this->fromFile(new UploadedFile($tmpPath, $info['basename'], 'application/json'));
}
}