3737use OCP \ITagManager ;
3838use OCP \IUserManager ;
3939use OCP \IUserSession ;
40+ use OCP \L10N \IFactory ;
4041use OCP \SabrePluginEvent ;
4142use OCP \SystemTag \ISystemTagManager ;
4243use OCP \SystemTag \ISystemTagObjectMapper ;
@@ -70,15 +71,13 @@ public function createServer(
7071 Plugin $ authPlugin ,
7172 callable $ viewCallBack ,
7273 ): Server {
74+ // /public.php/webdav/ shows the files in the share in the root itself
75+ // and not under /public.php/webdav/files/{token} so we should keep
76+ // compatibility for that.
77+ $ needsSharesInRoot = $ baseUri === '/public.php/webdav/ ' ;
78+ $ useCollection = $ isPublicShare && !$ needsSharesInRoot ;
7379 $ 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- }
80+ [$ tree , $ rootCollection ] = $ this ->getTree ($ useCollection );
8281 $ server = new Server ($ tree );
8382 // Set URL explicitly due to reverse-proxy situations
8483 $ server ->httpRequest ->setUrl ($ requestUri );
@@ -127,8 +126,8 @@ public function createServer(
127126 }
128127
129128 // wait with registering these until auth is handled and the filesystem is setup
130- $ server ->on ('beforeMethod:* ' , function () use ($ server , $ tree ,
131- $ viewCallBack , $ isPublicShare , $ rootCollection , $ debugEnabled ): void {
129+ $ server ->on ('beforeMethod:* ' , function () use ($ server ,
130+ $ tree , $ viewCallBack , $ isPublicShare , $ rootCollection , $ debugEnabled ): void {
132131 // ensure the skeleton is copied
133132 $ userFolder = \OC ::$ server ->getUserFolder ();
134133
@@ -147,36 +146,8 @@ public function createServer(
147146 $ root = new File ($ view , $ rootInfo );
148147 }
149148
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- ));
149+ if ($ rootCollection !== null ) {
150+ $ this ->initRootCollection ($ rootCollection , $ root );
180151 } else {
181152 /** @var ObjectTree $tree */
182153 $ tree ->init ($ root , $ view , $ this ->mountManager );
@@ -191,7 +162,7 @@ public function createServer(
191162 $ this ->userSession ,
192163 \OCP \Server::get (IFilenameValidator::class),
193164 \OCP \Server::get (IAccountManager::class),
194- false ,
165+ $ isPublicShare ,
195166 !$ debugEnabled
196167 )
197168 );
@@ -252,4 +223,61 @@ public function createServer(
252223 }, 30 ); // priority 30: after auth (10) and acl(20), before lock(50) and handling the request
253224 return $ server ;
254225 }
226+
227+ /**
228+ * Returns a Tree object and, if $useCollection is true, the collection used
229+ * as root.
230+ *
231+ * @param bool $useCollection Whether to use a collection or the legacy
232+ * ObjectTree, which doesn't use collections.
233+ * @return array{0: CachingTree|ObjectTree, 1: SimpleCollection|null}
234+ */
235+ public function getTree (bool $ useCollection ): array {
236+ if ($ useCollection ) {
237+ $ rootCollection = new SimpleCollection ('root ' );
238+ $ tree = new CachingTree ($ rootCollection );
239+ return [$ tree , $ rootCollection ];
240+ }
241+
242+ return [new ObjectTree (), null ];
243+ }
244+
245+ /**
246+ * Adds the user's principal backend to $rootCollection.
247+ */
248+ private function initRootCollection (SimpleCollection $ rootCollection , Directory |File $ root ): void {
249+ $ userPrincipalBackend = new Principal (
250+ \OCP \Server::get (IUserManager::class),
251+ \OCP \Server::get (IGroupManager::class),
252+ \OCP \Server::get (IAccountManager::class),
253+ \OCP \Server::get (\OCP \Share \IManager::class),
254+ \OCP \Server::get (IUserSession::class),
255+ \OCP \Server::get (IAppManager::class),
256+ \OCP \Server::get (ProxyMapper::class),
257+ \OCP \Server::get (KnownUserService::class),
258+ \OCP \Server::get (IConfig::class),
259+ \OCP \Server::get (IFactory::class),
260+ );
261+
262+ // Mount the share collection at /public.php/dav/files/<share token>
263+ $ rootCollection ->addChild (
264+ new RootCollection (
265+ $ root ,
266+ $ userPrincipalBackend ,
267+ 'principals/shares ' ,
268+ )
269+ );
270+
271+ // Mount the upload collection at /public.php/dav/uploads/<share token>
272+ $ rootCollection ->addChild (
273+ new \OCA \DAV \Upload \RootCollection (
274+ $ userPrincipalBackend ,
275+ 'principals/shares ' ,
276+ \OCP \Server::get (CleanupService::class),
277+ \OCP \Server::get (IRootFolder::class),
278+ \OCP \Server::get (IUserSession::class),
279+ \OCP \Server::get (\OCP \Share \IManager::class),
280+ )
281+ );
282+ }
255283}
0 commit comments