- Wird eine schon übernommene Stelle erneut gewählt, nennt die Meldung den tatsächlichen Stand: bereits freigegeben, wird verarbeitet oder Website-Freigabe zurückgenommen (bleibt zurückgenommen). Ein bei der Verarbeitung gescheitertes oder archiviertes Bild wird nicht wiederverwendet; die Herkunft zeigt dann auf das neue Bild. - „Medium löschen“ erreicht jetzt Standbilder (film_still_sources: cascade) und übernommene CMS-Bilder (website_media_aliases: Zuordnung bleibt als Sperre, die frühere Adresse /medien/cms/… liefert 404, das Widget zeigt kein Bild). Vorher hätte der Fremdschlüssel das Löschen mit einem Fehler abgebrochen. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
214 lines
8.2 KiB
PHP
214 lines
8.2 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\Media;
|
|
|
|
use App\Domain\Catalog\ContentElement;
|
|
use App\Domain\Catalog\ContentElementVersion;
|
|
use App\Domain\Catalog\ContentVersionStatus;
|
|
use App\Domain\Catalog\DomainRuleException;
|
|
use App\Domain\Cms\Catalog\PublicMaterialReader;
|
|
use App\Models\User;
|
|
use Illuminate\Database\QueryException;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Str;
|
|
|
|
/**
|
|
* Legt ein Bild aus einem Film als gewöhnliches Bild-Medium im Medienmanager ab (Medien- und Materialkarte).
|
|
*
|
|
* - Zwei Wege: das Vorschaubild des Films übernehmen oder eine Stelle (Sekunde) wählen. Beides ist eine
|
|
* ausdrückliche Handlung der Redaktion; erst dadurch entsteht ein Bild, das für die Website freigegeben werden kann.
|
|
* - Das Bild wird einmal gezogen und gespeichert (WebsiteIntake legt es an, die Warteschlange leitet es ab und gibt
|
|
* es frei). Seitenaufrufe lesen nur die gespeicherte Datei und starten nie ffmpeg.
|
|
* - Dieselbe Stelle (oder dieselbe Vorschaudatei) ergibt dasselbe Medium (film_still_sources).
|
|
* - Es wird nie der Film selbst, nur ein Einzelbild bis 1600 px Breite abgelegt. Traineranleitungen und
|
|
* archivierte oder gesperrte Filme sind ausgeschlossen. Eine Rechte- oder Lizenzentscheidung entsteht nicht.
|
|
*/
|
|
class FilmStill
|
|
{
|
|
/** Breite eines übernommenen Standbilds; die kleine Filmstufe (640 px) wäre auf der Website sichtbar unscharf. */
|
|
public const WIDTH = 1600;
|
|
|
|
/** Wahr, wenn der letzte Aufruf ein schon vorhandenes Bild geliefert hat (keine neue Extraktion). */
|
|
public bool $reused = false;
|
|
|
|
public function __construct(
|
|
private readonly ThumbnailDeriver $deriver,
|
|
private readonly WebsiteIntake $intake,
|
|
) {}
|
|
|
|
/** Grund, warum aus diesem Medium kein Standbild übernommen wird, sonst null. */
|
|
public function refusal(ContentElement $film): ?string
|
|
{
|
|
if ($film->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';
|
|
}
|
|
}
|