85 lines
2.5 KiB
PHP
85 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Domain\Catalog\ContentElementVersion;
|
|
use App\Domain\Catalog\ContentVersionStatus;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Queue\Queueable;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class StoreContentFile implements ShouldQueue
|
|
{
|
|
use Queueable;
|
|
|
|
public function __construct(
|
|
public readonly int $versionId,
|
|
public readonly string $source,
|
|
) {}
|
|
|
|
public function handle(): void
|
|
{
|
|
if (! str_starts_with($this->source, 'inhalte/') || str_contains($this->source, '..')) {
|
|
return;
|
|
}
|
|
|
|
$version = ContentElementVersion::query()->find($this->versionId);
|
|
|
|
if ($version === null || $version->status !== ContentVersionStatus::Entwurf) {
|
|
return;
|
|
}
|
|
|
|
$targetName = $this->storedName(basename($this->source));
|
|
|
|
if ($targetName === null) {
|
|
$this->markFailed($version, 'Der Dateityp wird nicht übernommen.');
|
|
|
|
return;
|
|
}
|
|
|
|
$target = 'inhalte/'.$version->id.'/'.$targetName;
|
|
$incoming = Storage::disk('cms_incoming');
|
|
|
|
if (! $incoming->exists($this->source)) {
|
|
$this->markFailed($version, 'Die hochgeladene Datei wurde nicht gefunden.');
|
|
|
|
return;
|
|
}
|
|
|
|
Storage::disk('media_derived')->put($target, $incoming->get($this->source));
|
|
|
|
$metadata = $version->metadata ?? [];
|
|
unset($metadata['incoming']);
|
|
$metadata['file'] = $target;
|
|
$version->metadata = $metadata;
|
|
$version->content_hash = hash('sha256', json_encode([
|
|
'body' => $version->body,
|
|
'metadata' => $metadata,
|
|
], JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE));
|
|
$version->status = ContentVersionStatus::Bereit;
|
|
$version->save();
|
|
}
|
|
|
|
private function storedName(string $name): ?string
|
|
{
|
|
$extension = strtolower(pathinfo($name, PATHINFO_EXTENSION));
|
|
$stem = preg_replace('/[^A-Za-z0-9_-]+/', '-', pathinfo($name, PATHINFO_FILENAME)) ?? '';
|
|
$stem = trim($stem, '-');
|
|
|
|
if (preg_match('/^[a-z0-9]{1,8}$/', $extension) !== 1) {
|
|
return null;
|
|
}
|
|
|
|
return ($stem !== '' ? $stem : 'datei').'.'.$extension;
|
|
}
|
|
|
|
private function markFailed(ContentElementVersion $version, string $reason): void
|
|
{
|
|
$metadata = $version->metadata ?? [];
|
|
unset($metadata['incoming']);
|
|
$metadata['error'] = $reason;
|
|
$version->metadata = $metadata;
|
|
$version->save();
|
|
}
|
|
}
|