diff --git a/api/src/Storage/Merge.php b/api/src/Storage/Merge.php index feb196491e..6f84e40100 100644 --- a/api/src/Storage/Merge.php +++ b/api/src/Storage/Merge.php @@ -2440,17 +2440,9 @@ abstract class Merge $target = $_target = (Vfs::is_writable(Vfs::get_home_dir()) ? Vfs::get_home_dir() : "/home/{$GLOBALS['egw_info']['user']['account_lid']}" - )."/$filename"; - $dupe_count = 0; - while(is_file(Vfs::PREFIX.$target)) - { - $dupe_count++; - $target = Vfs::dirname($_target) . '/' . - pathinfo($filename, PATHINFO_FILENAME) . - ' ('.($dupe_count + 1).')' . '.' . - pathinfo($filename, PATHINFO_EXTENSION); - } - copy($result, Vfs::PREFIX.$target); + ) . "/$filename"; + $target = Vfs::make_unique($target); + copy($result, Vfs::PREFIX . $target); unlink($result); // Find out what to do with it diff --git a/api/src/Vfs.php b/api/src/Vfs.php index 3337ba424c..89da3df8aa 100644 --- a/api/src/Vfs.php +++ b/api/src/Vfs.php @@ -2386,6 +2386,27 @@ class Vfs extends Vfs\Base } return self::_call_on_backend('get_minimum_file_id', array($path)); } + + /** + * Make sure the path is unique, by appending (#) to the filename if it already exists + * + * @param string $path + * + * @return string The same path, but modified if it exists + */ + static function make_unique($path) + { + $dupe_count = 0; + while(is_file(Vfs::PREFIX . $path)) + { + $dupe_count++; + $path = Vfs::dirname($path) . '/' . + pathinfo($path, PATHINFO_FILENAME) . + ' (' . ($dupe_count + 1) . ')' . '.' . + pathinfo($path, PATHINFO_EXTENSION); + } + return $path; + } } Vfs::init_static();