diff --git a/app/Http/Controllers/Concerns/ServesByteRanges.php b/app/Http/Controllers/Concerns/ServesByteRanges.php new file mode 100644 index 0000000..0c7eab9 --- /dev/null +++ b/app/Http/Controllers/Concerns/ServesByteRanges.php @@ -0,0 +1,94 @@ + 'inline', + 'X-Content-Type-Options' => 'nosniff', + 'Accept-Ranges' => 'bytes', + ]; + $range = $request->header('Range'); + + if (! is_string($range) || ! $ranged) { + return Storage::disk($diskName)->response($path, null, $headers); + } + + $disk = Storage::disk($diskName); + $size = (int) $disk->size($path); + $bounds = $this->bounds($range, $size); + + if ($bounds === null) { + return response()->stream(fn () => null, 416, $headers + ['Content-Range' => 'bytes */'.$size]); + } + + [$start, $end] = $bounds; + $length = $end - $start + 1; + $stream = $disk->readStream($path); + abort_if(! is_resource($stream), 404); + + return response()->stream(function () use ($stream, $start, $length): void { + fseek($stream, $start); + $left = $length; + + while ($left > 0 && ! feof($stream)) { + $chunk = fread($stream, min(self::CHUNK, $left)); + + if ($chunk === false || $chunk === '') { + break; + } + + echo $chunk; + $left -= strlen($chunk); + } + + fclose($stream); + }, 206, $headers + [ + 'Content-Type' => (string) ($disk->mimeType($path) ?: 'application/octet-stream'), + 'Content-Length' => (string) $length, + 'Content-Range' => 'bytes '.$start.'-'.$end.'/'.$size, + ]); + } + + /** + * Erster Bereich aus „bytes=a-b“, „bytes=a-“ oder „bytes=-n“; null, wenn er nicht erfüllbar ist. + * + * @return array{0: int, 1: int}|null + */ + private function bounds(string $header, int $size): ?array + { + if ($size < 1 || preg_match('/^bytes=(\d*)-(\d*)(?:,.*)?$/', trim($header), $match) !== 1) { + return null; + } + + [, $from, $to] = $match; + + if ($from === '' && $to === '') { + return null; + } + + if ($from === '') { + $suffix = min((int) $to, $size); + + return $suffix < 1 ? null : [$size - $suffix, $size - 1]; + } + + $start = (int) $from; + $end = $to === '' ? $size - 1 : min((int) $to, $size - 1); + + return $start > $end || $start >= $size ? null : [$start, $end]; + } +} diff --git a/app/Http/Controllers/ContentPreviewController.php b/app/Http/Controllers/ContentPreviewController.php index 913e219..afdf4f0 100644 --- a/app/Http/Controllers/ContentPreviewController.php +++ b/app/Http/Controllers/ContentPreviewController.php @@ -2,12 +2,15 @@ namespace App\Http\Controllers; +use App\Domain\Catalog\ContentElement; use App\Domain\Catalog\ContentElementVersion; use App\Domain\Identity\StaffRole; use App\Domain\Media\FilmPlayback; use App\Domain\Media\HlsPlaylist; use App\Domain\Media\MediaRendition; +use App\Http\Controllers\Concerns\ServesByteRanges; use App\Models\User; +use Illuminate\Http\Request; use Illuminate\Http\Response; use Illuminate\Support\Facades\Storage; use Illuminate\View\View; @@ -15,6 +18,27 @@ class ContentPreviewController extends Controller { + use ServesByteRanges; + + /** + * Die Website-Datei eines Films oder Hörstücks, damit die Redaktion sie vor der Freigabe ansehen kann. Nur für + * angemeldete Redakteure; öffentlich ist erst die freigegebene Kopie (`website.media`). + */ + public function websiteFile(Request $request, ContentElement $element): StreamedResponse + { + $this->authorizeStaff(); + $path = $element->preview_path; + + abort_unless( + is_string($path) && str_starts_with($path, 'teaser/') && ! str_contains($path, '..') + && in_array($element->output_class, ['film', 'audio'], true) + && Storage::disk('media_derived')->exists($path), + 404, + ); + + return $this->respondWithRanges($request, 'media_derived', $path, true); + } + public function show(ContentElementVersion $version): View { $this->authorizeStaff(); diff --git a/app/Http/Controllers/WebsiteMediaController.php b/app/Http/Controllers/WebsiteMediaController.php index 7d16a4a..df85a7c 100644 --- a/app/Http/Controllers/WebsiteMediaController.php +++ b/app/Http/Controllers/WebsiteMediaController.php @@ -4,13 +4,13 @@ use App\Domain\Catalog\ContentElement; use App\Domain\Media\WebsiteMedia; +use App\Http\Controllers\Concerns\ServesByteRanges; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Storage; use Symfony\Component\HttpFoundation\StreamedResponse; class WebsiteMediaController extends Controller { - private const CHUNK = 262144; + use ServesByteRanges; public function show(Request $request, ContentElement $element, WebsiteMedia $media): StreamedResponse { @@ -20,81 +20,6 @@ public function show(Request $request, ContentElement $element, WebsiteMedia $me $path = $element->website_public_path; abort_if(! is_string($path), 404); - $headers = [ - 'Content-Disposition' => 'inline', - 'X-Content-Type-Options' => 'nosniff', - 'Accept-Ranges' => 'bytes', - ]; - - $range = $request->header('Range'); - - // Filme und Hörstücke brauchen Bereichsabrufe (Safari und iOS spielen sonst nicht ab, Springen ist sonst unmöglich). - if (! is_string($range) || $presentation->kind === 'bild') { - return Storage::disk('media_public')->response($path, null, $headers); - } - - $disk = Storage::disk('media_public'); - $size = (int) $disk->size($path); - $bounds = $this->bounds($range, $size); - - if ($bounds === null) { - return response()->stream(fn () => null, 416, $headers + ['Content-Range' => 'bytes */'.$size]); - } - - [$start, $end] = $bounds; - $length = $end - $start + 1; - $stream = $disk->readStream($path); - abort_if(! is_resource($stream), 404); - - return response()->stream(function () use ($stream, $start, $length): void { - fseek($stream, $start); - $left = $length; - - while ($left > 0 && ! feof($stream)) { - $chunk = fread($stream, min(self::CHUNK, $left)); - - if ($chunk === false || $chunk === '') { - break; - } - - echo $chunk; - $left -= strlen($chunk); - } - - fclose($stream); - }, 206, $headers + [ - 'Content-Type' => (string) ($disk->mimeType($path) ?: 'application/octet-stream'), - 'Content-Length' => (string) $length, - 'Content-Range' => 'bytes '.$start.'-'.$end.'/'.$size, - ]); - } - - /** - * Erster Bereich aus „bytes=a-b“, „bytes=a-“ oder „bytes=-n“; null, wenn er nicht erfüllbar ist. - * - * @return array{0: int, 1: int}|null - */ - private function bounds(string $header, int $size): ?array - { - if ($size < 1 || preg_match('/^bytes=(\d*)-(\d*)(?:,.*)?$/', trim($header), $match) !== 1) { - return null; - } - - [, $from, $to] = $match; - - if ($from === '' && $to === '') { - return null; - } - - if ($from === '') { - $suffix = min((int) $to, $size); - - return $suffix < 1 ? null : [$size - $suffix, $size - 1]; - } - - $start = (int) $from; - $end = $to === '' ? $size - 1 : min((int) $to, $size - 1); - - return $start > $end || $start >= $size ? null : [$start, $end]; + return $this->respondWithRanges($request, 'media_public', $path, $presentation->kind !== 'bild'); } } diff --git a/resources/views/filament/media/viewer.blade.php b/resources/views/filament/media/viewer.blade.php index a0c6c71..8ab9864 100644 --- a/resources/views/filament/media/viewer.blade.php +++ b/resources/views/filament/media/viewer.blade.php @@ -97,6 +97,7 @@ @if ($fokus === 'inhalt') @if ($versions->isEmpty()) {{-- Viele Medien sind reine Präsentationsmedien ohne Fassung: kein leerer Strukturbereich. --}} + @include('filament.media.website-datei')
Noch keine Inhaltsfassung

Dieses Medium wird ohne Fassung verwendet, zum Beispiel als Präsentationsbild auf Karten und Seiten. Eine Fassung brauchst du erst, wenn die Datei oder ein Lehrtext in Modulen und Programmen eingesetzt werden soll.

@@ -343,16 +344,13 @@ @if (in_array($element->output_class, ['film', 'audio'], true)) @php $webDatei = filled($element->preview_path); - $webFehler = ! $webDatei && filled($element->thumbnail_error) ? $element->thumbnail_error : null; $webTypen = $element->output_class === 'audio' ? '.mp3,.m4a,.ogg' : '.mp4,.webm'; @endphp + @include('filament.media.website-datei')
Website-Datei
{{ $webDatei ? 'Vorhanden (ungeschützte Datei für die Website)' : 'Noch keine. Ohne sie lässt sich nur eine bereitgestellte Fassung mit Teaser freigeben.' }}
- @if ($webFehler) - - @endif @if ($editable)
diff --git a/resources/views/filament/media/website-datei.blade.php b/resources/views/filament/media/website-datei.blade.php new file mode 100644 index 0000000..9989e04 --- /dev/null +++ b/resources/views/filament/media/website-datei.blade.php @@ -0,0 +1,17 @@ +@php + /** Die Website-Datei eines Films oder Hörstücks: Ansicht für die Redaktion, öffentlich erst nach der Freigabe. */ + $datei = in_array($element->output_class, ['film', 'audio'], true) && filled($element->preview_path); + $fehler = in_array($element->output_class, ['film', 'audio'], true) && ! $datei && filled($element->thumbnail_error) ? $element->thumbnail_error : null; +@endphp +@if ($datei) +
+ @if ($element->output_class === 'audio') + + @else + + @endif +
Website-Datei, ungeschützt. Sie ist {{ $webOffen ? 'für die Website freigegeben' : 'noch nicht für die Website freigegeben' }}.
+
+@elseif ($fehler) + +@endif diff --git a/routes/web.php b/routes/web.php index 09a2712..37c3ea9 100644 --- a/routes/web.php +++ b/routes/web.php @@ -80,6 +80,10 @@ ->whereIn('kind', ['element', 'komposition']) ->whereNumber('subject') ->name('editorial.card'); +Route::get('/redaktion/website-datei/{element}', [ContentPreviewController::class, 'websiteFile']) + ->middleware('auth:staff') + ->whereNumber('element') + ->name('content.website-file'); Route::get('/redaktion/inhalt/{version}', [ContentPreviewController::class, 'show']) ->middleware('auth:staff') ->name('content.preview'); diff --git a/tests/Feature/WebsiteFileTest.php b/tests/Feature/WebsiteFileTest.php index 1bb591a..4ab8eff 100644 --- a/tests/Feature/WebsiteFileTest.php +++ b/tests/Feature/WebsiteFileTest.php @@ -164,6 +164,44 @@ public function test_replacing_the_file_of_a_released_film_publishes_the_new_cop Queue::assertPushed(PublishWebsiteMedia::class, fn (PublishWebsiteMedia $job): bool => $job->elementId === $film->id && $job->action === 'release'); } + public function test_a_versionless_film_shows_its_website_file_in_the_media_manager(): void + { + Storage::disk('cms_incoming')->put('inhalte/trailer.mp4', '0123456789'); + $this->actingAs($this->editor(), 'staff'); + $film = MediumForm::create([ + 'working_title' => 'Trailer', + 'shape' => 'film', + 'incoming' => 'inhalte/trailer.mp4', + 'without_version' => true, + ], hold: false)->fresh(); + $url = route('content.website-file', $film); + + Livewire::test(ViewContentElement::class, ['record' => $film->getKey()]) + ->assertSeeHtml('