From f01fad1cd0661a7fee42d701b0c17dd8f4def576 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sun, 31 Oct 2010 12:21:06 -0700 Subject: Deal in integers when doing aspect ratio operations. Fixes #1470. --- modules/gallery/models/item.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'modules/gallery/models/item.php') diff --git a/modules/gallery/models/item.php b/modules/gallery/models/item.php index 07f781d1..d16a5dc4 100644 --- a/modules/gallery/models/item.php +++ b/modules/gallery/models/item.php @@ -628,7 +628,7 @@ class Item_Model_Core extends ORM_MPTT { list ($height, $width) = $this->scale_dimensions($max); if ($center_vertically && $max) { // The constant is divide by 2 to calculate the file and 10 to convert to em - $margin_top = ($max - $height) / 20; + $margin_top = (int)(($max - $height) / 20); $extra_attrs["style"] = "margin-top: {$margin_top}em"; $extra_attrs["title"] = $this->title; } @@ -656,10 +656,10 @@ class Item_Model_Core extends ORM_MPTT { if ($height) { if (isset($max)) { if ($width > $height) { - $height = (int)($max * ($height / $width)); + $height = (int)($max * $height / $width); $width = $max; } else { - $width = (int)($max * ($width / $height)); + $width = (int)($max * $width / $height); $height = $max; } } @@ -700,10 +700,10 @@ class Item_Model_Core extends ORM_MPTT { $height = $this->height; if ($width > $max_size || $height > $max_size) { if ($width > $height) { - $height *= $max_size / $width; + $height = (int)($height * $max_size / $width); $width = $max_size; } else { - $width *= $max_size / $height; + $width = (int)($width * $max_size / $height); $height = $max_size; } } -- cgit v1.2.3 From d503513eca13c9c212d2772e9791744725127a8e Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sat, 6 Nov 2010 21:54:28 -0700 Subject: Create item_before_create and item_before_update events, and make Item_Model::data_file public. This allows us to intercept and tweak data files before saving the model which enables the max_size module. --- modules/gallery/models/item.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'modules/gallery/models/item.php') diff --git a/modules/gallery/models/item.php b/modules/gallery/models/item.php index d16a5dc4..eaf09c9c 100644 --- a/modules/gallery/models/item.php +++ b/modules/gallery/models/item.php @@ -20,7 +20,7 @@ class Item_Model_Core extends ORM_MPTT { protected $children = "items"; protected $sorting = array(); - protected $data_file = null; + public $data_file = null; public function __construct($id=null) { parent::__construct($id); @@ -320,6 +320,7 @@ class Item_Model_Core extends ORM_MPTT { $this->updated = time(); if (!$this->loaded()) { // Create a new item. + module::event("item_before_create", $this); // Set a weight if it's missing. We don't do this in the constructor because it's not a // simple assignment. @@ -398,6 +399,7 @@ class Item_Model_Core extends ORM_MPTT { module::event("item_created", $this); } else { // Update an existing item + module::event("item_before_update", $item); // If any significant fields have changed, load up a copy of the original item and // keep it around. -- cgit v1.2.3 From e87c502eded374e927c531ad10a78dac8ee70e64 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Tue, 9 Nov 2010 12:48:07 -0800 Subject: 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. --- modules/gallery/models/item.php | 13 +++++++++---- modules/gallery/tests/Item_Model_Test.php | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) (limited to 'modules/gallery/models/item.php') 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"); } } -- cgit v1.2.3 From 8d030cea64c0e5934e4dbf5fc56fab87c7e253bb Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Thu, 18 Nov 2010 09:52:40 -0800 Subject: Fix a bug where simultaneous deletes of the same item can result in it deleting the first item row in the database. The root issue is a bug in Kohana that's addressed in dca9b5f3fc8e80ee0667cac88d688e2287b1e7f4 but in this change we deal with the fact that reloading an item can result in an instance of the item that's unloaded. In those cases, we should just ignore it and move on. Fixes #1489. --- modules/gallery/libraries/ORM_MPTT.php | 8 ++++++-- modules/gallery/models/item.php | 5 +++++ 2 files changed, 11 insertions(+), 2 deletions(-) (limited to 'modules/gallery/models/item.php') diff --git a/modules/gallery/libraries/ORM_MPTT.php b/modules/gallery/libraries/ORM_MPTT.php index d8d88e4e..f20fafa0 100644 --- a/modules/gallery/libraries/ORM_MPTT.php +++ b/modules/gallery/libraries/ORM_MPTT.php @@ -96,12 +96,16 @@ class ORM_MPTT_Core extends ORM { $item->reload()->delete(); } - // Deleting children has affected this item - $this->reload(); + // Deleting children has affected this item, but we'll reload it below. } $this->lock(); $this->reload(); // Assume that the prior lock holder may have changed this entry + if (!$this->loaded()) { + // Concurrent deletes may result in this item already being gone. Ignore it. + return; + } + try { db::build() ->update($this->table_name) diff --git a/modules/gallery/models/item.php b/modules/gallery/models/item.php index 891153d1..9016a04a 100644 --- a/modules/gallery/models/item.php +++ b/modules/gallery/models/item.php @@ -71,6 +71,11 @@ class Item_Model_Core extends ORM_MPTT { } public function delete($ignored_id=null) { + if (!$this->loaded()) { + // Concurrent deletes may result in this item already being gone. Ignore it. + return; + } + if ($this->id == 1) { $v = new Validation(array("id")); $v->add_error("id", "cant_delete_root_album"); -- cgit v1.2.3