exists($path)) { abort(404); } $mimeType = $disk->mimeType($path) ?: 'application/octet-stream'; $lastModified = $disk->lastModified($path); $etag = md5($path . $lastModified); $size = $disk->size($path); if ($this->isNotModified($etag, $lastModified)) { return response('', 304) ->header('ETag', '"' . $etag . '"') ->header('Cache-Control', 'public, max-age=86400'); } return new StreamedResponse(function () use ($disk, $path) { $stream = $disk->readStream($path); fpassthru($stream); if (is_resource($stream)) { fclose($stream); } }, 200, [ 'Content-Type' => $mimeType, 'Content-Length' => $size, 'ETag' => '"' . $etag . '"', 'Last-Modified' => gmdate('D, d M Y H:i:s', $lastModified) . ' GMT', 'Cache-Control' => 'public, max-age=86400', ]); } private function isNotModified(string $etag, int $lastModified): bool { $requestEtag = request()->header('If-None-Match'); if ($requestEtag && $requestEtag === '"' . $etag . '"') { return true; } $ifModifiedSince = request()->header('If-Modified-Since'); if ($ifModifiedSince && strtotime($ifModifiedSince) >= $lastModified) { return true; } return false; } }