Skip to content

Commit 6c01183

Browse files
committed
refactor(OC_Helper): remove buildNotExistingFileNameForView
Move the functionality in the last place it is used OC\Files\Node\Folder Signed-off-by: Carl Schwan <[email protected]>
1 parent 6850683 commit 6c01183

File tree

9 files changed

+142
-179
lines changed

9 files changed

+142
-179
lines changed

apps/files_sharing/lib/External/Manager.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
namespace OCA\Files_Sharing\External;
1010

11-
use HttpResponse;
1211
use OC\Files\Filesystem;
1312
use OC\Files\SetupManager;
1413
use OC\User\NoUserException;
@@ -28,7 +27,6 @@
2827
use OCP\Files\NotPermittedException;
2928
use OCP\Files\Storage\IStorageFactory;
3029
use OCP\Http\Client\IClientService;
31-
use OCP\Http\Client\IResponse;
3230
use OCP\ICertificateManager;
3331
use OCP\IDBConnection;
3432
use OCP\IGroup;

build/psalm-baseline.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4094,10 +4094,6 @@
40944094
</InvalidArgument>
40954095
</file>
40964096
<file src="lib/private/legacy/OC_Helper.php">
4097-
<InvalidArrayOffset>
4098-
<code><![CDATA[$matches[0][$last_match]]]></code>
4099-
<code><![CDATA[$matches[1][$last_match]]]></code>
4100-
</InvalidArrayOffset>
41014097
<UndefinedInterfaceMethod>
41024098
<code><![CDATA[getQuota]]></code>
41034099
</UndefinedInterfaceMethod>

