Files
laraundfelix/app/Jobs/StoreContentFile.php
LarsandCursor c73207f2c3
Deploy Test / test (push) Successful in 26s
Deploy Test / deploy (push) Successful in 30s
Halte ungenutzte Medien bearbeitbar und lege Dateien direkt im Modul an.
Eine Fassung wird erst unveränderlich, wenn sie freigegeben und verwendet wird. Das Auge am Listeneintrag öffnet die Vorschau.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-17 10:34:42 +01:00

111 lines
3.1 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 int $timeout = 3600;
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;
}
$stream = $incoming->readStream($this->source);
if (! is_resource($stream)) {
$this->markFailed($version, 'Die hochgeladene Datei ließ sich nicht lesen.');
return;
}
try {
Storage::disk('media_derived')->writeStream($target, $stream);
} finally {
fclose($stream);
}
$metadata = $version->metadata ?? [];
$hold = ($metadata['hold'] ?? false) === true;
unset($metadata['incoming'], $metadata['hold']);
$metadata['file'] = $target;
if ($hold) {
$metadata['held'] = true;
}
$version->metadata = $metadata;
$version->content_hash = hash('sha256', json_encode([
'body' => $version->body,
'metadata' => $metadata,
], JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE));
if (! $hold) {
$version->status = ContentVersionStatus::Bereit;
}
$version->save();
DeriveThumbnail::dispatch('element', $version->content_element_id);
DeriveFilmRenditions::dispatch($version->id);
}
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();
}
}