]> _ Git - cubist_cms-back.git/commitdiff
wait #5363 @0.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 19 Jul 2022 13:22:56 +0000 (15:22 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 19 Jul 2022 13:22:56 +0000 (15:22 +0200)
src/app/Http/Controllers/Base/XSendFileController.php

index 8392cbcd56e4ce4352d6f122374cd2472a1d6796..18d6b4ba720f8b3fca6448185f27edee9fab5da4 100644 (file)
@@ -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);
     }
 }