diff options
author | Bart Visscher <bartv@thisnet.nl> | 2013-07-05 15:38:09 +0200 |
---|---|---|
committer | Bart Visscher <bartv@thisnet.nl> | 2013-07-05 15:38:09 +0200 |
commit | 22c29eb64b67ee18beacf126eecb78fd4a02608c (patch) | |
tree | e477c298fcfd832ffcf1823ee9d1b5c6951d5aea /lib/helper.php | |
parent | 424ec94680e69a3082a3d3f7c1ceefd7eeae15d2 (diff) | |
download | nextcloud-server-22c29eb64b67ee18beacf126eecb78fd4a02608c.tar.gz nextcloud-server-22c29eb64b67ee18beacf126eecb78fd4a02608c.zip |
Fix renaming using parenthesis
Diffstat (limited to 'lib/helper.php')
-rw-r--r-- | lib/helper.php | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/lib/helper.php b/lib/helper.php index 017221cef77..df0d120976d 100644 --- a/lib/helper.php +++ b/lib/helper.php @@ -660,11 +660,27 @@ class OC_Helper { } $newpath = $path . '/' . $filename; - $counter = 2; - while ($view->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; |