output_class, ['bild', 'film', 'audio'], true)) { throw new DomainRuleException('Eine Präsentationsdatei ist ein Bild, ein Film oder ein Hörstück.'); } if ($element->versions()->exists()) { throw new DomainRuleException('Dieses Medium hat bereits eine Inhaltsfassung. Die Präsentationsdatei ersetzt sie nicht.'); } if (! $this->readable($incoming)) { throw new DomainRuleException('Die Datei liegt nicht im Eingang.'); } $extension = strtolower(pathinfo($incoming, PATHINFO_EXTENSION)); $allowed = match ($element->output_class) { 'bild' => ['jpg', 'jpeg', 'png', 'webp', 'gif'], 'audio' => ['mp3', 'm4a', 'ogg'], default => ['mp4', 'webm'], }; if (! in_array($extension, $allowed, true)) { throw new DomainRuleException('Diese Datei ist keine freigebbare Ableitung. Es wird keine Inhaltsfassung angelegt.'); } $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 { Storage::disk('media_derived')->writeStream($target, $stream); } finally { fclose($stream); } if ($element->output_class === 'bild') { $element->thumbnail_path = $target; $element->thumbnail_source = 'eigen'; } else { $element->preview_path = $target; } $element->save(); } 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); } }