88namespace OCA \DAV \Connector \Sabre ;
99
1010use OC \Files \View ;
11+ use OC \KnownUser \KnownUserService ;
1112use OCA \DAV \AppInfo \PluginManager ;
1213use OCA \DAV \CalDAV \DefaultCalendarValidator ;
14+ use OCA \DAV \CalDAV \Proxy \ProxyMapper ;
1315use OCA \DAV \DAV \CustomPropertiesBackend ;
1416use OCA \DAV \DAV \ViewOnlyPlugin ;
1517use OCA \DAV \Files \BrowserErrorPagePlugin ;
2830use OCP \IPreview ;
2931use OCP \IRequest ;
3032use OCP \ITagManager ;
33+ use OCP \IUserManager ;
3134use OCP \IUserSession ;
3235use OCP \SabrePluginEvent ;
3336use OCP \SystemTag \ISystemTagManager ;
3437use OCP \SystemTag \ISystemTagObjectMapper ;
3538use Psr \Log \LoggerInterface ;
3639use Sabre \DAV \Auth \Plugin ;
40+ use Sabre \DAV \SimpleCollection ;
3741
3842class ServerFactory {
3943
@@ -54,13 +58,22 @@ public function __construct(
5458 /**
5559 * @param callable $viewCallBack callback that should return the view for the dav endpoint
5660 */
57- public function createServer (string $ baseUri ,
61+ public function createServer (
62+ bool $ isPublicShare ,
63+ string $ baseUri ,
5864 string $ requestUri ,
5965 Plugin $ authPlugin ,
60- callable $ viewCallBack ): Server {
66+ callable $ viewCallBack ,
67+ ): Server {
6168 // Fire up server
62- $ objectTree = new ObjectTree ();
63- $ server = new Server ($ objectTree );
69+ if ($ isPublicShare ) {
70+ $ rootCollection = new SimpleCollection ('root ' );
71+ $ tree = new CachingTree ($ rootCollection );
72+ } else {
73+ $ rootCollection = null ;
74+ $ tree = new ObjectTree ();
75+ }
76+ $ server = new Server ($ tree );
6477 // Set URL explicitly due to reverse-proxy situations
6578 $ server ->httpRequest ->setUrl ($ requestUri );
6679 $ server ->setBaseUri ($ baseUri );
@@ -81,7 +94,7 @@ public function createServer(string $baseUri,
8194 $ server ->addPlugin (new RequestIdHeaderPlugin ($ this ->request ));
8295
8396 $ server ->addPlugin (new ZipFolderPlugin (
84- $ objectTree ,
97+ $ tree ,
8598 $ this ->logger ,
8699 $ this ->eventDispatcher ,
87100 ));
@@ -101,7 +114,7 @@ public function createServer(string $baseUri,
101114 }
102115
103116 // wait with registering these until auth is handled and the filesystem is setup
104- $ server ->on ('beforeMethod:* ' , function () use ($ server , $ objectTree , $ viewCallBack ): void {
117+ $ server ->on ('beforeMethod:* ' , function () use ($ server , $ tree , $ viewCallBack, $ isPublicShare , $ rootCollection ): void {
105118 // ensure the skeleton is copied
106119 $ userFolder = \OC ::$ server ->getUserFolder ();
107120
@@ -115,15 +128,39 @@ public function createServer(string $baseUri,
115128
116129 // Create Nextcloud Dir
117130 if ($ rootInfo ->getType () === 'dir ' ) {
118- $ root = new Directory ($ view , $ rootInfo , $ objectTree );
131+ $ root = new Directory ($ view , $ rootInfo , $ tree );
119132 } else {
120133 $ root = new File ($ view , $ rootInfo );
121134 }
122- $ objectTree ->init ($ root , $ view , $ this ->mountManager );
135+
136+ if ($ isPublicShare ) {
137+ $ userPrincipalBackend = new Principal (
138+ \OCP \Server::get (IUserManager::class),
139+ \OCP \Server::get (IGroupManager::class),
140+ \OCP \Server::get (IAccountManager::class),
141+ \OCP \Server::get (\OCP \Share \IManager::class),
142+ \OCP \Server::get (IUserSession::class),
143+ \OCP \Server::get (IAppManager::class),
144+ \OCP \Server::get (ProxyMapper::class),
145+ \OCP \Server::get (KnownUserService::class),
146+ \OCP \Server::get (IConfig::class),
147+ \OC ::$ server ->getL10NFactory (),
148+ );
149+
150+ // Mount the share collection at /public.php/dav/shares/<share token>
151+ $ rootCollection ->addChild (new \OCA \DAV \Files \Sharing \RootCollection (
152+ $ root ,
153+ $ userPrincipalBackend ,
154+ 'principals/shares ' ,
155+ ));
156+ } else {
157+ /** @var ObjectTree $tree */
158+ $ tree ->init ($ root , $ view , $ this ->mountManager );
159+ }
123160
124161 $ server ->addPlugin (
125162 new FilesPlugin (
126- $ objectTree ,
163+ $ tree ,
127164 $ this ->config ,
128165 $ this ->request ,
129166 $ this ->previewManager ,
@@ -143,16 +180,16 @@ public function createServer(string $baseUri,
143180 ));
144181
145182 if ($ this ->userSession ->isLoggedIn ()) {
146- $ server ->addPlugin (new TagsPlugin ($ objectTree , $ this ->tagManager , $ this ->eventDispatcher , $ this ->userSession ));
183+ $ server ->addPlugin (new TagsPlugin ($ tree , $ this ->tagManager , $ this ->eventDispatcher , $ this ->userSession ));
147184 $ server ->addPlugin (new SharesPlugin (
148- $ objectTree ,
185+ $ tree ,
149186 $ this ->userSession ,
150187 $ userFolder ,
151188 \OCP \Server::get (\OCP \Share \IManager::class)
152189 ));
153190 $ server ->addPlugin (new CommentPropertiesPlugin (\OCP \Server::get (ICommentsManager::class), $ this ->userSession ));
154191 $ server ->addPlugin (new FilesReportPlugin (
155- $ objectTree ,
192+ $ tree ,
156193 $ view ,
157194 \OCP \Server::get (ISystemTagManager::class),
158195 \OCP \Server::get (ISystemTagObjectMapper::class),
@@ -167,7 +204,7 @@ public function createServer(string $baseUri,
167204 new \Sabre \DAV \PropertyStorage \Plugin (
168205 new CustomPropertiesBackend (
169206 $ server ,
170- $ objectTree ,
207+ $ tree ,
171208 $ this ->databaseConnection ,
172209 $ this ->userSession ->getUser (),
173210 \OCP \Server::get (DefaultCalendarValidator::class),
0 commit comments