91 lines
2.7 KiB
PHP
91 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Domain\Catalog\ContentElement;
|
|
use App\Domain\Catalog\IdentityChange;
|
|
use App\Domain\Media\WebsiteRelease;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Queue\Queueable;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class PublishWebsiteMedia implements ShouldQueue
|
|
{
|
|
use Queueable;
|
|
|
|
public function __construct(
|
|
public int $elementId,
|
|
public string $action,
|
|
public ?int $actorId = null,
|
|
) {}
|
|
|
|
public function handle(WebsiteRelease $release): void
|
|
{
|
|
$element = ContentElement::query()->find($this->elementId);
|
|
|
|
if (! $element instanceof ContentElement || ! in_array($this->action, ['release', 'withdraw'], true)) {
|
|
return;
|
|
}
|
|
|
|
if ($this->action === 'withdraw') {
|
|
$this->deletePublicFile($element);
|
|
$this->mark($element, $element->website_released_at?->toIso8601String(), null);
|
|
$element->forceFill([
|
|
'website_released_at' => null,
|
|
'website_public_path' => null,
|
|
])->save();
|
|
|
|
return;
|
|
}
|
|
|
|
$derived = $release->derivedPath($element);
|
|
|
|
if ($derived === null) {
|
|
return;
|
|
}
|
|
|
|
$target = $release->publicPath($element, $derived);
|
|
$previous = $element->website_public_path;
|
|
|
|
Storage::disk('media_public')->put($target, Storage::disk('media_derived')->get($derived));
|
|
|
|
if (is_string($previous) && $previous !== $target && str_starts_with($previous, 'website/'.$element->id.'/')) {
|
|
Storage::disk('media_public')->delete($previous);
|
|
}
|
|
|
|
$this->mark($element, $element->website_released_at?->toIso8601String(), now()->toIso8601String());
|
|
$element->forceFill([
|
|
'website_released_at' => now(),
|
|
'website_public_path' => $target,
|
|
])->save();
|
|
}
|
|
|
|
private function deletePublicFile(ContentElement $element): void
|
|
{
|
|
$path = $element->website_public_path;
|
|
|
|
if (! is_string($path) || ! str_starts_with($path, 'website/'.$element->id.'/') || str_contains($path, '..')) {
|
|
return;
|
|
}
|
|
|
|
Storage::disk('media_public')->delete($path);
|
|
}
|
|
|
|
private function mark(ContentElement $element, ?string $before, ?string $after): void
|
|
{
|
|
if ($before === $after) {
|
|
return;
|
|
}
|
|
|
|
IdentityChange::query()->create([
|
|
'subject_type' => $element->getMorphClass(),
|
|
'subject_id' => $element->id,
|
|
'field' => 'website_released_at',
|
|
'before' => $before,
|
|
'after' => $after,
|
|
'actor_id' => $this->actorId,
|
|
'occurred_at' => now(),
|
|
]);
|
|
}
|
|
}
|