Zeige die Website-Datei eines Mediums ohne Fassung im Medienmanager an
CI Feature / test (push) Successful in 2m5s
Deploy Test / test (push) Successful in 1m56s
Deploy Test / deploy (push) Successful in 27s

Ein Film oder Hörstück ohne Inhaltsfassung zeigte nur „Noch keine Inhaltsfassung“; die
hochgeladene Website-Datei war nirgends ansehbar. Die Detailansicht spielt sie jetzt ab
(Reiter Inhalt und Website), über eine Redaktions-Route nur für angemeldete Redakteure,
mit Bereichsabrufen. Die Bereichsauslieferung liegt in einem gemeinsamen Trait für die
Website-Route und die Redaktions-Route. Fehler der Übernahme erscheinen an derselben Stelle.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
LarsandClaude Sonnet 5.5 committed 2026-11-22 06:55:36 +01:00
1 parent ef3af0e036
commit be932c113a
7 files changed
+182 -82

No files matched your search

@@ -0,0 +1,94 @@
<?php
namespace App\Http\Controllers\Concerns;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\StreamedResponse;
/**
* Liefert eine Datei einer Disk aus, bei Filmen und Hörstücken mit Bereichsabrufen (206/416). Safari und iOS spielen
* sonst nicht ab, und ohne Bereiche ist kein Springen möglich.
*/
trait ServesByteRanges
{
private const CHUNK = 262144;
protected function respondWithRanges(Request $request, string $diskName, string $path, bool $ranged): StreamedResponse
{
$headers = [
'Content-Disposition' => '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];
}
}
@@ -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();
@@ -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');
}
}