diff options
Diffstat (limited to 'core/helpers/photo.php')
-rw-r--r-- | core/helpers/photo.php | 28 |
1 files changed, 18 insertions, 10 deletions
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()); |