LibraryController::fileResponse nutzt ServesByteRanges (206/416): Safari und iOS spielen ab, Springen und Fortsetzen eines Downloads gehen. Dateiname und attachment bleiben. Der Nachweis zählt einen vollständigen Abruf als eine Auslieferung; Teilabrufe schreiben nur, wenn noch kein Eintrag der letzten zehn Minuten steht. FileChecksum merkt die SHA-256 je Datei (Pfad, Größe, Zeit) und wird beim Speichern vorgewärmt, damit die erste Wiedergabe nicht auf das Hashen einer großen Datei wartet. Der entfernte decision-Schlüssel der Artikelseite war ungenutzt. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
115 lines
3.6 KiB
PHP
115 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Concerns;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Symfony\Component\HttpFoundation\HeaderUtils;
|
|
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;
|
|
|
|
/**
|
|
* @param string|null $name Dateiname für Ansicht oder Download (ohne: Name der Datei)
|
|
* @param string $disposition inline oder attachment
|
|
* @param array<string, string> $extra weitere Kopfzeilen
|
|
*/
|
|
protected function respondWithRanges(
|
|
Request $request,
|
|
string $diskName,
|
|
string $path,
|
|
bool $ranged,
|
|
?string $name = null,
|
|
string $disposition = 'inline',
|
|
array $extra = [],
|
|
): StreamedResponse {
|
|
$headers = [
|
|
'Content-Disposition' => $name === null ? $disposition : $this->disposition($disposition, $name),
|
|
'X-Content-Type-Options' => 'nosniff',
|
|
'Accept-Ranges' => 'bytes',
|
|
] + $extra;
|
|
$range = $request->header('Range');
|
|
|
|
if (! is_string($range) || ! $ranged) {
|
|
return Storage::disk($diskName)->response($path, $name, $headers, $disposition);
|
|
}
|
|
|
|
$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,
|
|
]);
|
|
}
|
|
|
|
private function disposition(string $type, string $name): string
|
|
{
|
|
$fallback = (string) preg_replace('/[^ -~]|["\\%]/', '_', $name);
|
|
|
|
return HeaderUtils::makeDisposition($type, $name, $fallback);
|
|
}
|
|
|
|
/**
|
|
* 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];
|
|
}
|
|
}
|