summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBharat Mediratta <bharat@menalto.com>2010-11-09 12:48:07 -0800
committerBharat Mediratta <bharat@menalto.com>2010-11-09 12:48:07 -0800
commite87c502eded374e927c531ad10a78dac8ee70e64 (patch)
tree5a4c90381fd8a915e025cfe522f85dfc98e6a2ae
parentf0781a9162129394236ef9c8cd68392d59b52791 (diff)
Tie the image cachebuster to the file mtime instead of
Item_Model::$updated since we want the url to be stable whenever possible. Fixes #1482.
-rw-r--r--modules/gallery/models/item.php13
-rw-r--r--modules/gallery/tests/Item_Model_Test.php23
2 files changed, 32 insertions, 4 deletions
diff --git a/modules/gallery/models/item.php b/modules/gallery/models/item.php
index eaf09c9c..891153d1 100644
--- a/modules/gallery/models/item.php
+++ b/modules/gallery/models/item.php
@@ -166,8 +166,9 @@ class Item_Model_Core extends ORM_MPTT {
*/
public function file_url($full_uri=false) {
$relative_path = "var/albums/" . $this->relative_path();
+ $cache_buster = $this->_cache_buster($this->file_path());
return ($full_uri ? url::abs_file($relative_path) : url::file($relative_path))
- . "?m={$this->updated}";
+ . $cache_buster;
}
/**
@@ -198,7 +199,7 @@ class Item_Model_Core extends ORM_MPTT {
* photo: http://example.com/gallery3/var/albums/album1/photo.thumb.jpg
*/
public function thumb_url($full_uri=false) {
- $cache_buster = "?m={$this->updated}";
+ $cache_buster = $this->_cache_buster($this->thumb_path());
$relative_path = "var/thumbs/" . $this->relative_path();
$base = ($full_uri ? url::abs_file($relative_path) : url::file($relative_path));
if ($this->is_photo()) {
@@ -227,9 +228,9 @@ class Item_Model_Core extends ORM_MPTT {
*/
public function resize_url($full_uri=false) {
$relative_path = "var/resizes/" . $this->relative_path();
+ $cache_buster = $this->_cache_buster($this->resize_path());
return ($full_uri ? url::abs_file($relative_path) : url::file($relative_path)) .
- ($this->is_album() ? "/.album.jpg" : "")
- . "?m={$this->updated}";
+ ($this->is_album() ? "/.album.jpg" : "") . $cache_buster;
}
/**
@@ -1024,4 +1025,8 @@ class Item_Model_Core extends ORM_MPTT {
}
return $data;
}
+
+ private function _cache_buster($path) {
+ return "?m=" . (string)(file_exists($path) ? filemtime($path) : 0);
+ }
}
diff --git a/modules/gallery/tests/Item_Model_Test.php b/modules/gallery/tests/Item_Model_Test.php
index 90c54e3c..264a2128 100644
--- a/modules/gallery/tests/Item_Model_Test.php
+++ b/modules/gallery/tests/Item_Model_Test.php
@@ -406,6 +406,29 @@ class Item_Model_Test extends Gallery_Unit_Test_Case {
return; // pass
}
$this->assert_true(false, "Shouldn't get here");
+ }
+
+ public function urls_test() {
+ $photo = test::random_photo();
+ $this->assert_true(
+ preg_match("|http://./var/resizes/name_\d+\.jpg\?m=\d+|", $photo->resize_url()),
+ $photo->resize_url() . " is malformed");
+ $this->assert_true(
+ preg_match("|http://./var/thumbs/name_\d+\.jpg\?m=\d+|", $photo->thumb_url()),
+ $photo->thumb_url() . " is malformed");
+ $this->assert_true(
+ preg_match("|http://./var/albums/name_\d+\.jpg\?m=\d+|", $photo->file_url()),
+ $photo->file_url() . " is malformed");
+ // Albums have special thumbnails. Empty album has cachebuster of 0 since it has no thumbnail
+ $album = test::random_album();
+ $this->assert_true(
+ preg_match("|http://./var/thumbs/name_\d+/\.album\.jpg\?m=0|", $album->thumb_url()),
+ $album->thumb_url() . " is malformed");
+
+ $photo = test::random_photo($album);
+ $this->assert_true(
+ preg_match("|http://./var/thumbs/name_\d+/\.album\.jpg\?m=\d+|", $album->thumb_url()),
+ $album->thumb_url() . " is malformed");
}
}