@@ -275,11 +275,38 @@ private function getChildren(array $nodes, int $depth = 1, int $currentDepth = 0
275275 return $ children ;
276276 }
277277
278+ /**
279+ * Get all parents with their contents of the current folder.
280+ *
281+ * @param Folder $currentFolder - The current folder to get the parents for
282+ * @param string $root - The root path to stop at
283+ * @param array $children - The children of the current folder to include in the response
284+ */
285+ private function getParents (Folder $ currentFolder , string $ root , array $ children ): array {
286+ $ parentFolder = $ currentFolder ->getParent ();
287+ $ parentContent = array_filter ($ parentFolder ->getDirectoryListing (), fn (Node $ node ) => $ node instanceof Folder);
288+ $ parentData = array_map (fn (Folder $ node ) => [
289+ 'id ' => $ node ->getId (),
290+ 'basename ' => basename ($ node ->getPath ()),
291+ 'displayName ' => $ node ->getName (),
292+ 'children ' => $ node ->getId () === $ currentFolder ->getId ()
293+ ? $ children
294+ : [],
295+ ], $ parentContent );
296+
297+ if ($ parentFolder ->getPath () === $ root ) {
298+ return array_values ($ parentData );
299+ }
300+
301+ return $ this ->getParents ($ parentFolder , $ root , array_values ($ parentData ));
302+ }
303+
278304 /**
279305 * Returns the folder tree of the user
280306 *
281307 * @param string $path The path relative to the user folder
282308 * @param int $depth The depth of the tree
309+ * @param bool $withParents Whether to include parent folders in the response
283310 *
284311 * @return JSONResponse<Http::STATUS_OK, FilesFolderTree, array{}>|JSONResponse<Http::STATUS_UNAUTHORIZED|Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND, array{message: string}, array{}>
285312 *
@@ -291,7 +318,7 @@ private function getChildren(array $nodes, int $depth = 1, int $currentDepth = 0
291318 #[NoAdminRequired]
292319 #[ApiRoute(verb: 'GET ' , url: '/api/v1/folder-tree ' )]
293320 #[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT )]
294- public function getFolderTree (string $ path = '/ ' , int $ depth = 1 ): JSONResponse {
321+ public function getFolderTree (string $ path = '/ ' , int $ depth = 1 , bool $ withParents = false ): JSONResponse {
295322 $ user = $ this ->userSession ->getUser ();
296323 if (!($ user instanceof IUser)) {
297324 return new JSONResponse ([
@@ -310,6 +337,10 @@ public function getFolderTree(string $path = '/', int $depth = 1): JSONResponse
310337 }
311338 $ nodes = $ node ->getDirectoryListing ();
312339 $ tree = $ this ->getChildren ($ nodes , $ depth );
340+
341+ if ($ withParents && $ path !== '/ ' ) {
342+ $ tree = $ this ->getParents ($ node , $ userFolderPath , $ tree );
343+ }
313344 } catch (NotFoundException $ e ) {
314345 return new JSONResponse ([
315346 'message ' => $ this ->l10n ->t ('Folder not found ' ),
0 commit comments