diff --git a/app/Domain/Media/ThumbnailDeriver.php b/app/Domain/Media/ThumbnailDeriver.php index e03f8a4..7a197c4 100644 --- a/app/Domain/Media/ThumbnailDeriver.php +++ b/app/Domain/Media/ThumbnailDeriver.php @@ -87,6 +87,27 @@ public function forElementFrame(ContentElement $element, ContentElementVersion $ $this->rememberElement($element, 'frame:'.$version->id.'@'.$this->secondText($seconds)); } + /** + * Vorschaubild aus einer Stelle der Website-Datei. Die Herkunft steht als `frame:0@{Sekunde}`; die 0 steht für die + * Website-Datei (es gibt keine Fassung), alle Leser der Herkunft kommen damit zurecht. + */ + public function forElementWebsiteFrame(ContentElement $element, float $seconds): void + { + $bytes = $this->jpegAtWebsiteFile($element, $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:0@'.$this->secondText($seconds)); + } + public function forCompositionUpload(Composition $composition, string $incoming): void { if (! str_starts_with($incoming, 'vorschau/') || str_contains($incoming, '..')) { @@ -187,7 +208,26 @@ public function frameJpeg(ContentElementVersion $version, float $seconds, int $w $file = $this->frameInput($version, $original); - if ($file === null) { + return $file === null ? null : $this->jpegFromFile($file, $seconds, $width); + } + + /** + * Einzelbild aus der Website-Datei eines Films (`teaser/`), für Medien ohne Inhaltsfassung. + */ + public function jpegAtWebsiteFile(ContentElement $element, float $seconds): ?string + { + $file = $element->preview_path; + + if ($element->output_class !== 'film' || ! $this->readableDerived($file, 'teaser/')) { + return null; + } + + return $this->jpegFromFile($file, $seconds, 640); + } + + private function jpegFromFile(string $file, float $seconds, int $width): ?string + { + if ($seconds < 0 || $seconds > 7200) { return null; } diff --git a/app/Filament/Resources/ContentElements/Pages/ViewContentElement.php b/app/Filament/Resources/ContentElements/Pages/ViewContentElement.php index 758851e..0a3f203 100644 --- a/app/Filament/Resources/ContentElements/Pages/ViewContentElement.php +++ b/app/Filament/Resources/ContentElements/Pages/ViewContentElement.php @@ -266,8 +266,9 @@ public function adoptFrame(): void /** @var ContentElement $element */ $element = $this->getRecord(); $shown = $this->shownVersion(); + $fromWebsiteFile = ! $shown instanceof ContentElementVersion && $this->hasWebsiteFilm($element); - if (! $shown instanceof ContentElementVersion || $element->output_class !== 'film') { + if ((! $shown instanceof ContentElementVersion && ! $fromWebsiteFile) || $element->output_class !== 'film') { $this->pictureFeedback = 'Für dieses Medium gibt es keine Filmstelle.'; return; @@ -290,7 +291,9 @@ public function adoptFrame(): void $this->keepAlt($element); $element->thumbnail_error = null; $element->save(); - DeriveThumbnail::dispatch('element', $element->id, null, null, $shown->id, $seconds); + $fromWebsiteFile + ? DeriveThumbnail::dispatch('element', $element->id, null, null, null, $seconds, true) + : 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.'; @@ -365,10 +368,24 @@ public function useOwnImage(): void ?: 'Das Vorschaubild ist gespeichert. Die Fassung bleibt unverändert.'; } + /** Ein Film ohne Inhaltsfassung, aber mit Website-Datei: die Filmstelle kommt aus dieser Datei. */ + public function hasWebsiteFilm(?ContentElement $element = null): bool + { + $element ??= $this->getRecord(); + + return $element->output_class === 'film' + && filled($element->preview_path) + && ! $element->versions()->exists(); + } + public function framePreviewUrl(): ?string { $shown = $this->shownVersion(); + if (! $shown instanceof ContentElementVersion && $this->hasWebsiteFilm() && is_numeric($this->frameSecond) && trim($this->frameSecond) !== '') { + return route('content.website-frame', ['element' => $this->getRecord(), 'sekunde' => $this->frameSecond]); + } + if (! $shown instanceof ContentElementVersion || ! is_numeric($this->frameSecond) || trim($this->frameSecond) === '') { return null; } diff --git a/app/Http/Controllers/ContentPreviewController.php b/app/Http/Controllers/ContentPreviewController.php index afdf4f0..d60c109 100644 --- a/app/Http/Controllers/ContentPreviewController.php +++ b/app/Http/Controllers/ContentPreviewController.php @@ -8,6 +8,7 @@ use App\Domain\Media\FilmPlayback; use App\Domain\Media\HlsPlaylist; use App\Domain\Media\MediaRendition; +use App\Domain\Media\ThumbnailDeriver; use App\Http\Controllers\Concerns\ServesByteRanges; use App\Models\User; use Illuminate\Http\Request; @@ -62,6 +63,24 @@ public function file(ContentElementVersion $version): StreamedResponse ]); } + /** + * Einzelbild einer Stelle der Website-Datei, zur Auswahl des Vorschaubilds. + */ + public function websiteFrame(Request $request, ContentElement $element, ThumbnailDeriver $deriver): Response + { + $this->authorizeStaff(); + $seconds = $request->query('sekunde'); + abort_unless(is_numeric($seconds), 404); + + $bytes = $deriver->jpegAtWebsiteFile($element, (float) $seconds); + abort_unless(is_string($bytes), 404); + + return response($bytes, 200, [ + 'Content-Type' => 'image/jpeg', + 'Cache-Control' => 'private, no-store', + ]); + } + public function fileUrl(ContentElementVersion $version): ?string { return $this->storedPath($version) === null ? null : route('content.file', $version); diff --git a/app/Jobs/DeriveThumbnail.php b/app/Jobs/DeriveThumbnail.php index 60a3c09..83031db 100644 --- a/app/Jobs/DeriveThumbnail.php +++ b/app/Jobs/DeriveThumbnail.php @@ -20,6 +20,7 @@ public function __construct( public readonly ?int $fromElement = null, public readonly ?int $versionId = null, public readonly ?float $atSecond = null, + public readonly bool $websiteFile = false, ) {} public function handle(ThumbnailDeriver $deriver): void @@ -54,6 +55,12 @@ public function handle(ThumbnailDeriver $deriver): void return; } + if ($this->websiteFile && $this->atSecond !== null) { + $deriver->forElementWebsiteFrame($element, $this->atSecond); + + return; + } + if ($this->versionId !== null && $this->atSecond !== null) { $version = ContentElementVersion::query()->find($this->versionId); diff --git a/resources/views/filament/media/viewer.blade.php b/resources/views/filament/media/viewer.blade.php index 8ab9864..c25e7c7 100644 --- a/resources/views/filament/media/viewer.blade.php +++ b/resources/views/filament/media/viewer.blade.php @@ -248,7 +248,7 @@ @if ($canRevise)