diff --git a/src/Controllers/PostController.php b/src/Controllers/PostController.php index ee5e80b8..8a9bac75 100644 --- a/src/Controllers/PostController.php +++ b/src/Controllers/PostController.php @@ -529,12 +529,9 @@ class PostController if (!is_readable($path)) throw new SimpleException('Thumbnail file is not readable'); - $ttl = 60 * 60 * 24 * 3; - \Chibi\HeadersHelper::set('Pragma', 'public'); - \Chibi\HeadersHelper::set('Cache-Control', 'public, max-age=' . $ttl); - \Chibi\HeadersHelper::set('Expires', gmdate('D, d M Y H:i:s \G\M\T', time() + $ttl)); - + $this->context->transport->cacheDaysToLive = 30; $this->context->transport->mimeType = 'image/png'; + $this->context->transport->fileHash = 'thumb' . $post->file_hash; $this->context->transport->filePath = $path; } @@ -568,13 +565,12 @@ class PostController $ext); $fn = preg_replace('/[[:^print:]]/', '', $fn); - $ttl = 60 * 60 * 24; - \Chibi\HeadersHelper::set('Pragma', 'public'); - \Chibi\HeadersHelper::set('Cache-Control', 'public, max-age=' . $ttl); - \Chibi\HeadersHelper::set('Expires', gmdate('D, d M Y H:i:s \G\M\T', time() + $ttl)); + $ttl = 60 * 60 * 24 * 14; + $this->context->transport->cacheDaysToLive = 14; $this->context->transport->customFileName = $fn; $this->context->transport->mimeType = $post->mimeType; + $this->context->transport->fileHash = 'post' . $post->file_hash; $this->context->transport->filePath = $path; } diff --git a/src/Views/layout-file.phtml b/src/Views/layout-file.phtml index 180d64de..7a4f4386 100644 --- a/src/Views/layout-file.phtml +++ b/src/Views/layout-file.phtml @@ -6,6 +6,24 @@ if (!empty($this->context->transport->errorMessage)) } else { + $lastModified = filemtime($this->context->transport->filePath); + $eTag = $this->context->transport->fileHash; + $ttl = $this->context->transport->cacheDaysToLive * 24 * 3600; + + $ifModifiedSince = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) + ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] + : false; + + $eTagHeader = isset($_SERVER['HTTP_IF_NONE_MATCH']) + ? trim(trim($_SERVER['HTTP_IF_NONE_MATCH']), '"') + : false; + + \Chibi\HeadersHelper::set('ETag', '"' . $eTag . '"'); + \Chibi\HeadersHelper::set('Last-Modified', gmdate('D, d M Y H:i:s \G\M\T', $lastModified)); + \Chibi\HeadersHelper::set('Pragma', 'public'); + \Chibi\HeadersHelper::set('Cache-Control', 'public, max-age=' . $ttl); + \Chibi\HeadersHelper::set('Expires', gmdate('D, d M Y H:i:s \G\M\T', time() + $ttl)); + if (isset($this->context->transport->customFileName)) { \Chibi\HeadersHelper::set( @@ -17,6 +35,12 @@ else 'Content-Type', $this->context->transport->mimeType); + if (strtotime($ifModifiedSince) == $lastModified or $eTagHeader == $eTag) + { + header('HTTP/1.1 304 Not Modified'); + exit; + } + readfile($this->context->transport->filePath); flush(); }