Die Karte spielt den für die Website freigegebenen Trailer (Rolle Trailer, sonst Vorschauclip, bei einem Film ersatzweise sein Teaser) stumm beim Überfahren mit der Maus ab, am Handy mit Tippen, per Tastatur über die Schaltfläche. Reduzierte Bewegung und Datensparmodus starten nichts von selbst; der Clip wird erst beim ersten Start geladen. Ohne Freigabe bleibt das Kartenbild, die Redaktionsvorschau nennt den Grund. Neue Einstellung "Trailer auf der Karte" je Karte (ADR-43). Die Route website.media beantwortet Bereichsabrufe (206/416) für Filme und Hörstücke, damit Safari und iOS abspielen. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
101 lines
3.1 KiB
PHP
101 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Domain\Catalog\ContentElement;
|
|
use App\Domain\Media\WebsiteMedia;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Symfony\Component\HttpFoundation\StreamedResponse;
|
|
|
|
class WebsiteMediaController extends Controller
|
|
{
|
|
private const CHUNK = 262144;
|
|
|
|
public function show(Request $request, ContentElement $element, WebsiteMedia $media): StreamedResponse
|
|
{
|
|
$presentation = $media->present($element);
|
|
abort_if($presentation === null, 404);
|
|
|
|
$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];
|
|
}
|
|
}
|