archived_at !== null) { return 'Das Programm oder Modul ist archiviert.'; } return null; } public function hasThumbnail(Composition $composition): bool { $path = $composition->thumbnail_path; return is_string($path) && str_starts_with($path, 'vorschau/') && ! str_contains($path, '..') && Storage::disk('media_derived')->exists($path); } public function fromThumbnail(Composition $composition, ?User $actor = null): ContentElement { $this->reused = false; $refusal = $this->refusal($composition); if ($refusal !== null) { throw new DomainRuleException($refusal); } if (! $this->hasThumbnail($composition)) { throw new DomainRuleException('Dieses Programm oder Modul hat noch kein Vorschaubild. Setzen Sie es im Redaktionsmanager.'); } $bytes = Storage::disk('media_derived')->get((string) $composition->thumbnail_path); if (! is_string($bytes) || $bytes === '') { throw new DomainRuleException('Das Vorschaubild ließ sich nicht lesen.'); } $key = 'file:'.hash('sha256', $bytes); $existing = $this->existing($composition, $key); if ($existing instanceof ContentElement) { $this->reused = true; return $existing; } $title = (string) ($composition->display_title ?: $composition->designation); $path = 'cms/titelbild-'.$composition->id.'-'.Str::lower(Str::random(8)).'.jpg'; Storage::disk('cms_incoming')->put($path, $bytes); $still = $this->intake->acceptImage($path, 'Titelbild: '.$title, null, 'Titelbild zu „'.$title.'“', $actor); try { CompositionStillSource::query()->updateOrCreate( ['composition_id' => $composition->id, 'source_key' => $key], ['still_element_id' => $still->id, 'actor_id' => $actor?->id], ); } catch (QueryException) { return $this->existing($composition, $key) ?? $still; } return $still; } private function existing(Composition $composition, string $key): ?ContentElement { $source = CompositionStillSource::query()->with('still')->where('composition_id', $composition->id)->where('source_key', $key)->first(); $still = $source?->still; // Ein archiviertes oder bei der Verarbeitung gescheitertes Bild wird nicht wiederverwendet. return $still instanceof ContentElement && $still->archived_at === null && ! filled($still->thumbnail_error) ? $still : null; } }