> */ 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); } }