Skip to content

Commit c5ad20d

Browse files
committed
refactor: extract tree initialization logic
Signed-off-by: Salvatore Martire <[email protected]>
1 parent e89a8d8 commit c5ad20d

File tree

2 files changed

+62
-40
lines changed

2 files changed

+62
-40
lines changed

apps/dav/lib/Connector/Sabre/ServerFactory.php

Lines changed: 62 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use OCP\ITagManager;
3838
use OCP\IUserManager;
3939
use OCP\IUserSession;
40+
use OCP\L10N\IFactory;
4041
use OCP\SabrePluginEvent;
4142
use OCP\SystemTag\ISystemTagManager;
4243
use OCP\SystemTag\ISystemTagObjectMapper;
@@ -71,14 +72,7 @@ public function createServer(
7172
callable $viewCallBack,
7273
): Server {
7374
$debugEnabled = $this->config->getSystemValue('debug', false);
74-
// Fire up server
75-
if ($isPublicShare) {
76-
$rootCollection = new SimpleCollection('root');
77-
$tree = new CachingTree($rootCollection);
78-
} else {
79-
$rootCollection = null;
80-
$tree = new ObjectTree();
81-
}
75+
[$tree, $rootCollection] = $this->getTree($isPublicShare);
8276
$server = new Server($tree);
8377
// Set URL explicitly due to reverse-proxy situations
8478
$server->httpRequest->setUrl($requestUri);
@@ -128,7 +122,7 @@ public function createServer(
128122

129123
// wait with registering these until auth is handled and the filesystem is setup
130124
$server->on('beforeMethod:*', function () use ($server, $tree,
131-
$viewCallBack, $isPublicShare, $rootCollection, $debugEnabled): void {
125+
$viewCallBack, $rootCollection, $debugEnabled): void {
132126
// ensure the skeleton is copied
133127
$userFolder = \OC::$server->getUserFolder();
134128

@@ -147,36 +141,8 @@ public function createServer(
147141
$root = new File($view, $rootInfo);
148142
}
149143

150-
if ($isPublicShare) {
151-
$userPrincipalBackend = new Principal(
152-
\OCP\Server::get(IUserManager::class),
153-
\OCP\Server::get(IGroupManager::class),
154-
\OCP\Server::get(IAccountManager::class),
155-
\OCP\Server::get(\OCP\Share\IManager::class),
156-
\OCP\Server::get(IUserSession::class),
157-
\OCP\Server::get(IAppManager::class),
158-
\OCP\Server::get(ProxyMapper::class),
159-
\OCP\Server::get(KnownUserService::class),
160-
\OCP\Server::get(IConfig::class),
161-
\OC::$server->getL10NFactory(),
162-
);
163-
164-
// Mount the share collection at /public.php/dav/shares/<share token>
165-
$rootCollection->addChild(new RootCollection(
166-
$root,
167-
$userPrincipalBackend,
168-
'principals/shares',
169-
));
170-
171-
// Mount the upload collection at /public.php/dav/uploads/<share token>
172-
$rootCollection->addChild(new \OCA\DAV\Upload\RootCollection(
173-
$userPrincipalBackend,
174-
'principals/shares',
175-
\OCP\Server::get(CleanupService::class),
176-
\OCP\Server::get(IRootFolder::class),
177-
\OCP\Server::get(IUserSession::class),
178-
\OCP\Server::get(\OCP\Share\IManager::class),
179-
));
144+
if ($rootCollection !== null) {
145+
$this->initRootCollection($rootCollection, $root);
180146
} else {
181147
/** @var ObjectTree $tree */
182148
$tree->init($root, $view, $this->mountManager);
@@ -252,4 +218,61 @@ public function createServer(
252218
}, 30); // priority 30: after auth (10) and acl(20), before lock(50) and handling the request
253219
return $server;
254220
}
221+
222+
/**
223+
* Returns a Tree object and, if $useCollection is true, the collection used
224+
* as root.
225+
*
226+
* @param bool $useCollection Whether to use a collection or the legacy
227+
* ObjectTree, which doesn't use collections.
228+
* @return array{0: CachingTree|ObjectTree, 1: SimpleCollection|null}
229+
*/
230+
public function getTree(bool $useCollection): array {
231+
if ($useCollection) {
232+
$rootCollection = new SimpleCollection('root');
233+
$tree = new CachingTree($rootCollection);
234+
return [$tree, $rootCollection];
235+
}
236+
237+
return [new ObjectTree(), null];
238+
}
239+
240+
/**
241+
* Adds the user's principal backend to $rootCollection.
242+
*/
243+
private function initRootCollection(SimpleCollection $rootCollection, Directory|File $root): void {
244+
$userPrincipalBackend = new Principal(
245+
\OCP\Server::get(IUserManager::class),
246+
\OCP\Server::get(IGroupManager::class),
247+
\OCP\Server::get(IAccountManager::class),
248+
\OCP\Server::get(\OCP\Share\IManager::class),
249+
\OCP\Server::get(IUserSession::class),
250+
\OCP\Server::get(IAppManager::class),
251+
\OCP\Server::get(ProxyMapper::class),
252+
\OCP\Server::get(KnownUserService::class),
253+
\OCP\Server::get(IConfig::class),
254+
\OCP\Server::get(IFactory::class),
255+
);
256+
257+
// Mount the share collection at /public.php/dav/files/<share token>
258+
$rootCollection->addChild(
259+
new RootCollection(
260+
$root,
261+
$userPrincipalBackend,
262+
'principals/shares',
263+
)
264+
);
265+
266+
// Mount the upload collection at /public.php/dav/uploads/<share token>
267+
$rootCollection->addChild(
268+
new \OCA\DAV\Upload\RootCollection(
269+
$userPrincipalBackend,
270+
'principals/shares',
271+
\OCP\Server::get(CleanupService::class),
272+
\OCP\Server::get(IRootFolder::class),
273+
\OCP\Server::get(IUserSession::class),
274+
\OCP\Server::get(\OCP\Share\IManager::class),
275+
)
276+
);
277+
}
255278
}

build/psalm-baseline.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,6 @@
736736
</file>
737737
<file src="apps/dav/lib/Connector/Sabre/ServerFactory.php">
738738
<DeprecatedMethod>
739-
<code><![CDATA[getL10NFactory]]></code>
740739
<code><![CDATA[getUserFolder]]></code>
741740
</DeprecatedMethod>
742741
<InternalMethod>

0 commit comments

Comments
 (0)