11<?php
22
3+ declare (strict_types=1 );
4+
35/**
46 * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
57 * SPDX-License-Identifier: AGPL-3.0-or-later
68 */
79
8- declare (strict_types=1 );
9- /**
10- * @copyright Copyright (c) 2019 Julius Härtl <jus@bitgrid.net>
11- *
12- * @author Julius Härtl <jus@bitgrid.net>
13- *
14- * @license GNU AGPL version 3 or any later version
15- *
16- * This program is free software: you can redistribute it and/or modify
17- * it under the terms of the GNU Affero General Public License as
18- * published by the Free Software Foundation, either version 3 of the
19- * License, or (at your option) any later version.
20- *
21- * This program is distributed in the hope that it will be useful,
22- * but WITHOUT ANY WARRANTY; without even the implied warranty of
23- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24- * GNU Affero General Public License for more details.
25- *
26- * You should have received a copy of the GNU Affero General Public License
27- * along with this program. If not, see <http://www.gnu.org/licenses/>.
28- *
29- */
30-
3110namespace OCA \Text \Controller ;
3211
3312use Exception ;
4322use OCP \DirectEditing \IManager as IDirectEditingManager ;
4423use OCP \DirectEditing \RegisterDirectEditorEvent ;
4524use OCP \EventDispatcher \IEventDispatcher ;
46- use OCP \Files \File ;
4725use OCP \Files \Folder ;
4826use OCP \Files \IRootFolder ;
4927use OCP \Files \NotFoundException ;
@@ -73,12 +51,16 @@ public function __construct(
7351 }
7452
7553 /**
76- * Checks for available files in the current folder and returns required details to present
77- * the rich workspace
54+ * Checks for available files in the current folder and returns required
55+ * details to present the rich workspace.
56+ *
57+ * Returns 200 with file metadata and folder permissions if a README is found,
58+ * or 404 with folder permissions if not (so the client can still offer to create one).
59+ *
60+ * @param string $path Path relative to the user's root folder
7861 */
7962 #[NoAdminRequired]
8063 public function folder (string $ path = '/ ' ): DataResponse {
81- /** */
8264 try {
8365 /** @psalm-suppress PossiblyNullArgument */
8466 $ userFolder = $ this ->rootFolder ->getUserFolder ($ this ->userId );
@@ -117,9 +99,11 @@ public function folder(string $path = '/'): DataResponse {
11799 }
118100
119101 /**
120- * Checks for available files in the current folder and returns required details to present
121- * the rich workspace
122- * @api
102+ * Checks for available files in a publicly shared folder and returns required
103+ * details to present the rich workspace.
104+ *
105+ * @param string $shareToken Public share token
106+ * @param string $path Path relative to the share root
123107 */
124108 #[NoAdminRequired]
125109 #[PublicPage]
@@ -166,6 +150,14 @@ public function publicFolder(string $shareToken, string $path = '/'): DataRespon
166150 return new DataResponse (['message ' => 'No workspace file found ' ], Http::STATUS_NOT_FOUND );
167151 }
168152
153+ /**
154+ * Returns a direct editing URL for the workspace README in the given folder.
155+ *
156+ * If a README file already exists, opens it for editing. If not, creates a new
157+ * file using the first entry of getSupportedFilenames() as the default name.
158+ *
159+ * @param string $path Path to the folder, relative to the user's root folder
160+ */
169161 #[NoAdminRequired]
170162 public function direct (string $ path ): DataResponse {
171163 $ this ->eventDispatcher ->dispatchTyped (new RegisterDirectEditorEvent ($ this ->directEditingManager ));
@@ -174,9 +166,13 @@ public function direct(string $path): DataResponse {
174166 /** @psalm-suppress PossiblyNullArgument */
175167 $ folder = $ this ->rootFolder ->getUserFolder ($ this ->userId )->get ($ path );
176168 if ($ folder instanceof Folder) {
177- $ file = $ this ->getFile ($ folder );
169+ $ file = $ this ->workspaceService -> getFile ($ folder );
178170 if ($ file === null ) {
179- $ token = $ this ->directEditingManager ->create ($ path . '/ ' . $ this ->workspaceService ->getSupportedFilenames ()[0 ], Application::APP_NAME , TextDocumentCreator::CREATOR_ID );
171+ $ token = $ this ->directEditingManager ->create (
172+ $ path . '/ ' . $ this ->workspaceService ->getSupportedFilenames ()[0 ],
173+ Application::APP_NAME ,
174+ TextDocumentCreator::CREATOR_ID
175+ );
180176 } else {
181177 $ token = $ this ->directEditingManager ->open ($ path . '/ ' . $ file ->getName (), Application::APP_NAME );
182178 }
@@ -191,18 +187,4 @@ public function direct(string $path): DataResponse {
191187
192188 return new DataResponse (['message ' => 'No workspace file found ' ], Http::STATUS_NOT_FOUND );
193189 }
194-
195- private function getFile (Folder $ folder ): ?File {
196- $ file = null ;
197- foreach ($ this ->workspaceService ->getSupportedFilenames () as $ filename ) {
198- try {
199- $ node = $ folder ->get ($ filename );
200- if ($ node instanceof File) {
201- $ file = $ node ;
202- }
203- } catch (NotFoundException ) {
204- }
205- }
206- return $ file ;
207- }
208190}
0 commit comments