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>
95 lines
2.9 KiB
PHP
95 lines
2.9 KiB
PHP
<?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];
|
|
}
|
|
}
|