Der Web-Prozess hat derived/ nur lesend; das Anlegen als eigenständiges Präsentationsmedium schrieb dort selbst und scheiterte auf den Servern (die Tests fakten die Disk). Jetzt prüft PresentationFile nur und übergibt das Kopieren an StorePresentationFile; ein Fehler steht am Medium. Die Prüfung läuft vor dem Anlegen, sodass keine leere Hülle zurückbleibt, und nennt das Format (MOV und M4V sind keine Website-Dateien). Am Medium gibt es unter Website-Freigabe die Website-Datei zum nachträglichen Hochladen, auch bei einem Film mit geschützter Fassung (ungeschützter Trailer). Die Fassung bleibt unverändert. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
120 lines
4.2 KiB
PHP
120 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\Media;
|
|
|
|
use App\Domain\Catalog\ContentElement;
|
|
use App\Domain\Catalog\DomainRuleException;
|
|
use App\Jobs\StorePresentationFile;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
/**
|
|
* Legt die technische Ableitung eines Präsentationsmediums (Website-Datei) an der Medienidentität ab.
|
|
* Es entsteht keine Inhaltsfassung, keine Freigabe und keine Berechtigung. Eine vorhandene Inhaltsfassung bleibt
|
|
* unverändert und geschützt; die Website-Datei ist eine eigene, bewusst gewählte Datei daneben.
|
|
*
|
|
* Der Web-Prozess hat `derived/` nur lesend. Er prüft deshalb nur und gibt das Kopieren an die Warteschlange
|
|
* (`StorePresentationFile`, ruft `place()` auf).
|
|
*/
|
|
class PresentationFile
|
|
{
|
|
/**
|
|
* Dateiendungen, die ein Browser auf der Website abspielen kann.
|
|
*
|
|
* @var array<string, list<string>>
|
|
*/
|
|
public const EXTENSIONS = [
|
|
'bild' => ['jpg', 'jpeg', 'png', 'webp', 'gif'],
|
|
'audio' => ['mp3', 'm4a', 'ogg'],
|
|
'film' => ['mp4', 'webm'],
|
|
];
|
|
|
|
/**
|
|
* Prüft die Datei im Eingang, ohne etwas zu verändern. Wirft eine verständliche Regelverletzung.
|
|
*/
|
|
public function validate(string $outputClass, string $incoming): void
|
|
{
|
|
if (! isset(self::EXTENSIONS[$outputClass])) {
|
|
throw new DomainRuleException('Eine Präsentationsdatei ist ein Bild, ein Film oder ein Hörstück.');
|
|
}
|
|
|
|
$extension = strtolower(pathinfo($incoming, PATHINFO_EXTENSION));
|
|
|
|
if (! in_array($extension, self::EXTENSIONS[$outputClass], true)) {
|
|
throw new DomainRuleException(sprintf(
|
|
'Für die Website geht nur %s. Eine %s-Datei kann der Browser nicht direkt abspielen; wandle sie vorher um oder lade sie ohne „Eigenständiges Präsentationsmedium“ hoch.',
|
|
$this->formats($outputClass),
|
|
$extension === '' ? 'Datei ohne Endung' : '.'.$extension,
|
|
));
|
|
}
|
|
|
|
if (! $this->readable($incoming)) {
|
|
throw new DomainRuleException('Die Datei liegt nicht im Eingang.');
|
|
}
|
|
}
|
|
|
|
public function store(ContentElement $element, string $incoming): void
|
|
{
|
|
$this->validate((string) $element->output_class, $incoming);
|
|
|
|
$element->thumbnail_error = null;
|
|
$element->save();
|
|
|
|
StorePresentationFile::dispatch($element->id, $incoming);
|
|
}
|
|
|
|
/**
|
|
* Kopiert die Datei nach `derived/`. Läuft nur in der Warteschlange, die dort schreiben darf.
|
|
*/
|
|
public function place(ContentElement $element, string $incoming): void
|
|
{
|
|
$this->validate((string) $element->output_class, $incoming);
|
|
|
|
$extension = strtolower(pathinfo($incoming, PATHINFO_EXTENSION));
|
|
$prefix = $element->output_class === 'bild' ? 'vorschau' : 'teaser';
|
|
$target = $prefix.'/'.$element->id.'/datei.'.$extension;
|
|
$stream = Storage::disk('cms_incoming')->readStream($incoming);
|
|
|
|
if (! is_resource($stream)) {
|
|
throw new DomainRuleException('Die Datei ließ sich nicht lesen.');
|
|
}
|
|
|
|
try {
|
|
$written = Storage::disk('media_derived')->writeStream($target, $stream);
|
|
} finally {
|
|
fclose($stream);
|
|
}
|
|
|
|
if ($written === false) {
|
|
throw new DomainRuleException('Die Datei ließ sich nicht ablegen.');
|
|
}
|
|
|
|
if ($element->output_class === 'bild') {
|
|
$element->thumbnail_path = $target;
|
|
$element->thumbnail_source = 'eigen';
|
|
} else {
|
|
$element->preview_path = $target;
|
|
}
|
|
|
|
$element->thumbnail_error = null;
|
|
$element->save();
|
|
Storage::disk('cms_incoming')->delete($incoming);
|
|
}
|
|
|
|
public function formats(string $outputClass): string
|
|
{
|
|
return match ($outputClass) {
|
|
'bild' => 'JPG, PNG, WebP oder GIF',
|
|
'audio' => 'MP3, M4A oder OGG',
|
|
default => 'MP4 (H.264) oder WebM',
|
|
};
|
|
}
|
|
|
|
private function readable(string $path): bool
|
|
{
|
|
return (str_starts_with($path, 'inhalte/') || str_starts_with($path, 'cms/') || str_starts_with($path, 'vorschau/'))
|
|
&& ! str_contains($path, '..')
|
|
&& preg_match('#^(inhalte|cms|vorschau)/[A-Za-z0-9._\-/]+$#', $path) === 1
|
|
&& Storage::disk('cms_incoming')->exists($path);
|
|
}
|
|
}
|