Skip to content

Commit

Permalink
Cache images loaded from the route (when cache is enabled) (#905)
Browse files Browse the repository at this point in the history
* Cache images loaded from the route (when cache is enabled)

* Use GMT instead of +0000 as used in DATE_RFC1123 format
  • Loading branch information
flaviocopes authored and rhukster committed Jul 11, 2016
1 parent 8e7cc01 commit 29f6da6
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions system/src/Grav/Common/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,25 @@ public static function download($file, $force_download = true, $sec = 0, $bytes
} else {
$new_length = $size;
header("Content-Length: " . $size);

if (Grav::instance()['config']->get('system.cache.enabled')) {
$expires = Grav::instance()['config']->get('system.pages.expires');
if ($expires > 0) {
$expires_date = gmdate('D, d M Y H:i:s T', time() + $expires);
header('Cache-Control: max-age=' . $expires);
header('Expires: ' . $expires_date);
header('Pragma: cache');
}
header('Last-Modified: ' . gmdate("D, d M Y H:i:s T", filemtime($file)));

// Return 304 Not Modified if the file is already cached in the browser
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= filemtime($file))
{
header('HTTP/1.1 304 Not Modified');
exit();
}
}
}

/* output the file itself */
Expand Down

0 comments on commit 29f6da6

Please sign in to comment.