Skip to content

Commit 61f0a5b

Browse files
committed
feat: Add Folder::getOrCreateFolder api
Allow to remove some boilerplate and also this new function is type safe. Signed-off-by: Carl Schwan <[email protected]>
1 parent ed9cee1 commit 61f0a5b

File tree

4 files changed

+43
-9
lines changed

4 files changed

+43
-9
lines changed

lib/private/Files/Node/Folder.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use OC\User\LazyUser;
1717
use OCP\Files\Cache\ICacheEntry;
1818
use OCP\Files\FileInfo;
19+
use OCP\Files\Folder as IFolder;
1920
use OCP\Files\Mount\IMountPoint;
2021
use OCP\Files\Node as INode;
2122
use OCP\Files\NotFoundException;
@@ -26,8 +27,9 @@
2627
use OCP\Files\Search\ISearchOrder;
2728
use OCP\Files\Search\ISearchQuery;
2829
use OCP\IUserManager;
30+
use Override;
2931

30-
class Folder extends Node implements \OCP\Files\Folder {
32+
class Folder extends Node implements IFolder {
3133

3234
private ?IUserManager $userManager = null;
3335

@@ -480,4 +482,28 @@ private function recreateIfNeeded(): void {
480482
$this->wasDeleted = false;
481483
}
482484
}
485+
486+
#[Override]
487+
public function getOrCreateFolder(string $path, int $maxRetries = 5): IFolder {
488+
$i = 0;
489+
while (true) {
490+
$path = $i === 0 ? $path : $path . ' (' . $i . ')';
491+
try {
492+
$folder = $this->get($path);
493+
if ($folder instanceof IFolder) {
494+
return $folder;
495+
}
496+
} catch (NotFoundException) {
497+
$folder = $this->get(dirname($path));
498+
if (!($folder instanceof Folder)) {
499+
throw new NotPermittedException("Unable to create folder $path. Parent is not a directory.");
500+
}
501+
return $folder->newFolder(basename($path));
502+
}
503+
$i++;
504+
if ($i > 5) {
505+
throw new NotPermittedException('Unable to load or create folder.');
506+
}
507+
}
508+
}
483509
}

lib/private/Files/Node/LazyFolder.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use OCP\Files\IRootFolder;
1515
use OCP\Files\Mount\IMountPoint;
1616
use OCP\Files\NotPermittedException;
17+
use Override;
1718

1819
/**
1920
* Class LazyFolder
@@ -138,6 +139,11 @@ public function get($path) {
138139
return $this->getRootFolder()->get($this->getFullPath($path));
139140
}
140141

142+
#[Override]
143+
public function getOrCreateFolder(string $path, int $maxRetries = 5): Folder {
144+
return $this->getRootFolder()->getOrCreateFolder($this->getFullPath($path), $maxRetries);
145+
}
146+
141147
/**
142148
* @inheritDoc
143149
*/

lib/private/Files/Template/TemplateManager.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -355,14 +355,7 @@ public function initializeTemplateDirectory(?string $path = null, ?string $userI
355355
}
356356
}
357357

358-
try {
359-
/** @var Folder $folder */
360-
$folder = $userFolder->get($userTemplatePath);
361-
} catch (NotFoundException $e) {
362-
/** @var Folder $folder */
363-
$folder = $userFolder->get(dirname($userTemplatePath));
364-
$folder = $folder->newFolder(basename($userTemplatePath));
365-
}
358+
$folder = $userFolder->getOrCreateFolder($userTemplatePath);
366359

367360
$folderIsEmpty = count($folder->getDirectoryListing()) === 0;
368361

lib/public/Files/Folder.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ public function getDirectoryListing();
6565
*/
6666
public function get($path);
6767

68+
/**
69+
* Get or create new folder if the folder does not already exist.
70+
*
71+
* @param string $path relative path of the file or folder
72+
* @throw \OCP\Files\NotPermittedException
73+
* @since 33.0.0
74+
*/
75+
public function getOrCreateFolder(string $path, int $maxRetries = 5): Folder;
76+
6877
/**
6978
* Check if a file or folder exists in the folder
7079
*

0 commit comments

Comments
 (0)