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]; } }