lib/private/Files/Node/Folder.php

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -389,13 +389,50 @@ public function delete() {
389389
/**
390390
* Add a suffix to the name in case the file exists
391391
*
392-
* @param string $name
392+
* @param string $filename
393393
* @return string
394394
* @throws NotPermittedException
395395
*/
396-
public function getNonExistingName($name) {
397-
$uniqueName = \OC_Helper::buildNotExistingFileNameForView($this->getPath(), $name, $this->view);
398-
return trim($this->getRelativePath($uniqueName), '/');
396+
public function getNonExistingName($filename) {
397+
$path = $this->getPath();
398+
if ($path === '/') {
399+
$path = '';
400+
}
401+
if ($pos = strrpos($filename, '.')) {
402+
$name = substr($filename, 0, $pos);
403+
$ext = substr($filename, $pos);
404+
} else {
405+
$name = $filename;
406+
$ext = '';
407+
}
408+
409+
$newpath = $path . '/' . $filename;
410+
if ($this->view->file_exists($newpath)) {
411+
if (preg_match_all('/\((\d+)\)/', $name, $matches, PREG_OFFSET_CAPTURE)) {
412+
/** @var array<int<0, max>, array> $matches */
413+
//Replace the last "(number)" with "(number+1)"
414+
$last_match = count($matches[0]) - 1;
415+
$counter = $matches[1][$last_match][0] + 1;
416+
$offset = $matches[0][$last_match][1];
417+
$match_length = strlen($matches[0][$last_match][0]);
418+
} else {
419+
$counter = 2;
420+
$match_length = 0;
421+
$offset = false;
422+
}
423+
do {
424+
if ($offset) {
425+
//Replace the last "(number)" with "(number+1)"
426+
$newname = substr_replace($name, '(' . $counter . ')', $offset, $match_length);
427+
} else {
428+
$newname = $name . ' (' . $counter . ')';
429+
}
430+
$newpath = $path . '/' . $newname . $ext;
431+
$counter++;
432+
} while ($this->view->file_exists($newpath));
433+
}
434+
435+
return trim($this->getRelativePath($newpath), '/');
399436
}
400437

401438
/**

lib/private/Files/Node/LazyFolder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ public function isCreatable() {
490490
/**
491491
* @inheritDoc
492492
*/
493-
public function getNonExistingName($name) {
493+
public function getNonExistingName($fileame) {
494494
return $this->__call(__FUNCTION__, func_get_args());
495495
}
496496

lib/private/legacy/OC_Helper.php

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -129,65 +129,6 @@ public static function streamCopy($source, $target) {
129129
return \OCP\Files::streamCopy($source, $target, true);
130130
}
131131

132-
/**
133-
* Adds a suffix to the name in case the file exists
134-
*
135-
* @param string $path
136-
* @param string $filename
137-
* @return string
138-
*/
139-
public static function buildNotExistingFileName($path, $filename) {
140-
$view = \OC\Files\Filesystem::getView();
141-
return self::buildNotExistingFileNameForView($path, $filename, $view);
142-
}
143-
144-
/**
145-
* Adds a suffix to the name in case the file exists
146-
*
147-
* @param string $path
148-
* @param string $filename
149-
* @return string
150-
*/
151-
public static function buildNotExistingFileNameForView($path, $filename, \OC\Files\View $view) {
152-
if ($path === '/') {
153-
$path = '';
154-
}
155-
if ($pos = strrpos($filename, '.')) {
156-
$name = substr($filename, 0, $pos);
157-
$ext = substr($filename, $pos);
158-
} else {
159-
$name = $filename;
160-
$ext = '';
161-
}
162-
163-
$newpath = $path . '/' . $filename;
164-
if ($view->file_exists($newpath)) {
165-
if (preg_match_all('/\((\d+)\)/', $name, $matches, PREG_OFFSET_CAPTURE)) {
166-
//Replace the last "(number)" with "(number+1)"
167-
$last_match = count($matches[0]) - 1;
168-
$counter = $matches[1][$last_match][0] + 1;
169-
$offset = $matches[0][$last_match][1];
170-
$match_length = strlen($matches[0][$last_match][0]);
171-
} else {
172-
$counter = 2;
173-
$match_length = 0;
174-
$offset = false;
175-
}
176-
do {
177-
if ($offset) {
178-
//Replace the last "(number)" with "(number+1)"
179-
$newname = substr_replace($name, '(' . $counter . ')', $offset, $match_length);
180-
} else {
181-
$newname = $name . ' (' . $counter . ')';
182-
}
183-
$newpath = $path . '/' . $newname . $ext;
184-
$counter++;
185-
} while ($view->file_exists($newpath));
186-
}
187-
188-
return $newpath;
189-
}
190-
191132
/**
192133
* Checks if a function is available
193134
*

lib/public/Files.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,4 @@ public static function streamCopy($source, $target, ?bool $includeResult = null)
126126
}
127127
return $includeResult ? [$count, $result] : $count;
128128
}
129-
130-
/**
131-
* Adds a suffix to the name in case the file exists
132-
* @param string $path
133-
* @param string $filename
134-
* @return string
135-
* @since 5.0.0
136-
* @deprecated 14.0.0 use getNonExistingName of the OCP\Files\Folder object
137-
*/
138-
public static function buildNotExistingFileName($path, $filename) {
139-
return \OC_Helper::buildNotExistingFileName($path, $filename);
140-
}
141129
}

lib/public/Files/Folder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,12 @@ public function isCreatable();
185185
/**
186186
* Add a suffix to the name in case the file exists
187187
*
188-
* @param string $name
188+
* @param string $filename
189189
* @return string
190190
* @throws NotPermittedException
191191
* @since 8.1.0
192192
*/
193-
public function getNonExistingName($name);
193+
public function getNonExistingName($filename);
194194

195195
/**
196196
* @param int $limit

tests/lib/Files/Node/FolderTest.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,4 +1041,102 @@ public function testSearchSubStoragesLimitOffset(int $offset, int $limit, array
10411041
}, $result);
10421042
$this->assertEquals($expectedPaths, $ids);
10431043
}
1044+
1045+
private function callBuildNotExistingFileNameForView(string $path, string $name, View&MockObject $view): string {
1046+
$rootFolder = $this->createMock(IRootFolder::class);
1047+
$folder = new Folder($rootFolder, $view, $path);
1048+
return $path . (str_ends_with('/', $path) ? '' : '/') . $folder->getNonExistingName($name);
1049+
}
1050+
1051+
public function testBuildNotExistingFileNameForView(): void {
1052+
$viewMock = $this->createMock(View::class);
1053+
$this->assertEquals('/filename', $this->callBuildNotExistingFileNameForView('/', 'filename', $viewMock));
1054+
$this->assertEquals('dir/filename.ext', $this->callBuildNotExistingFileNameForView('dir', 'filename.ext', $viewMock));
1055+
1056+
$viewMock = $this->createMock(View::class);
1057+
$viewMock->expects($this->exactly(2))
1058+
->method('file_exists')
1059+
->willReturnMap([
1060+
// Conflict on filename.ext
1061+
['dir/filename.ext', true],
1062+
['dir/filename (2).ext', false],
1063+
]);
1064+
$this->assertEquals('dir/filename (2).ext', $this->callBuildNotExistingFileNameForView('dir', 'filename.ext', $viewMock));
1065+
1066+
$viewMock = $this->createMock(View::class);
1067+
$viewMock->expects($this->exactly(3))
1068+
->method('file_exists')
1069+
->willReturnMap([
1070+
// Conflict on filename.ext
1071+
['dir/filename.ext', true],
1072+
['dir/filename (2).ext', true],
1073+
['dir/filename (3).ext', false],
1074+
]);
1075+
$this->assertEquals('dir/filename (3).ext', $this->callBuildNotExistingFileNameForView('dir', 'filename.ext', $viewMock));
1076+
1077+
$viewMock = $this->createMock(View::class);
1078+
$viewMock->expects($this->exactly(2))
1079+
->method('file_exists')
1080+
->willReturnMap([
1081+
['dir/filename (1).ext', true],
1082+
['dir/filename (2).ext', false],
1083+
]);
1084+
$this->assertEquals('dir/filename (2).ext', $this->callBuildNotExistingFileNameForView('dir', 'filename (1).ext', $viewMock));
1085+
1086+
$viewMock = $this->createMock(View::class);
1087+
$viewMock->expects($this->exactly(2))
1088+
->method('file_exists')
1089+
->willReturnMap([
1090+
['dir/filename (2).ext', true],
1091+
['dir/filename (3).ext', false],
1092+
]);
1093+
$this->assertEquals('dir/filename (3).ext', $this->callBuildNotExistingFileNameForView('dir', 'filename (2).ext', $viewMock));
1094+
1095+
$viewMock = $this->createMock(View::class);
1096+
$viewMock->expects($this->exactly(3))
1097+
->method('file_exists')
1098+
->willReturnMap([
1099+
['dir/filename (2).ext', true],
1100+
['dir/filename (3).ext', true],
1101+
['dir/filename (4).ext', false],
1102+
]);
1103+
$this->assertEquals('dir/filename (4).ext', $this->callBuildNotExistingFileNameForView('dir', 'filename (2).ext', $viewMock));
1104+
1105+
$viewMock = $this->createMock(View::class);
1106+
$viewMock->expects($this->exactly(2))
1107+
->method('file_exists')
1108+
->willReturnMap([
1109+
['dir/filename(1).ext', true],
1110+
['dir/filename(2).ext', false],
1111+
]);
1112+
$this->assertEquals('dir/filename(2).ext', $this->callBuildNotExistingFileNameForView('dir', 'filename(1).ext', $viewMock));
1113+
1114+
$viewMock = $this->createMock(View::class);
1115+
$viewMock->expects($this->exactly(2))
1116+
->method('file_exists')
1117+
->willReturnMap([
1118+
['dir/filename(1) (1).ext', true],
1119+
['dir/filename(1) (2).ext', false],
1120+
]);
1121+
$this->assertEquals('dir/filename(1) (2).ext', $this->callBuildNotExistingFileNameForView('dir', 'filename(1) (1).ext', $viewMock));
1122+
1123+
$viewMock = $this->createMock(View::class);
1124+
$viewMock->expects($this->exactly(3))
1125+
->method('file_exists')
1126+
->willReturnMap([
1127+
['dir/filename(1) (1).ext', true],
1128+
['dir/filename(1) (2).ext', true],
1129+
['dir/filename(1) (3).ext', false],
1130+
]);
1131+
$this->assertEquals('dir/filename(1) (3).ext', $this->callBuildNotExistingFileNameForView('dir', 'filename(1) (1).ext', $viewMock));
1132+
1133+
$viewMock = $this->createMock(View::class);
1134+
$viewMock->expects($this->exactly(2))
1135+
->method('file_exists')
1136+
->willReturnMap([
1137+
['dir/filename(1) (2) (3).ext', true],
1138+
['dir/filename(1) (2) (4).ext', false],
1139+
]);
1140+
$this->assertEquals('dir/filename(1) (2) (4).ext', $this->callBuildNotExistingFileNameForView('dir', 'filename(1) (2) (3).ext', $viewMock));
1141+
}
10441142
}

0 commit comments

Comments
 (0)