From 5037fafa790b4d6a0157f38f7237c45aa69bf157 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Tue, 19 Jul 2022 15:22:56 +0200 Subject: [PATCH] wait #5363 @0.5 --- .../Controllers/Base/XSendFileController.php | 29 +++++++++++++++++-- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/app/Http/Controllers/Base/XSendFileController.php b/src/app/Http/Controllers/Base/XSendFileController.php index 8392cbc..18d6b4b 100644 --- a/src/app/Http/Controllers/Base/XSendFileController.php +++ b/src/app/Http/Controllers/Base/XSendFileController.php @@ -9,10 +9,33 @@ class XSendFileController extends Controller { protected function xSendFile($path) { - if (file_exists($path)) { - return response(null)->header('Content-Type', Files::_getMimeType($path))->header('X-Sendfile', $path); + return self::sendfile($path); + } + + public static function sendfile($path, $maxage = 86400) + { + $response = response(null); + $request = request(); + if (!file_exists($path)) { + return $response->setStatusCode(404); + } + $mtime = filemtime($path); + $fsize = filesize($path); + $response->header('Content-Type', Files::_getMimeType($path))->header('Content-Length', $fsize)->header('Last-Modified', gmdate('D, d M Y H:i:s T', $mtime)); + if ($maxage > 0) { + $etag = $mtime . '.' . $fsize; + $response->header('ETag', '"' . $etag . '"')->header('Cache-Control', 'max-age=' . $maxage); + $httpisnotmatch = $request->server('HTTP_IF_NONE_MATCH'); + if (null !== $httpisnotmatch && $etag === $httpisnotmatch) { + return $response->setStatusCode(304); + } } else { - return response(null)->setStatusCode(404); + $response->header('Cache-Control', 'no-store'); } + return $response->header('X-Sendfile', $path); + } + + public static function sendfileNoCache($path){ + return self::sendfile($path,0); } } -- 2.39.5