From 74bd94d30de6340fcda67215f49f97c8ae7147c9 Mon Sep 17 00:00:00 2001 From: int2001 Date: Fri, 26 Dec 2025 19:42:20 +0000 Subject: [PATCH] Aggresive Browser-Caching --- application/controllers/Eqsl.php | 49 +++++++++++++++++++++++----- application/views/eqslcard/index.php | 2 +- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/application/controllers/Eqsl.php b/application/controllers/Eqsl.php index 9d59d5dc9..4ae30fcdf 100644 --- a/application/controllers/Eqsl.php +++ b/application/controllers/Eqsl.php @@ -299,6 +299,10 @@ class eqsl extends CI_Controller { $this->session->set_flashdata('error', __("You're not allowed to do that!")); redirect('dashboard'); } + $etag=$this->gen_check_etag("wl_eqsl_cacher_".$id,$width); + if ($etag == '0') { // Cached on Client side + return; + } $this->load->library('electronicqsl'); $this->load->model('Eqsl_images'); @@ -406,28 +410,55 @@ class eqsl extends CI_Controller { log_message('error', 'Failed to save eQSL image to: ' . $image_path); } - $this->output_image_with_width($content, $width); + $this->output_image_with_width($content, $width, $etag); // This must be 1st time (because it's freshly fetched from eQSL) - so add the etag return; // Only process the first image found } } else { - // Load cached image - $image_file = $this->Eqsl_images->get_imagePath('p') . '/' . $this->Eqsl_images->get_image($id); - $content = file_get_contents($image_file); - if ($content !== false) { - $this->output_image_with_width($content, $width); - } else { - show_error(__('Failed to load cached eQSL image'), 500); + // Load server-cached image if etag isn't 0 + if ($etag != '0') { + $image_file = $this->Eqsl_images->get_imagePath('p') . '/' . $this->Eqsl_images->get_image($id); + $content = file_get_contents($image_file); + if ($content !== false) { + $this->output_image_with_width($content, $width, $etag); + } else { + show_error(__('Failed to load cached eQSL image'), 500); + } } } } + private function gen_check_etag($eta,$modifier) { + + $etag = '"' . md5($eta.$modifier) . '"'; + + if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && + trim($_SERVER['HTTP_IF_NONE_MATCH']) === $etag) { + session_write_close(); + session_cache_limiter('public'); + header('HTTP/1.1 304 Not Modified'); + header('ETag: ' . $etag); + header('Pragma: public'); + header('Cache-Control: public, max-age=31536000, immutable'); + header('Expires: ' . gmdate('D, d M Y H:i:s', strtotime('+1 year')) . ' GMT'); // Never expire + return '0'; + } + return $etag; + } + /** * Output image with optional width-based thumbnail generation * @param string $image_data Binary image data * @param int $width Desired width (null for original size) */ - private function output_image_with_width($image_data, $width) { + private function output_image_with_width($image_data, $width, $etag) { + session_write_close(); + session_cache_limiter('public'); + header('Content-Type: image/jpg'); + header('ETag: ' . $etag); + header('Pragma: public'); + header('Cache-Control: public, max-age=31536000, immutable'); + header('Expires: ' . gmdate('D, d M Y H:i:s', strtotime('+1 year')) . ' GMT'); // Never expire // If width is null or 0, output original image if ($width!=(int)$width || $width === null || $width <= 0 || $width>1500) { // Return original Image if huger 1500 or smaller 100 or crap diff --git a/application/views/eqslcard/index.php b/application/views/eqslcard/index.php index ddbf21ba8..609d82cb6 100644 --- a/application/views/eqslcard/index.php +++ b/application/views/eqslcard/index.php @@ -61,7 +61,7 @@ echo ''; if ($qsl->COL_EQSL_QSLRDATE) { $timestamp = strtotime($qsl->COL_EQSL_QSLRDATE); echo date($custom_date_format, $timestamp); } echo ''; - echo 'COL_PRIMARY_KEY).'\' data-fancybox=\'images\' data-width=\'528\' data-height=\'336\' class=\'btn btn-sm btn-success\'>' . __("View") . 'COL_PRIMARY_KEY).'/160\' height="100px">'; + echo 'COL_PRIMARY_KEY).'\' data-fancybox=\'images\' data-width=\'528\' data-height=\'336\' class=\'btn btn-sm btn-success\'>' . __("View") . 'COL_PRIMARY_KEY).'/160\' height="100px">'; echo ''; }