diff options
author | Bart Visscher <bartv@thisnet.nl> | 2013-07-16 12:06:53 -0700 |
---|---|---|
committer | Bart Visscher <bartv@thisnet.nl> | 2013-07-16 12:06:53 -0700 |
commit | 285f288cf344625dbddd3c6e4bacb9df1ddb6ce0 (patch) | |
tree | d433f1c2b6cdc3e48fc1fe538dc9e6d858a2c26a /lib | |
parent | d673d8c25310f0ea73927b8b252e6823a11087f4 (diff) | |
parent | 22c29eb64b67ee18beacf126eecb78fd4a02608c (diff) | |
download | nextcloud-server-285f288cf344625dbddd3c6e4bacb9df1ddb6ce0.tar.gz nextcloud-server-285f288cf344625dbddd3c6e4bacb9df1ddb6ce0.zip |
Merge pull request #3959 from owncloud/buildNotExistingFileNameForView
Fix renaming using parenthesis
Diffstat (limited to 'lib')
-rw-r--r-- | lib/helper.php | 38 |
1 files changed, 33 insertions, 5 deletions
diff --git a/lib/helper.php b/lib/helper.php index 1860a55fc8f..df0d120976d 100644 --- a/lib/helper.php +++ b/lib/helper.php @@ -636,6 +636,18 @@ class OC_Helper { * @return string */ public static function buildNotExistingFileName($path, $filename) { + $view = \OC\Files\Filesystem::getView(); + return self::buildNotExistingFileNameForView($path, $filename, $view); + } + + /** + * Adds a suffix to the name in case the file exists + * + * @param $path + * @param $filename + * @return string + */ + public static function buildNotExistingFileNameForView($path, $filename, \OC\Files\View $view) { if($path==='/') { $path=''; } @@ -648,11 +660,27 @@ class OC_Helper { } $newpath = $path . '/' . $filename; - $counter = 2; - while (\OC\Files\Filesystem::file_exists($newpath)) { - $newname = $name . ' (' . $counter . ')' . $ext; - $newpath = $path . '/' . $newname; - $counter++; + if ($view->file_exists($newpath)) { + if(preg_match_all('/\((\d+)\)/', $name, $matches, PREG_OFFSET_CAPTURE)) { + //Replace the last "(number)" with "(number+1)" + $last_match = count($matches[0])-1; + $counter = $matches[1][$last_match][0]+1; + $offset = $matches[0][$last_match][1]; + $match_length = strlen($matches[0][$last_match][0]); + } else { + $counter = 2; + $offset = false; + } + do { + if($offset) { + //Replace the last "(number)" with "(number+1)" + $newname = substr_replace($name, '('.$counter.')', $offset, $match_length); + } else { + $newname = $name . ' (' . $counter . ')'; + } + $newpath = $path . '/' . $newname . $ext; + $counter++; + } while ($view->file_exists($newpath)); } return $newpath; |