output_class !== 'film') { return 'Nur aus einem Film lässt sich ein Bild übernehmen.'; } if ($film->archived_at !== null) { return 'Der Film ist archiviert.'; } $slug = $film->editorialContentKind?->slug; if (in_array($slug, PublicMaterialReader::CONFIDENTIAL_KINDS, true)) { return 'Aus Traineranleitungen werden keine Bilder für die Website übernommen.'; } return null; } /** Die bereitgestellte Fassung, aus der Stellen gezogen werden (die höchste). */ public function version(ContentElement $film): ?ContentElementVersion { return $film->versions() ->where('status', ContentVersionStatus::Bereit->value) ->orderByDesc('number') ->first(); } /** Sekunde der Stelle, aus der das aktuelle Vorschaubild des Films stammt, sonst null. */ public function thumbnailSecond(ContentElement $film): ?string { $source = $film->thumbnail_source; return is_string($source) && preg_match('/^frame:\d+@([\d.]+)$/', $source, $match) === 1 ? $match[1] : null; } public function fromSecond(ContentElement $film, float $seconds, ?User $actor = null): ContentElement { $this->reused = false; $this->guard($film); if ($seconds < 0 || $seconds > 7200) { throw new DomainRuleException('Die Zeitposition liegt außerhalb des Films.'); } $version = $this->version($film); if (! $version instanceof ContentElementVersion) { throw new DomainRuleException('Der Film hat noch keine bereitgestellte Fassung.'); } return $this->fromFrame($film, $version, $seconds, $actor); } public function fromThumbnail(ContentElement $film, ?User $actor = null): ContentElement { $this->reused = false; $this->guard($film); // Stammt das Vorschaubild aus einer Filmstelle, wird dieselbe Stelle in voller Auflösung gezogen. $second = $this->thumbnailSecond($film); if ($second !== null && preg_match('/^frame:(\d+)@/', (string) $film->thumbnail_source, $match) === 1) { $version = ContentElementVersion::query()->find((int) $match[1]); if ($version instanceof ContentElementVersion && (int) $version->content_element_id === (int) $film->id && $version->status !== ContentVersionStatus::Gesperrt) { try { return $this->fromFrame($film, $version, (float) $second, $actor); } catch (DomainRuleException) { // Die Stelle lässt sich nicht mehr ziehen: das gespeicherte Vorschaubild selbst übernehmen. } } } $path = $film->thumbnail_path; if (! is_string($path) || ! str_starts_with($path, 'vorschau/') || str_contains($path, '..') || ! Storage::disk('media_derived')->exists($path)) { throw new DomainRuleException('Dieser Film hat noch kein Vorschaubild. Setzen Sie es im Medienmanager oder wählen Sie eine Stelle.'); } $bytes = Storage::disk('media_derived')->get($path); if (! is_string($bytes) || $bytes === '') { throw new DomainRuleException('Das Vorschaubild ließ sich nicht lesen.'); } $key = 'file:'.hash('sha256', $bytes); $existing = $this->existing($film, $key); if ($existing instanceof ContentElement) { $this->reused = true; return $existing; } return $this->store($film, $key, $bytes, 'Vorschaubild', $actor); } private function fromFrame(ContentElement $film, ContentElementVersion $version, float $seconds, ?User $actor): ContentElement { $second = $this->secondText($seconds); $key = 'frame:'.$version->id.'@'.$second; $existing = $this->existing($film, $key); if ($existing instanceof ContentElement) { $this->reused = true; return $existing; } $bytes = $this->deriver->frameJpeg($version, (float) $second, self::WIDTH, original: true); if (! is_string($bytes)) { throw new DomainRuleException('Aus dieser Filmstelle ließ sich kein Bild erzeugen.'); } return $this->store($film, $key, $bytes, 'Fassung '.$version->number.', '.str_replace('.', ',', $second).' s', $actor); } private function guard(ContentElement $film): void { $refusal = $this->refusal($film); if ($refusal !== null) { throw new DomainRuleException($refusal); } } private function existing(ContentElement $film, string $key): ?ContentElement { $source = FilmStillSource::query()->with('still')->where('film_element_id', $film->id)->where('source_key', $key)->first(); $still = $source?->still; // Ein archiviertes oder an der Verarbeitung gescheitertes Bild wird nicht wiederverwendet; die Stelle kann neu // übernommen werden (store() ordnet dann das neue Bild zu). return $still instanceof ContentElement && $still->archived_at === null && ! filled($still->thumbnail_error) ? $still : null; } private function store(ContentElement $film, string $key, string $bytes, string $label, ?User $actor): ContentElement { $title = (string) ($film->display_title ?: $film->working_title); $path = 'cms/standbild-'.$film->id.'-'.Str::lower(Str::random(8)).'.jpg'; Storage::disk('cms_incoming')->put($path, $bytes); $still = $this->intake->acceptImage( $path, 'Standbild: '.$title.' ('.$label.')', filled($film->website_credit) ? (string) $film->website_credit : null, 'Szene aus dem Film „'.$title.'“', $actor, ); try { FilmStillSource::query()->updateOrCreate( ['film_element_id' => $film->id, 'source_key' => $key], ['still_element_id' => $still->id, 'actor_id' => $actor?->id], ); } catch (QueryException) { // Gleichzeitige Übernahme derselben Stelle: das zuerst angelegte Bild gilt. return $this->existing($film, $key) ?? $still; } return $still; } private function secondText(float $seconds): string { $text = number_format(max(0, min(7200, $seconds)), 3, '.', ''); return rtrim(rtrim($text, '0'), '.') ?: '0'; } }