1919use OCP \AppFramework \Http \Attribute \NoAdminRequired ;
2020use OCP \AppFramework \Http \Attribute \PasswordConfirmationRequired ;
2121use OCP \AppFramework \Http \DataResponse ;
22+ use OCP \AppFramework \OCS \OCSBadRequestException ;
2223use OCP \AppFramework \OCS \OCSForbiddenException ;
2324use OCP \AppFramework \OCS \OCSNotFoundException ;
2425use OCP \AppFramework \OCSController ;
@@ -164,12 +165,28 @@ public function getFolder(int $id): DataResponse {
164165 private function checkFolderExists (int $ id ): ?DataResponse {
165166 $ storageId = $ this ->getRootFolderStorageId ();
166167 if ($ storageId === null ) {
167- return new DataResponse ([], Http:: STATUS_NOT_FOUND );
168+ throw new OCSNotFoundException ( ' Groupfolder not found ' );
168169 }
169170
170171 $ folder = $ this ->manager ->getFolder ($ id , $ storageId );
171172 if ($ folder === null ) {
172- return new DataResponse ([], Http::STATUS_NOT_FOUND );
173+ throw new OCSNotFoundException ('Groupfolder not found ' );
174+ }
175+
176+ return null ;
177+ }
178+
179+ private function checkMountPointExists (string $ mountpoint ): ?DataResponse {
180+ $ storageId = $ this ->getRootFolderStorageId ();
181+ if ($ storageId === null ) {
182+ throw new OCSNotFoundException ('Groupfolder not found ' );
183+ }
184+
185+ $ folders = $ this ->manager ->getAllFolders ();
186+ foreach ($ folders as $ folder ) {
187+ if ($ folder ['mount_point ' ] === $ mountpoint ) {
188+ throw new OCSBadRequestException ('Mount point already exists ' );
189+ }
173190 }
174191
175192 return null ;
@@ -185,6 +202,7 @@ private function getRootFolderStorageId(): ?int {
185202 * @param string $mountpoint Mountpoint of the new Groupfolder
186203 * @return DataResponse<Http::STATUS_OK, GroupFoldersFolder, array{}>
187204 * @throws OCSNotFoundException Groupfolder not found
205+ * @throws OCSBadRequestException Folder already exists
188206 *
189207 * 200: Groupfolder added successfully
190208 */
@@ -193,12 +211,13 @@ private function getRootFolderStorageId(): ?int {
193211 #[NoAdminRequired]
194212 #[FrontpageRoute(verb: 'POST ' , url: '/folders ' )]
195213 public function addFolder (string $ mountpoint ): DataResponse {
196-
197214 $ storageId = $ this ->rootFolder ->getMountPoint ()->getNumericStorageId ();
198215 if ($ storageId === null ) {
199216 throw new OCSNotFoundException ();
200217 }
201218
219+ $ this ->checkMountPointExists (trim ($ mountpoint ));
220+
202221 $ id = $ this ->manager ->createFolder (trim ($ mountpoint ));
203222 $ folder = $ this ->manager ->getFolder ($ id , $ storageId );
204223 if ($ folder === null ) {
@@ -223,10 +242,7 @@ public function addFolder(string $mountpoint): DataResponse {
223242 #[NoAdminRequired]
224243 #[FrontpageRoute(verb: 'DELETE ' , url: '/folders/{id} ' )]
225244 public function removeFolder (int $ id ): DataResponse {
226- $ response = $ this ->checkFolderExists ($ id );
227- if ($ response ) {
228- return $ response ;
229- }
245+ $ this ->checkFolderExists ($ id );
230246
231247 $ folder = $ this ->mountProvider ->getFolder ($ id );
232248 if ($ folder === null ) {
@@ -245,6 +261,8 @@ public function removeFolder(int $id): DataResponse {
245261 * @param int $id ID of the Groupfolder
246262 * @param string $mountPoint New mount point path
247263 * @return DataResponse<Http::STATUS_OK, array{success: true, folder: GroupFoldersFolder}, array{}>
264+ * @throws OCSNotFoundException Groupfolder not found
265+ * @throws OCSBadRequestException Mount point already exists
248266 *
249267 * 200: Mount point changed successfully
250268 */
@@ -255,6 +273,9 @@ public function removeFolder(int $id): DataResponse {
255273 public function setMountPoint (int $ id , string $ mountPoint ): DataResponse {
256274 $ this ->manager ->renameFolder ($ id , trim ($ mountPoint ));
257275
276+ $ this ->checkFolderExists ($ id );
277+ $ this ->checkMountPointExists (trim ($ mountPoint ));
278+
258279 $ folder = $ this ->manager ->getFolder ($ id );
259280 if ($ folder === null ) {
260281 throw new OCSNotFoundException ();
@@ -278,10 +299,7 @@ public function setMountPoint(int $id, string $mountPoint): DataResponse {
278299 #[NoAdminRequired]
279300 #[FrontpageRoute(verb: 'POST ' , url: '/folders/{id}/groups ' )]
280301 public function addGroup (int $ id , string $ group ): DataResponse {
281- $ response = $ this ->checkFolderExists ($ id );
282- if ($ response ) {
283- return $ response ;
284- }
302+ $ this ->checkFolderExists ($ id );
285303
286304 $ this ->manager ->addApplicableGroup ($ id , $ group );
287305
@@ -308,10 +326,7 @@ public function addGroup(int $id, string $group): DataResponse {
308326 #[NoAdminRequired]
309327 #[FrontpageRoute(verb: 'DELETE ' , url: '/folders/{id}/groups/{group} ' , requirements: ['group ' => '.+ ' ])]
310328 public function removeGroup (int $ id , string $ group ): DataResponse {
311- $ response = $ this ->checkFolderExists ($ id );
312- if ($ response ) {
313- return $ response ;
314- }
329+ $ this ->checkFolderExists ($ id );
315330
316331 $ this ->manager ->removeApplicableGroup ($ id , $ group );
317332
@@ -339,10 +354,7 @@ public function removeGroup(int $id, string $group): DataResponse {
339354 #[NoAdminRequired]
340355 #[FrontpageRoute(verb: 'POST ' , url: '/folders/{id}/groups/{group} ' , requirements: ['group ' => '.+ ' ])]
341356 public function setPermissions (int $ id , string $ group , int $ permissions ): DataResponse {
342- $ response = $ this ->checkFolderExists ($ id );
343- if ($ response ) {
344- return $ response ;
345- }
357+ $ this ->checkFolderExists ($ id );
346358
347359 $ this ->manager ->setGroupPermissions ($ id , $ group , $ permissions );
348360
@@ -371,10 +383,7 @@ public function setPermissions(int $id, string $group, int $permissions): DataRe
371383 #[NoAdminRequired]
372384 #[FrontpageRoute(verb: 'POST ' , url: '/folders/{id}/manageACL ' )]
373385 public function setManageACL (int $ id , string $ mappingType , string $ mappingId , bool $ manageAcl ): DataResponse {
374- $ response = $ this ->checkFolderExists ($ id );
375- if ($ response ) {
376- return $ response ;
377- }
386+ $ this ->checkFolderExists ($ id );
378387
379388 $ this ->manager ->setManageACL ($ id , $ mappingType , $ mappingId , $ manageAcl );
380389
@@ -401,10 +410,7 @@ public function setManageACL(int $id, string $mappingType, string $mappingId, bo
401410 #[NoAdminRequired]
402411 #[FrontpageRoute(verb: 'POST ' , url: '/folders/{id}/quota ' )]
403412 public function setQuota (int $ id , int $ quota ): DataResponse {
404- $ response = $ this ->checkFolderExists ($ id );
405- if ($ response ) {
406- return $ response ;
407- }
413+ $ this ->checkFolderExists ($ id );
408414
409415 $ this ->manager ->setFolderQuota ($ id , $ quota );
410416
@@ -431,10 +437,7 @@ public function setQuota(int $id, int $quota): DataResponse {
431437 #[NoAdminRequired]
432438 #[FrontpageRoute(verb: 'POST ' , url: '/folders/{id}/acl ' )]
433439 public function setACL (int $ id , bool $ acl ): DataResponse {
434- $ response = $ this ->checkFolderExists ($ id );
435- if ($ response ) {
436- return $ response ;
437- }
440+ $ this ->checkFolderExists ($ id );
438441
439442 $ this ->manager ->setFolderACL ($ id , $ acl );
440443
@@ -452,6 +455,8 @@ public function setACL(int $id, bool $acl): DataResponse {
452455 * @param int $id ID of the Groupfolder
453456 * @param string $mountpoint New Mountpoint of the Groupfolder
454457 * @return DataResponse<Http::STATUS_OK, array{success: true, folder: GroupFoldersFolder}, array{}>|DataResponse<Http::STATUS_NOT_FOUND, list<empty>, array{}>
458+ * @throws OCSNotFoundException Groupfolder not found
459+ * @throws OCSBadRequestException Mount point already exists or invalid mount point provided
455460 *
456461 * 200: Groupfolder renamed successfully
457462 * 404: Groupfolder not found
@@ -461,13 +466,27 @@ public function setACL(int $id, bool $acl): DataResponse {
461466 #[NoAdminRequired]
462467 #[FrontpageRoute(verb: 'POST ' , url: '/folders/{id}/mountpoint ' )]
463468 public function renameFolder (int $ id , string $ mountpoint ): DataResponse {
464- $ response = $ this ->checkFolderExists ($ id );
465- if ($ response ) {
466- return $ response ;
469+ $ this ->checkFolderExists ($ id );
470+
471+ // Check if the new mountpoint is valid
472+ if (empty ($ mountpoint )) {
473+ throw new OCSBadRequestException ('Mount point cannot be empty ' );
474+ }
475+
476+ // Check if we actually need to do anything
477+ $ folder = $ this ->manager ->getFolder ($ id );
478+ if ($ folder === null ) {
479+ throw new OCSNotFoundException ();
480+ }
481+
482+ if ($ folder ['mount_point ' ] === trim ($ mountpoint )) {
483+ return new DataResponse (['success ' => true , 'folder ' => $ this ->formatFolder ($ folder )]);
467484 }
468485
486+ $ this ->checkMountPointExists (trim ($ mountpoint ));
469487 $ this ->manager ->renameFolder ($ id , trim ($ mountpoint ));
470488
489+ // Get the new folder data
471490 $ folder = $ this ->manager ->getFolder ($ id );
472491 if ($ folder === null ) {
473492 throw new OCSNotFoundException ();
0 commit comments