diff --git a/app/Domain/Catalog/ContentElement.php b/app/Domain/Catalog/ContentElement.php
index f78a422..178dee3 100644
--- a/app/Domain/Catalog/ContentElement.php
+++ b/app/Domain/Catalog/ContentElement.php
@@ -20,6 +20,7 @@ class ContentElement extends Model
'summary',
'thumbnail_path',
'thumbnail_alt',
+ 'thumbnail_source',
'thumbnail_error',
'editorial_content_kind_id',
'archived_at',
diff --git a/app/Domain/Media/ThumbnailDeriver.php b/app/Domain/Media/ThumbnailDeriver.php
index 603f32a..fdd7b1b 100644
--- a/app/Domain/Media/ThumbnailDeriver.php
+++ b/app/Domain/Media/ThumbnailDeriver.php
@@ -24,12 +24,48 @@ public function forElement(ContentElement $element): void
}
if (in_array($element->output_class, ['film', 'audio'], true)) {
- $this->frame($element, $file);
+ $this->frame($element, $version, $file);
return;
}
$this->still($element, Storage::disk('media_derived'), $file, 'element-'.$element->id);
+ $this->rememberElement($element, 'datei');
+ }
+
+ public function forElementUpload(ContentElement $element, string $incoming): void
+ {
+ if (! str_starts_with($incoming, 'vorschau/') || str_contains($incoming, '..')) {
+ $this->fail($element, 'Das Vorschaubild wurde nicht übernommen.');
+
+ return;
+ }
+
+ $this->still($element, Storage::disk('cms_incoming'), $incoming, 'element-'.$element->id);
+ $this->rememberElement($element, 'eigen');
+ }
+
+ public function forElementFrame(ContentElement $element, ContentElementVersion $version, float $seconds): void
+ {
+ if ($version->content_element_id !== $element->id || $element->output_class !== 'film') {
+ $this->fail($element, 'Aus dieser Filmstelle ließ sich kein Vorschaubild erzeugen.');
+
+ return;
+ }
+
+ $bytes = $this->jpegAtVersion($version, $seconds);
+
+ if (! is_string($bytes)) {
+ $this->fail($element, 'Aus dieser Filmstelle ließ sich kein Vorschaubild erzeugen.');
+
+ return;
+ }
+
+ $name = 'element-'.$element->id;
+ $path = 'vorschau/'.$name.'.jpg';
+ Storage::disk('media_derived')->put($path, $bytes);
+ $this->still($element, Storage::disk('media_derived'), $path, $name);
+ $this->rememberElement($element, 'frame:'.$version->id.'@'.$this->secondText($seconds));
}
public function forCompositionUpload(Composition $composition, string $incoming): void
@@ -166,7 +202,7 @@ private function secondText(float $seconds): string
return rtrim(rtrim($text, '0'), '.') ?: '0';
}
- private function frame(ContentElement $element, string $file): void
+ private function frame(ContentElement $element, ContentElementVersion $version, string $file): void
{
$binary = (new ExecutableFinder)->find('ffmpeg');
$disk = Storage::disk('media_derived');
@@ -200,8 +236,13 @@ private function frame(ContentElement $element, string $file): void
}
$element->thumbnail_path = $target;
- $element->thumbnail_alt = $element->working_title;
+ if (! filled($element->thumbnail_alt)) {
+ $element->thumbnail_alt = $element->working_title;
+ }
$element->thumbnail_error = null;
+ if ($element->output_class === 'film') {
+ $element->thumbnail_source = 'frame:'.$version->id.'@0';
+ }
$element->save();
}
@@ -243,7 +284,7 @@ private function still(ContentElement|Composition $subject, Filesystem $disk, st
Storage::disk('media_derived')->put($path, $bytes);
$subject->thumbnail_path = $path;
- if (! ($subject instanceof Composition && filled($subject->thumbnail_alt))) {
+ if (! filled($subject->thumbnail_alt)) {
$subject->thumbnail_alt = $subject instanceof Composition ? $subject->designation : $subject->working_title;
}
@@ -251,6 +292,18 @@ private function still(ContentElement|Composition $subject, Filesystem $disk, st
$subject->save();
}
+ private function rememberElement(ContentElement $element, string $source): void
+ {
+ $element->refresh();
+
+ if ($element->thumbnail_error !== null || ! filled($element->thumbnail_path)) {
+ return;
+ }
+
+ $element->thumbnail_source = $source;
+ $element->save();
+ }
+
private function rememberChoice(Composition $composition, string $source): void
{
$composition->refresh();
diff --git a/app/Domain/Media/ThumbnailRef.php b/app/Domain/Media/ThumbnailRef.php
new file mode 100644
index 0000000..ba80801
--- /dev/null
+++ b/app/Domain/Media/ThumbnailRef.php
@@ -0,0 +1,15 @@
+ $kind, 'subject' => $id]);
+ }
+}
diff --git a/app/Filament/Resources/ContentElements/Pages/EditContentElement.php b/app/Filament/Resources/ContentElements/Pages/EditContentElement.php
index f643434..0039bdb 100644
--- a/app/Filament/Resources/ContentElements/Pages/EditContentElement.php
+++ b/app/Filament/Resources/ContentElements/Pages/EditContentElement.php
@@ -15,10 +15,12 @@
use App\Domain\Catalog\Pillar;
use App\Domain\Catalog\PillarService;
use App\Domain\Catalog\ReleaseContent;
+use App\Domain\Media\ThumbnailRef;
use App\Filament\Resources\ContentElements\ContentElementResource;
use App\Filament\Support\ElementRevision;
use App\Filament\Support\MediaUploadLimit;
use App\Filament\Support\ReportsDomainRules;
+use App\Jobs\DeriveThumbnail;
use Filament\Actions\Action;
use Filament\Forms\Components\CheckboxList;
use Filament\Forms\Components\FileUpload;
@@ -102,9 +104,27 @@ public function form(Schema $schema): Schema
return $options;
})
->helperText('Wirkt auf die Suche und legt keine neue Fassung an.'),
- FileUpload::make('thumbnail_upload')->label('Vorschaubild')
+ Placeholder::make('thumbnail_current')->label('Aktuelles Vorschaubild')->content(function (): HtmlString {
+ /** @var ContentElement $element */
+ $element = $this->getRecord();
+ $url = ThumbnailRef::url('editorial.card', 'element', $element->id, $element->thumbnail_path);
+ $caption = match ($element->thumbnail_source) {
+ 'eigen' => 'Eigenes Vorschaubild.',
+ 'datei' => 'Vorschaubild aus der Datei des Mediums.',
+ default => '',
+ };
+ if (is_string($element->thumbnail_source) && preg_match('/^frame:\d+@([\d.]+)$/', $element->thumbnail_source, $match) === 1) {
+ $caption = 'Vorschaubild aus dem Film bei '.$match[1].' Sekunden.';
+ }
+ if ($url === null) {
+ return new HtmlString('Noch kein Vorschaubild. Eine Filmstelle wählst du beim Ansehen des Mediums.');
+ }
+
+ return new HtmlString(''.($caption !== '' ? '
'.e($caption).'
' : '')); + }), + FileUpload::make('thumbnail_upload')->label('Eigenes Vorschaubild') ->disk('cms_incoming')->directory('vorschau')->visibility('private')->image() - ->helperText('Nur die Karte. Die Inhaltsfassung bleibt unverändert.'), + ->helperText('Gilt für die Anzeige und als Vorschlag in Modulen und Programmen. Eine Filmstelle wählst du beim Ansehen. Speichern legt keine Fassung an.'), CheckboxList::make('pillars') ->label('Fachliche Säulen') ->options(fn (): array => Pillar::query()->orderBy('name')->pluck('name', 'id')->all()) @@ -209,10 +229,9 @@ protected function handleRecordUpdate(Model $record, array $data): Model $record->save(); } if (is_string($data['thumbnail_upload'] ?? null) && $data['thumbnail_upload'] !== '') { - $record->thumbnail_path = $data['thumbnail_upload']; $record->thumbnail_error = null; - $record->thumbnail_alt = $record->working_title; $record->save(); + DeriveThumbnail::dispatch('element', $record->id, $data['thumbnail_upload']); } if (array_key_exists('dimension_ids', $data)) { $ids = array_map(intval(...), $data['dimension_ids'] ?? []); diff --git a/app/Filament/Resources/ContentElements/Pages/ViewContentElement.php b/app/Filament/Resources/ContentElements/Pages/ViewContentElement.php index f23e25d..1124fc5 100644 --- a/app/Filament/Resources/ContentElements/Pages/ViewContentElement.php +++ b/app/Filament/Resources/ContentElements/Pages/ViewContentElement.php @@ -9,11 +9,13 @@ use App\Domain\Catalog\IdentityProfile; use App\Domain\Catalog\Pillar; use App\Domain\Catalog\PillarService; +use App\Domain\Media\ThumbnailRef; use App\Filament\Resources\ContentElements\ContentElementResource; use App\Filament\Support\CatalogReading; use App\Filament\Support\ElementRevision; use App\Filament\Support\ReportsDomainRules; use App\Http\Controllers\ContentPreviewController; +use App\Jobs\DeriveThumbnail; use Filament\Actions\Action; use Filament\Forms\Components\CheckboxList; use Filament\Forms\Components\Textarea; @@ -23,15 +25,26 @@ use Filament\Schemas\Schema; use Illuminate\Contracts\Support\Htmlable; use Illuminate\Support\Collection; +use Livewire\Features\SupportFileUploads\TemporaryUploadedFile; +use Livewire\WithFileUploads; class ViewContentElement extends ViewRecord { use ReportsDomainRules; + use WithFileUploads; protected static string $resource = ContentElementResource::class; public ?int $fassung = null; + public string $frameSecond = ''; + + public string $thumbnailAlt = ''; + + public string $pictureFeedback = ''; + + public mixed $previewUpload = null; + public function mount(int|string $record): void { parent::mount($record); @@ -41,6 +54,8 @@ public function mount(int|string $record): void if ($requested > 0) { $this->fassung = $requested; } + + $this->thumbnailAlt = (string) ($this->getRecord()->thumbnail_alt ?? ''); } public function getSubheading(): string|Htmlable|null @@ -61,6 +76,125 @@ public function showVersion(int $version): void $this->fassung = $version; } + public function adoptFrame(): void + { + abort_unless(ContentElementResource::editor(), 403); + /** @var ContentElement $element */ + $element = $this->getRecord(); + $shown = $this->shownVersion(); + + if (! $shown instanceof ContentElementVersion || $element->output_class !== 'film') { + $this->pictureFeedback = 'Für dieses Medium gibt es keine Filmstelle.'; + + return; + } + + if (! is_numeric($this->frameSecond) || trim($this->frameSecond) === '') { + $this->pictureFeedback = 'Wähle zuerst eine Zeitposition.'; + + return; + } + + $seconds = (float) $this->frameSecond; + + if ($seconds < 0 || $seconds > 7200) { + $this->pictureFeedback = 'Die Zeitposition liegt außerhalb des Films.'; + + return; + } + + $this->keepAlt($element); + $element->thumbnail_error = null; + $element->save(); + DeriveThumbnail::dispatch('element', $element->id, null, null, $shown->id, $seconds); + $element->refresh(); + $this->pictureFeedback = $element->thumbnail_error + ?: 'Das Vorschaubild ist gespeichert. Die Fassung bleibt unverändert.'; + } + + public function storePreview(): void + { + abort_unless(ContentElementResource::editor(), 403); + + if (! $this->previewUpload instanceof TemporaryUploadedFile) { + $this->pictureFeedback = 'Wähle zuerst eine Bilddatei.'; + + return; + } + + $extension = strtolower($this->previewUpload->getClientOriginalExtension()); + + if (! in_array($extension, ['png', 'jpg', 'jpeg', 'webp'], true)) { + $this->pictureFeedback = 'Das Vorschaubild muss eine Bilddatei sein.'; + + return; + } + + $path = $this->previewUpload->storeAs( + 'vorschau', + 'karte-'.bin2hex(random_bytes(4)).'.'.$extension, + 'cms_incoming', + ); + $this->previewUpload = null; + + if (! is_string($path)) { + $this->pictureFeedback = 'Das Vorschaubild wurde nicht übernommen.'; + + return; + } + + $this->savePreviewImage($path); + } + + public function savePreviewImage(string $path): void + { + abort_unless(ContentElementResource::editor(), 403); + /** @var ContentElement $element */ + $element = $this->getRecord(); + $this->keepAlt($element); + $element->thumbnail_error = null; + $element->save(); + DeriveThumbnail::dispatch('element', $element->id, $path); + $element->refresh(); + $this->pictureFeedback = $element->thumbnail_error + ?: 'Das Vorschaubild ist gespeichert. Die Fassung bleibt unverändert.'; + } + + public function useOwnImage(): void + { + abort_unless(ContentElementResource::editor(), 403); + /** @var ContentElement $element */ + $element = $this->getRecord(); + + if ($element->output_class !== 'bild') { + $this->pictureFeedback = 'Die Datei dieses Mediums ist kein Bild. Lade ein Vorschaubild hoch.'; + + return; + } + + $this->keepAlt($element); + $element->thumbnail_error = null; + $element->save(); + DeriveThumbnail::dispatch('element', $element->id); + $element->refresh(); + $this->pictureFeedback = $element->thumbnail_error + ?: 'Das Vorschaubild ist gespeichert. Die Fassung bleibt unverändert.'; + } + + public function framePreviewUrl(): ?string + { + $shown = $this->shownVersion(); + + if (! $shown instanceof ContentElementVersion || ! is_numeric($this->frameSecond) || trim($this->frameSecond) === '') { + return null; + } + + return route('editorial.frame', [ + 'version' => $shown->id, + 'sekunde' => $this->frameSecond, + ]); + } + public function deactivateAction(): Action { return Action::make('deactivate') @@ -158,9 +292,52 @@ private function viewerData(): array 'pillars' => $element->pillars->pluck('name')->all(), 'usage' => app(CatalogReading::class)->elementUsage($element), 'canRevise' => ContentElementResource::editor(), + 'pictureUrl' => ThumbnailRef::url('editorial.card', 'element', $element->id, $element->thumbnail_path), + 'pictureCaption' => $this->pictureCaption($element), ]; } + private function shownVersion(): ?ContentElementVersion + { + /** @var ContentElement $element */ + $element = $this->getRecord(); + $versions = $element->versions()->orderByDesc('number')->get(); + + return $versions->firstWhere('id', $this->fassung) + ?? $versions->first(fn (ContentElementVersion $version): bool => $version->status === ContentVersionStatus::Bereit) + ?? $versions->first(); + } + + private function keepAlt(ContentElement $element): void + { + $alt = trim($this->thumbnailAlt); + + if ($alt === '') { + return; + } + + $element->thumbnail_alt = $alt; + } + + private function pictureCaption(ContentElement $element): string + { + $source = $element->thumbnail_source; + + if (! is_string($source) || $source === '') { + return ''; + } + + if (preg_match('/^frame:\d+@([\d.]+)$/', $source, $match) === 1) { + return 'Vorschaubild aus dem Film bei '.$match[1].' Sekunden.'; + } + + return match ($source) { + 'eigen' => 'Eigenes Vorschaubild.', + 'datei' => 'Vorschaubild aus der Datei des Mediums.', + default => '', + }; + } + private function ownedVersion(int $id): ?ContentElementVersion { /** @var ContentElement $element */ diff --git a/app/Filament/Support/EditorialCatalog.php b/app/Filament/Support/EditorialCatalog.php index 5428930..1cb8b73 100644 --- a/app/Filament/Support/EditorialCatalog.php +++ b/app/Filament/Support/EditorialCatalog.php @@ -645,17 +645,122 @@ public function cover(Composition $composition, array $members): array return ['url' => $own, 'note' => null, 'own' => true]; } - $films = $this->filmPictures($members); + $picture = $this->suggestPicture($members); - if (count($films) === 1) { + if ($picture === null) { + return ['url' => null, 'note' => null, 'own' => false]; + } + + return [ + 'url' => $this->cardUrl($picture['kind'], $picture['subject'], 'vorschau/gesetzt'), + 'note' => $picture['note'], + 'own' => false, + ]; + } + + /** + * Das eigene Titelbild, sonst das erste Vorschaubild aus dem freigegebenen Aufbau. + * + * @return array{kind: string, subject: int, alt: string, note: string}|null + */ + public function visiblePicture(Composition $composition, ?CompositionVersion $version): ?array + { + if ($this->cardUrl('komposition', $composition->id, $composition->thumbnail_path) !== null) { return [ - 'url' => $films[0]['image'], - 'note' => 'Vorschlag aus dem Film '.$films[0]['title'], - 'own' => false, + 'kind' => 'komposition', + 'subject' => $composition->id, + 'alt' => filled($composition->thumbnail_alt) ? (string) $composition->thumbnail_alt : $this->compositionTitle($composition), + 'note' => '', ]; } - return ['url' => null, 'note' => null, 'own' => false]; + if ($version === null) { + return null; + } + + $version->loadMissing('members'); + $members = []; + + foreach ($version->members as $member) { + $members[] = [ + 'target' => $member->content_element_version_id + ? 'element:'.$member->content_element_version_id + : 'composition:'.$member->nested_composition_version_id, + ]; + } + + return $this->suggestPicture($members); + } + + /** + * @param list{{ $pillars === [] ? 'Keiner Säule zugeordnet.' : implode(' · ', $pillars) }}
+ @if ($canRevise || $pictureUrl) +Gilt für die Anzeige dieses Mediums und als Vorschlag in Modulen und Programmen, solange die dort kein eigenes Titelbild haben. Eine Inhaltsfassung entsteht dabei nicht.
+ @if ($pictureUrl) +Noch kein Vorschaubild.
+ @endif + @if ($pictureCaption !== '') +{{ $pictureCaption }}
+ @endif + @if ($this->pictureFeedback !== '') +{{ $this->pictureFeedback }}
+ @endif + @if ($canRevise) + + + @if ($element->output_class === 'film' && $shown) +Die Vorschau nimmt die kleine Filmstufe, sofern sie vorliegt. Die Filmdatei selbst bleibt unverändert.
+ @if ($frameUrl = $this->framePreviewUrl()) +Noch keine Fassung.
diff --git a/resources/views/member/article.blade.php b/resources/views/member/article.blade.php index 28f98b1..a4e0c96 100644 --- a/resources/views/member/article.blade.php +++ b/resources/views/member/article.blade.php @@ -28,7 +28,7 @@ @endif @if ($entry['playable'])