diff options
author | Tim Almdal <tnalmdal@shaw.ca> | 2009-02-07 18:12:06 +0000 |
---|---|---|
committer | Tim Almdal <tnalmdal@shaw.ca> | 2009-02-07 18:12:06 +0000 |
commit | 90277704a2d2a8fd8d7f3191024ec901ad317f22 (patch) | |
tree | 0ce4e300f89b34a490964008b25f70c7d8340e5c | |
parent | 69e0d72b930ec4326a257c41f365256d9913552d (diff) |
Changed the way album and photo creation guards against duplicate
names.
Added similiar code to insure that path names are not duplicated.
-rw-r--r-- | core/helpers/album.php | 24 | ||||
-rw-r--r-- | core/helpers/photo.php | 28 | ||||
-rw-r--r-- | core/tests/Album_Helper_Test.php | 1 | ||||
-rw-r--r-- | core/tests/Photo_Helper_Test.php | 12 |
4 files changed, 48 insertions, 17 deletions
diff --git a/core/helpers/album.php b/core/helpers/album.php index 21ad3810..b19a003d 100644 --- a/core/helpers/album.php +++ b/core/helpers/album.php @@ -39,22 +39,32 @@ class album_Core { throw new Exception("@todo INVALID_PARENT"); } + // Randomize the name if there's a conflict + $name_count = ORM::factory("item") + ->where("parent_id", $parent->id) + ->where("name", $name) + ->count_all(); + $name = $name_count == 0 ? $name : sprintf("%s_%03d", $name, $name_count); + + $path = !empty($path) ? $path : preg_replace("/[^A-Za-z0-9\.\-_]/", "_", $name); + + // Randomize the path if there's a conflict + $path_count = ORM::factory("item") + ->where("parent_id", $parent->id) + ->where("path", $path) + ->count_all(); + $path = $path_count == 0 ? $path : sprintf("%s_%03d", $path, $path_count); + $album = ORM::factory("item"); $album->type = "album"; $album->title = $title; $album->description = $description; $album->name = $name; - $album->path = !empty($path) ? $path : preg_replace("/[^A-Za-z0-9\.\-_]/", "_", $name); + $album->path = $path; $album->owner_id = $owner_id; $album->thumb_dirty = 1; $album->resize_dirty = 1; - while (ORM::factory("item") - ->where("parent_id", $parent->id) - ->where("name", $album->name) - ->find()->id) { - $album->name = "{$name}-" . rand(); - } $album = $album->add_to_parent($parent); mkdir($album->file_path()); diff --git a/core/helpers/photo.php b/core/helpers/photo.php index ab782da9..1b087bd1 100644 --- a/core/helpers/photo.php +++ b/core/helpers/photo.php @@ -56,12 +56,29 @@ class photo_Core { $name .= "." . $pi["extension"]; } + // Randomize the name if there's a conflict + $name_count = ORM::factory("item") + ->where("parent_id", $parent->id) + ->where("name", $name) + ->count_all(); + $name = $name_count == 0 ? $name : + sprintf("%s_%03d.%s", $pi["filename"], $name_count, $pi["extension"]); + + $path = !empty($path) ? $path : preg_replace("/[^A-Za-z0-9\.\-_]/", "_", $name); + + // Randomize the path if there's a conflict + $path_count = ORM::factory("item") + ->where("parent_id", $parent->id) + ->where("path", $path) + ->count_all(); + $path = $path_count == 0 ? $path : sprintf("%s_%03d", $path, $path_count); + $photo = ORM::factory("item"); $photo->type = "photo"; $photo->title = $title; $photo->description = $description; $photo->name = $name; - $photo->path = !empty($path) ? $path : preg_replace("/[^A-Za-z0-9\.\-_]/", "_", $name); + $photo->path = $path; $photo->owner_id = $owner_id; $photo->width = $image_info[0]; $photo->height = $image_info[1]; @@ -69,15 +86,6 @@ class photo_Core { $photo->thumb_dirty = 1; $photo->resize_dirty = 1; - // Randomize the name if there's a conflict - while (ORM::Factory("item") - ->where("parent_id", $parent->id) - ->where("name", $photo->name) - ->find()->id) { - // @todo Improve this. Random numbers are not user friendly - $photo->name = rand() . "." . $pi["extension"]; - } - // This saves the photo $photo->add_to_parent($parent); copy($filename, $photo->file_path()); diff --git a/core/tests/Album_Helper_Test.php b/core/tests/Album_Helper_Test.php index d085ca88..75981265 100644 --- a/core/tests/Album_Helper_Test.php +++ b/core/tests/Album_Helper_Test.php @@ -43,6 +43,7 @@ class Album_Helper_Test extends Unit_Test_Case { $album1 = album::create($root, $rand, $rand, $rand); $album2 = album::create($root, $rand, $rand, $rand); $this->assert_true($album1->name != $album2->name); + $this->assert_true($album1->path != $album2->path); } public function thumb_url_test() { diff --git a/core/tests/Photo_Helper_Test.php b/core/tests/Photo_Helper_Test.php index 2219a846..5db63e49 100644 --- a/core/tests/Photo_Helper_Test.php +++ b/core/tests/Photo_Helper_Test.php @@ -53,6 +53,18 @@ class Photo_Helper_Test extends Unit_Test_Case { $photo1 = photo::create($root, DOCROOT . "core/tests/test.jpg", "$rand.jpg", $rand, $rand); $photo2 = photo::create($root, DOCROOT . "core/tests/test.jpg", "$rand.jpg", $rand, $rand); $this->assert_true($photo1->name != $photo2->name); + $this->assert_true($photo1->path != $photo2->path); + } + + public function create_special_characters_in_name_test() { + $rand = rand(); + $rand2 = rand(); + $name = "$rand & $rand's.jpg"; + $path = !empty($path) ? $path : preg_replace("/[^A-Za-z0-9\.\-_]/", "_", $name); + $root = ORM::factory("item", 1); + $photo = photo::create($root, DOCROOT . "core/tests/test.jpg", $name, $rand, $rand); + $this->assert_equal($name, $photo->name); + $this->assert_equal($path, $photo->path); } public function create_photo_with_no_extension_test() { |