From c5ad20d92546c4e69f07e9c2e85eae2d156fe023 Mon Sep 17 00:00:00 2001 From: Salvatore Martire <4652631+salmart-dev@users.noreply.github.com> Date: Tue, 16 Sep 2025 16:58:09 +0200 Subject: [PATCH] refactor: extract tree initialization logic Signed-off-by: Salvatore Martire <4652631+salmart-dev@users.noreply.github.com> --- .../dav/lib/Connector/Sabre/ServerFactory.php | 101 +++++++++++------- build/psalm-baseline.xml | 1 - 2 files changed, 62 insertions(+), 40 deletions(-) diff --git a/apps/dav/lib/Connector/Sabre/ServerFactory.php b/apps/dav/lib/Connector/Sabre/ServerFactory.php index d01ea6b223e..c86d22956be 100644 --- a/apps/dav/lib/Connector/Sabre/ServerFactory.php +++ b/apps/dav/lib/Connector/Sabre/ServerFactory.php @@ -37,6 +37,7 @@ use OCP\IRequest; use OCP\ITagManager; use OCP\IUserManager; use OCP\IUserSession; +use OCP\L10N\IFactory; use OCP\SabrePluginEvent; use OCP\SystemTag\ISystemTagManager; use OCP\SystemTag\ISystemTagObjectMapper; @@ -71,14 +72,7 @@ class ServerFactory { callable $viewCallBack, ): Server { $debugEnabled = $this->config->getSystemValue('debug', false); - // Fire up server - if ($isPublicShare) { - $rootCollection = new SimpleCollection('root'); - $tree = new CachingTree($rootCollection); - } else { - $rootCollection = null; - $tree = new ObjectTree(); - } + [$tree, $rootCollection] = $this->getTree($isPublicShare); $server = new Server($tree); // Set URL explicitly due to reverse-proxy situations $server->httpRequest->setUrl($requestUri); @@ -128,7 +122,7 @@ class ServerFactory { // wait with registering these until auth is handled and the filesystem is setup $server->on('beforeMethod:*', function () use ($server, $tree, - $viewCallBack, $isPublicShare, $rootCollection, $debugEnabled): void { + $viewCallBack, $rootCollection, $debugEnabled): void { // ensure the skeleton is copied $userFolder = \OC::$server->getUserFolder(); @@ -147,36 +141,8 @@ class ServerFactory { $root = new File($view, $rootInfo); } - if ($isPublicShare) { - $userPrincipalBackend = new Principal( - \OCP\Server::get(IUserManager::class), - \OCP\Server::get(IGroupManager::class), - \OCP\Server::get(IAccountManager::class), - \OCP\Server::get(\OCP\Share\IManager::class), - \OCP\Server::get(IUserSession::class), - \OCP\Server::get(IAppManager::class), - \OCP\Server::get(ProxyMapper::class), - \OCP\Server::get(KnownUserService::class), - \OCP\Server::get(IConfig::class), - \OC::$server->getL10NFactory(), - ); - - // Mount the share collection at /public.php/dav/shares/ - $rootCollection->addChild(new RootCollection( - $root, - $userPrincipalBackend, - 'principals/shares', - )); - - // Mount the upload collection at /public.php/dav/uploads/ - $rootCollection->addChild(new \OCA\DAV\Upload\RootCollection( - $userPrincipalBackend, - 'principals/shares', - \OCP\Server::get(CleanupService::class), - \OCP\Server::get(IRootFolder::class), - \OCP\Server::get(IUserSession::class), - \OCP\Server::get(\OCP\Share\IManager::class), - )); + if ($rootCollection !== null) { + $this->initRootCollection($rootCollection, $root); } else { /** @var ObjectTree $tree */ $tree->init($root, $view, $this->mountManager); @@ -252,4 +218,61 @@ class ServerFactory { }, 30); // priority 30: after auth (10) and acl(20), before lock(50) and handling the request return $server; } + + /** + * Returns a Tree object and, if $useCollection is true, the collection used + * as root. + * + * @param bool $useCollection Whether to use a collection or the legacy + * ObjectTree, which doesn't use collections. + * @return array{0: CachingTree|ObjectTree, 1: SimpleCollection|null} + */ + public function getTree(bool $useCollection): array { + if ($useCollection) { + $rootCollection = new SimpleCollection('root'); + $tree = new CachingTree($rootCollection); + return [$tree, $rootCollection]; + } + + return [new ObjectTree(), null]; + } + + /** + * Adds the user's principal backend to $rootCollection. + */ + private function initRootCollection(SimpleCollection $rootCollection, Directory|File $root): void { + $userPrincipalBackend = new Principal( + \OCP\Server::get(IUserManager::class), + \OCP\Server::get(IGroupManager::class), + \OCP\Server::get(IAccountManager::class), + \OCP\Server::get(\OCP\Share\IManager::class), + \OCP\Server::get(IUserSession::class), + \OCP\Server::get(IAppManager::class), + \OCP\Server::get(ProxyMapper::class), + \OCP\Server::get(KnownUserService::class), + \OCP\Server::get(IConfig::class), + \OCP\Server::get(IFactory::class), + ); + + // Mount the share collection at /public.php/dav/files/ + $rootCollection->addChild( + new RootCollection( + $root, + $userPrincipalBackend, + 'principals/shares', + ) + ); + + // Mount the upload collection at /public.php/dav/uploads/ + $rootCollection->addChild( + new \OCA\DAV\Upload\RootCollection( + $userPrincipalBackend, + 'principals/shares', + \OCP\Server::get(CleanupService::class), + \OCP\Server::get(IRootFolder::class), + \OCP\Server::get(IUserSession::class), + \OCP\Server::get(\OCP\Share\IManager::class), + ) + ); + } } diff --git a/build/psalm-baseline.xml b/build/psalm-baseline.xml index d8e42dff4da..8fbc32c4fec 100644 --- a/build/psalm-baseline.xml +++ b/build/psalm-baseline.xml @@ -736,7 +736,6 @@ -