From ded8a43bc3c2171743f33b9c34d1dec4e02217d8 Mon Sep 17 00:00:00 2001 From: Mikael Roos Date: Mon, 16 Mar 2015 15:36:06 +0100 Subject: [PATCH] Naming cache-file using md5 for remote images, fix #86 --- CImage.php | 2 +- CRemoteImage.php | 80 +++++++++++----------------------------------- REVISION.md | 1 + webroot/imgd.php | 82 +++++++++++------------------------------------- webroot/imgp.php | 82 +++++++++++------------------------------------- webroot/imgs.php | 82 +++++++++++------------------------------------- 6 files changed, 77 insertions(+), 252 deletions(-) diff --git a/CImage.php b/CImage.php index a776edb..2397229 100644 --- a/CImage.php +++ b/CImage.php @@ -537,7 +537,7 @@ class CImage $src = $remote->download($src); $this->log("Remote HTTP status: " . $remote->getStatus()); - $this->log("Remote item has local cached file: $src"); + $this->log("Remote item is in local cache: $src"); $this->log("Remote details on cache:" . print_r($remote->getDetails(), true)); return $src; diff --git a/CRemoteImage.php b/CRemoteImage.php index 408a0b8..a442a82 100644 --- a/CRemoteImage.php +++ b/CRemoteImage.php @@ -48,7 +48,7 @@ class CRemoteImage /** - * Base name of cache file for downloaded item. + * Base name of cache file for downloaded item and name of image. */ private $fileName; @@ -61,13 +61,6 @@ class CRemoteImage - /** - * Filename for image-file. - */ - private $fileImage; - - - /** * Cache details loaded from file. */ @@ -75,16 +68,6 @@ class CRemoteImage - /** - * Constructor - * - */ - public function __construct() - { - ; - } - - /** * Get status of last HTTP request. * @@ -157,34 +140,13 @@ class CRemoteImage - /** - * Translate a content type to a file extension. - * - * @param string $type a valid content type. - * - * @return string as file extension or false if no match. - */ - function contentTypeToFileExtension($type) { - $extension = array( - 'image/jpeg' => 'jpg', - 'image/png' => 'png', - 'image/gif' => 'gif', - ); - - return isset($extension[$type]) - ? $extension[$type] - : false; - } - - - /** * Set header fields. * * @return $this */ function setHeaderFields() { - $this->http->setHeader("User-Agent", "CImage/0.6 (PHP/". phpversion() . " cURL)"); + $this->http->setHeader("User-Agent", "CImage/0.7.0 (PHP/". phpversion() . " cURL)"); $this->http->setHeader("Accept", "image/jpeg,image/png,image/gif"); if ($this->useCache) { @@ -209,30 +171,24 @@ class CRemoteImage $maxAge = $this->http->getMaxAge($this->defaultMaxAge); $lastModified = $this->http->getLastModified(); $type = $this->http->getContentType(); - $extension = $this->contentTypeToFileExtension($type); $this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date); $this->cache['Max-Age'] = $maxAge; $this->cache['Content-Type'] = $type; - $this->cache['File-Extension'] = $extension; + $this->cache['Url'] = $this->url; if ($lastModified) { $this->cache['Last-Modified'] = gmdate("D, d M Y H:i:s T", $lastModified); } - if ($extension) { + // Save only if body is a valid image + $body = $this->http->getBody(); + $img = imagecreatefromstring($body); - $this->fileImage = $this->fileName . "." . $extension; - - // Save only if body is a valid image - $body = $this->http->getBody(); - $img = imagecreatefromstring($body); - - if ($img !== false) { - file_put_contents($this->fileImage, $body); - file_put_contents($this->fileJson, json_encode($this->cache)); - return $this->fileImage; - } + if ($img !== false) { + file_put_contents($this->fileName, $body); + file_put_contents($this->fileJson, json_encode($this->cache)); + return $this->fileName; } return false; @@ -251,15 +207,15 @@ class CRemoteImage $maxAge = $this->http->getMaxAge($this->defaultMaxAge); $lastModified = $this->http->getLastModified(); - $this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date); - $this->cache['Max-Age'] = $maxAge; + $this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date); + $this->cache['Max-Age'] = $maxAge; if ($lastModified) { $this->cache['Last-Modified'] = gmdate("D, d M Y H:i:s T", $lastModified); } file_put_contents($this->fileJson, json_encode($this->cache)); - return $this->fileImage; + return $this->fileName; } @@ -313,7 +269,7 @@ class CRemoteImage */ public function loadCacheDetails() { - $cacheFile = str_replace(array("/", ":", "#", ".", "?"), "-", $this->url); + $cacheFile = md5($this->url); $this->fileName = $this->saveFolder . $cacheFile; $this->fileJson = $this->fileName . ".json"; if (is_readable($this->fileJson)) { @@ -330,15 +286,15 @@ class CRemoteImage */ public function getCachedSource() { - $this->fileImage = $this->fileName . "." . $this->cache['File-Extension']; - $imageExists = is_readable($this->fileImage); + $imageExists = is_readable($this->fileName); // Is cache valid? $date = strtotime($this->cache['Date']); $maxAge = $this->cache['Max-Age']; - $now = time(); + $now = time(); + if ($imageExists && $date + $maxAge > $now) { - return $this->fileImage; + return $this->fileName; } // Prepare for a 304 if available diff --git a/REVISION.md b/REVISION.md index 6ee65c2..954daa7 100644 --- a/REVISION.md +++ b/REVISION.md @@ -5,6 +5,7 @@ Revision history v0.7.0.x (latest) ------------------------------------- +* Naming cache-file using md5 for remote images, fix #86. * Loading images without depending on filename extension, fix #85. * Adding unittest with phpunit #84, fix #13 * Adding support for whitelist of remote hostnames, #84 diff --git a/webroot/imgd.php b/webroot/imgd.php index a555bb2..6c302c0 100644 --- a/webroot/imgd.php +++ b/webroot/imgd.php @@ -327,7 +327,7 @@ class CRemoteImage /** - * Base name of cache file for downloaded item. + * Base name of cache file for downloaded item and name of image. */ private $fileName; @@ -340,13 +340,6 @@ class CRemoteImage - /** - * Filename for image-file. - */ - private $fileImage; - - - /** * Cache details loaded from file. */ @@ -354,16 +347,6 @@ class CRemoteImage - /** - * Constructor - * - */ - public function __construct() - { - ; - } - - /** * Get status of last HTTP request. * @@ -436,34 +419,13 @@ class CRemoteImage - /** - * Translate a content type to a file extension. - * - * @param string $type a valid content type. - * - * @return string as file extension or false if no match. - */ - function contentTypeToFileExtension($type) { - $extension = array( - 'image/jpeg' => 'jpg', - 'image/png' => 'png', - 'image/gif' => 'gif', - ); - - return isset($extension[$type]) - ? $extension[$type] - : false; - } - - - /** * Set header fields. * * @return $this */ function setHeaderFields() { - $this->http->setHeader("User-Agent", "CImage/0.6 (PHP/". phpversion() . " cURL)"); + $this->http->setHeader("User-Agent", "CImage/0.7.0 (PHP/". phpversion() . " cURL)"); $this->http->setHeader("Accept", "image/jpeg,image/png,image/gif"); if ($this->useCache) { @@ -488,30 +450,24 @@ class CRemoteImage $maxAge = $this->http->getMaxAge($this->defaultMaxAge); $lastModified = $this->http->getLastModified(); $type = $this->http->getContentType(); - $extension = $this->contentTypeToFileExtension($type); $this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date); $this->cache['Max-Age'] = $maxAge; $this->cache['Content-Type'] = $type; - $this->cache['File-Extension'] = $extension; + $this->cache['Url'] = $this->url; if ($lastModified) { $this->cache['Last-Modified'] = gmdate("D, d M Y H:i:s T", $lastModified); } - if ($extension) { + // Save only if body is a valid image + $body = $this->http->getBody(); + $img = imagecreatefromstring($body); - $this->fileImage = $this->fileName . "." . $extension; - - // Save only if body is a valid image - $body = $this->http->getBody(); - $img = imagecreatefromstring($body); - - if ($img !== false) { - file_put_contents($this->fileImage, $body); - file_put_contents($this->fileJson, json_encode($this->cache)); - return $this->fileImage; - } + if ($img !== false) { + file_put_contents($this->fileName, $body); + file_put_contents($this->fileJson, json_encode($this->cache)); + return $this->fileName; } return false; @@ -530,15 +486,15 @@ class CRemoteImage $maxAge = $this->http->getMaxAge($this->defaultMaxAge); $lastModified = $this->http->getLastModified(); - $this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date); - $this->cache['Max-Age'] = $maxAge; + $this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date); + $this->cache['Max-Age'] = $maxAge; if ($lastModified) { $this->cache['Last-Modified'] = gmdate("D, d M Y H:i:s T", $lastModified); } file_put_contents($this->fileJson, json_encode($this->cache)); - return $this->fileImage; + return $this->fileName; } @@ -592,7 +548,7 @@ class CRemoteImage */ public function loadCacheDetails() { - $cacheFile = str_replace(array("/", ":", "#", ".", "?"), "-", $this->url); + $cacheFile = md5($this->url); $this->fileName = $this->saveFolder . $cacheFile; $this->fileJson = $this->fileName . ".json"; if (is_readable($this->fileJson)) { @@ -609,15 +565,15 @@ class CRemoteImage */ public function getCachedSource() { - $this->fileImage = $this->fileName . "." . $this->cache['File-Extension']; - $imageExists = is_readable($this->fileImage); + $imageExists = is_readable($this->fileName); // Is cache valid? $date = strtotime($this->cache['Date']); $maxAge = $this->cache['Max-Age']; - $now = time(); + $now = time(); + if ($imageExists && $date + $maxAge > $now) { - return $this->fileImage; + return $this->fileName; } // Prepare for a 304 if available @@ -1169,7 +1125,7 @@ class CImage $src = $remote->download($src); $this->log("Remote HTTP status: " . $remote->getStatus()); - $this->log("Remote item has local cached file: $src"); + $this->log("Remote item is in local cache: $src"); $this->log("Remote details on cache:" . print_r($remote->getDetails(), true)); return $src; diff --git a/webroot/imgp.php b/webroot/imgp.php index 4e0c9cd..6591687 100644 --- a/webroot/imgp.php +++ b/webroot/imgp.php @@ -327,7 +327,7 @@ class CRemoteImage /** - * Base name of cache file for downloaded item. + * Base name of cache file for downloaded item and name of image. */ private $fileName; @@ -340,13 +340,6 @@ class CRemoteImage - /** - * Filename for image-file. - */ - private $fileImage; - - - /** * Cache details loaded from file. */ @@ -354,16 +347,6 @@ class CRemoteImage - /** - * Constructor - * - */ - public function __construct() - { - ; - } - - /** * Get status of last HTTP request. * @@ -436,34 +419,13 @@ class CRemoteImage - /** - * Translate a content type to a file extension. - * - * @param string $type a valid content type. - * - * @return string as file extension or false if no match. - */ - function contentTypeToFileExtension($type) { - $extension = array( - 'image/jpeg' => 'jpg', - 'image/png' => 'png', - 'image/gif' => 'gif', - ); - - return isset($extension[$type]) - ? $extension[$type] - : false; - } - - - /** * Set header fields. * * @return $this */ function setHeaderFields() { - $this->http->setHeader("User-Agent", "CImage/0.6 (PHP/". phpversion() . " cURL)"); + $this->http->setHeader("User-Agent", "CImage/0.7.0 (PHP/". phpversion() . " cURL)"); $this->http->setHeader("Accept", "image/jpeg,image/png,image/gif"); if ($this->useCache) { @@ -488,30 +450,24 @@ class CRemoteImage $maxAge = $this->http->getMaxAge($this->defaultMaxAge); $lastModified = $this->http->getLastModified(); $type = $this->http->getContentType(); - $extension = $this->contentTypeToFileExtension($type); $this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date); $this->cache['Max-Age'] = $maxAge; $this->cache['Content-Type'] = $type; - $this->cache['File-Extension'] = $extension; + $this->cache['Url'] = $this->url; if ($lastModified) { $this->cache['Last-Modified'] = gmdate("D, d M Y H:i:s T", $lastModified); } - if ($extension) { + // Save only if body is a valid image + $body = $this->http->getBody(); + $img = imagecreatefromstring($body); - $this->fileImage = $this->fileName . "." . $extension; - - // Save only if body is a valid image - $body = $this->http->getBody(); - $img = imagecreatefromstring($body); - - if ($img !== false) { - file_put_contents($this->fileImage, $body); - file_put_contents($this->fileJson, json_encode($this->cache)); - return $this->fileImage; - } + if ($img !== false) { + file_put_contents($this->fileName, $body); + file_put_contents($this->fileJson, json_encode($this->cache)); + return $this->fileName; } return false; @@ -530,15 +486,15 @@ class CRemoteImage $maxAge = $this->http->getMaxAge($this->defaultMaxAge); $lastModified = $this->http->getLastModified(); - $this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date); - $this->cache['Max-Age'] = $maxAge; + $this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date); + $this->cache['Max-Age'] = $maxAge; if ($lastModified) { $this->cache['Last-Modified'] = gmdate("D, d M Y H:i:s T", $lastModified); } file_put_contents($this->fileJson, json_encode($this->cache)); - return $this->fileImage; + return $this->fileName; } @@ -592,7 +548,7 @@ class CRemoteImage */ public function loadCacheDetails() { - $cacheFile = str_replace(array("/", ":", "#", ".", "?"), "-", $this->url); + $cacheFile = md5($this->url); $this->fileName = $this->saveFolder . $cacheFile; $this->fileJson = $this->fileName . ".json"; if (is_readable($this->fileJson)) { @@ -609,15 +565,15 @@ class CRemoteImage */ public function getCachedSource() { - $this->fileImage = $this->fileName . "." . $this->cache['File-Extension']; - $imageExists = is_readable($this->fileImage); + $imageExists = is_readable($this->fileName); // Is cache valid? $date = strtotime($this->cache['Date']); $maxAge = $this->cache['Max-Age']; - $now = time(); + $now = time(); + if ($imageExists && $date + $maxAge > $now) { - return $this->fileImage; + return $this->fileName; } // Prepare for a 304 if available @@ -1169,7 +1125,7 @@ class CImage $src = $remote->download($src); $this->log("Remote HTTP status: " . $remote->getStatus()); - $this->log("Remote item has local cached file: $src"); + $this->log("Remote item is in local cache: $src"); $this->log("Remote details on cache:" . print_r($remote->getDetails(), true)); return $src; diff --git a/webroot/imgs.php b/webroot/imgs.php index a555bb2..6c302c0 100644 --- a/webroot/imgs.php +++ b/webroot/imgs.php @@ -327,7 +327,7 @@ class CRemoteImage /** - * Base name of cache file for downloaded item. + * Base name of cache file for downloaded item and name of image. */ private $fileName; @@ -340,13 +340,6 @@ class CRemoteImage - /** - * Filename for image-file. - */ - private $fileImage; - - - /** * Cache details loaded from file. */ @@ -354,16 +347,6 @@ class CRemoteImage - /** - * Constructor - * - */ - public function __construct() - { - ; - } - - /** * Get status of last HTTP request. * @@ -436,34 +419,13 @@ class CRemoteImage - /** - * Translate a content type to a file extension. - * - * @param string $type a valid content type. - * - * @return string as file extension or false if no match. - */ - function contentTypeToFileExtension($type) { - $extension = array( - 'image/jpeg' => 'jpg', - 'image/png' => 'png', - 'image/gif' => 'gif', - ); - - return isset($extension[$type]) - ? $extension[$type] - : false; - } - - - /** * Set header fields. * * @return $this */ function setHeaderFields() { - $this->http->setHeader("User-Agent", "CImage/0.6 (PHP/". phpversion() . " cURL)"); + $this->http->setHeader("User-Agent", "CImage/0.7.0 (PHP/". phpversion() . " cURL)"); $this->http->setHeader("Accept", "image/jpeg,image/png,image/gif"); if ($this->useCache) { @@ -488,30 +450,24 @@ class CRemoteImage $maxAge = $this->http->getMaxAge($this->defaultMaxAge); $lastModified = $this->http->getLastModified(); $type = $this->http->getContentType(); - $extension = $this->contentTypeToFileExtension($type); $this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date); $this->cache['Max-Age'] = $maxAge; $this->cache['Content-Type'] = $type; - $this->cache['File-Extension'] = $extension; + $this->cache['Url'] = $this->url; if ($lastModified) { $this->cache['Last-Modified'] = gmdate("D, d M Y H:i:s T", $lastModified); } - if ($extension) { + // Save only if body is a valid image + $body = $this->http->getBody(); + $img = imagecreatefromstring($body); - $this->fileImage = $this->fileName . "." . $extension; - - // Save only if body is a valid image - $body = $this->http->getBody(); - $img = imagecreatefromstring($body); - - if ($img !== false) { - file_put_contents($this->fileImage, $body); - file_put_contents($this->fileJson, json_encode($this->cache)); - return $this->fileImage; - } + if ($img !== false) { + file_put_contents($this->fileName, $body); + file_put_contents($this->fileJson, json_encode($this->cache)); + return $this->fileName; } return false; @@ -530,15 +486,15 @@ class CRemoteImage $maxAge = $this->http->getMaxAge($this->defaultMaxAge); $lastModified = $this->http->getLastModified(); - $this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date); - $this->cache['Max-Age'] = $maxAge; + $this->cache['Date'] = gmdate("D, d M Y H:i:s T", $date); + $this->cache['Max-Age'] = $maxAge; if ($lastModified) { $this->cache['Last-Modified'] = gmdate("D, d M Y H:i:s T", $lastModified); } file_put_contents($this->fileJson, json_encode($this->cache)); - return $this->fileImage; + return $this->fileName; } @@ -592,7 +548,7 @@ class CRemoteImage */ public function loadCacheDetails() { - $cacheFile = str_replace(array("/", ":", "#", ".", "?"), "-", $this->url); + $cacheFile = md5($this->url); $this->fileName = $this->saveFolder . $cacheFile; $this->fileJson = $this->fileName . ".json"; if (is_readable($this->fileJson)) { @@ -609,15 +565,15 @@ class CRemoteImage */ public function getCachedSource() { - $this->fileImage = $this->fileName . "." . $this->cache['File-Extension']; - $imageExists = is_readable($this->fileImage); + $imageExists = is_readable($this->fileName); // Is cache valid? $date = strtotime($this->cache['Date']); $maxAge = $this->cache['Max-Age']; - $now = time(); + $now = time(); + if ($imageExists && $date + $maxAge > $now) { - return $this->fileImage; + return $this->fileName; } // Prepare for a 304 if available @@ -1169,7 +1125,7 @@ class CImage $src = $remote->download($src); $this->log("Remote HTTP status: " . $remote->getStatus()); - $this->log("Remote item has local cached file: $src"); + $this->log("Remote item is in local cache: $src"); $this->log("Remote details on cache:" . print_r($remote->getDetails(), true)); return $src;