Merge pull request #3959 from owncloud/buildNotExistingFileNameForView
Fix renaming using parenthesis
This commit is contained in:
commit
285f288cf3
|
@ -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;
|
||||
|
|
|
@ -146,4 +146,64 @@ class Test_Helper extends PHPUnit_Framework_TestCase {
|
|||
$result = OC_Helper::recursiveArraySearch($haystack, "NotFound");
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
function testBuildNotExistingFileNameForView() {
|
||||
$viewMock = $this->getMock('\OC\Files\View', array(), array(), '', false);
|
||||
$this->assertEquals('/filename', OC_Helper::buildNotExistingFileNameForView('/', 'filename', $viewMock));
|
||||
$this->assertEquals('dir/filename.ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename.ext', $viewMock));
|
||||
|
||||
$viewMock->expects($this->at(0))
|
||||
->method('file_exists')
|
||||
->will($this->returnValue(true)); // filename.ext exists
|
||||
$this->assertEquals('dir/filename (2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename.ext', $viewMock));
|
||||
|
||||
$viewMock->expects($this->at(0))
|
||||
->method('file_exists')
|
||||
->will($this->returnValue(true)); // filename.ext exists
|
||||
$viewMock->expects($this->at(1))
|
||||
->method('file_exists')
|
||||
->will($this->returnValue(true)); // filename (2).ext exists
|
||||
$this->assertEquals('dir/filename (3).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename.ext', $viewMock));
|
||||
|
||||
$viewMock->expects($this->at(0))
|
||||
->method('file_exists')
|
||||
->will($this->returnValue(true)); // filename (1).ext exists
|
||||
$this->assertEquals('dir/filename (2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename (1).ext', $viewMock));
|
||||
|
||||
$viewMock->expects($this->at(0))
|
||||
->method('file_exists')
|
||||
->will($this->returnValue(true)); // filename (2).ext exists
|
||||
$this->assertEquals('dir/filename (3).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename (2).ext', $viewMock));
|
||||
|
||||
$viewMock->expects($this->at(0))
|
||||
->method('file_exists')
|
||||
->will($this->returnValue(true)); // filename (2).ext exists
|
||||
$viewMock->expects($this->at(1))
|
||||
->method('file_exists')
|
||||
->will($this->returnValue(true)); // filename (3).ext exists
|
||||
$this->assertEquals('dir/filename (4).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename (2).ext', $viewMock));
|
||||
|
||||
$viewMock->expects($this->at(0))
|
||||
->method('file_exists')
|
||||
->will($this->returnValue(true)); // filename(1).ext exists
|
||||
$this->assertEquals('dir/filename(2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1).ext', $viewMock));
|
||||
|
||||
$viewMock->expects($this->at(0))
|
||||
->method('file_exists')
|
||||
->will($this->returnValue(true)); // filename(1) (1).ext exists
|
||||
$this->assertEquals('dir/filename(1) (2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1) (1).ext', $viewMock));
|
||||
|
||||
$viewMock->expects($this->at(0))
|
||||
->method('file_exists')
|
||||
->will($this->returnValue(true)); // filename(1) (1).ext exists
|
||||
$viewMock->expects($this->at(1))
|
||||
->method('file_exists')
|
||||
->will($this->returnValue(true)); // filename(1) (2).ext exists
|
||||
$this->assertEquals('dir/filename(1) (3).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1) (1).ext', $viewMock));
|
||||
|
||||
$viewMock->expects($this->at(0))
|
||||
->method('file_exists')
|
||||
->will($this->returnValue(true)); // filename(1) (2) (3).ext exists
|
||||
$this->assertEquals('dir/filename(1) (2) (4).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1) (2) (3).ext', $viewMock));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue