mirror of
https://github.com/nextcloud/server.git
synced 2026-02-18 18:28:50 -05:00
feat(files): allow to include parents in folder tree API
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
This commit is contained in:
parent
dd2c8d93ce
commit
17ed55ff56
4 changed files with 54 additions and 4 deletions
|
|
@ -275,11 +275,38 @@ class ApiController extends Controller {
|
|||
return $children;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all parents with their contents of the current folder.
|
||||
*
|
||||
* @param Folder $currentFolder - The current folder to get the parents for
|
||||
* @param string $root - The root path to stop at
|
||||
* @param array $children - The children of the current folder to include in the response
|
||||
*/
|
||||
private function getParents(Folder $currentFolder, string $root, array $children): array {
|
||||
$parentFolder = $currentFolder->getParent();
|
||||
$parentContent = array_filter($parentFolder->getDirectoryListing(), fn (Node $node) => $node instanceof Folder);
|
||||
$parentData = array_map(fn (Folder $node) => [
|
||||
'id' => $node->getId(),
|
||||
'basename' => basename($node->getPath()),
|
||||
'displayName' => $node->getName(),
|
||||
'children' => $node->getId() === $currentFolder->getId()
|
||||
? $children
|
||||
: [],
|
||||
], $parentContent);
|
||||
|
||||
if ($parentFolder->getPath() === $root) {
|
||||
return array_values($parentData);
|
||||
}
|
||||
|
||||
return $this->getParents($parentFolder, $root, array_values($parentData));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the folder tree of the user
|
||||
*
|
||||
* @param string $path The path relative to the user folder
|
||||
* @param int $depth The depth of the tree
|
||||
* @param bool $withParents Whether to include parent folders in the response
|
||||
*
|
||||
* @return JSONResponse<Http::STATUS_OK, FilesFolderTree, array{}>|JSONResponse<Http::STATUS_UNAUTHORIZED|Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND, array{message: string}, array{}>
|
||||
*
|
||||
|
|
@ -291,7 +318,7 @@ class ApiController extends Controller {
|
|||
#[NoAdminRequired]
|
||||
#[ApiRoute(verb: 'GET', url: '/api/v1/folder-tree')]
|
||||
#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT)]
|
||||
public function getFolderTree(string $path = '/', int $depth = 1): JSONResponse {
|
||||
public function getFolderTree(string $path = '/', int $depth = 1, bool $withParents = false): JSONResponse {
|
||||
$user = $this->userSession->getUser();
|
||||
if (!($user instanceof IUser)) {
|
||||
return new JSONResponse([
|
||||
|
|
@ -310,6 +337,10 @@ class ApiController extends Controller {
|
|||
}
|
||||
$nodes = $node->getDirectoryListing();
|
||||
$tree = $this->getChildren($nodes, $depth);
|
||||
|
||||
if ($withParents && $path !== '/') {
|
||||
$tree = $this->getParents($node, $userFolderPath, $tree);
|
||||
}
|
||||
} catch (NotFoundException $e) {
|
||||
return new JSONResponse([
|
||||
'message' => $this->l10n->t('Folder not found'),
|
||||
|
|
|
|||
|
|
@ -2647,6 +2647,15 @@
|
|||
"default": 1
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "withParents",
|
||||
"in": "query",
|
||||
"description": "Whether to include parent folders in the response",
|
||||
"schema": {
|
||||
"type": "boolean",
|
||||
"default": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "OCS-APIRequest",
|
||||
"in": "header",
|
||||
|
|
|
|||
|
|
@ -80,12 +80,13 @@ function getTreeNodes(tree: Tree, currentPath: string = '/', nodes: TreeNode[] =
|
|||
*
|
||||
* @param path - The path to get the tree from
|
||||
* @param depth - The depth to fetch
|
||||
* @param withParents - Whether to include parent folders in the response
|
||||
*/
|
||||
export async function getFolderTreeNodes(path: string = '/', depth: number = 1): Promise<TreeNode[]> {
|
||||
export async function getFolderTreeNodes(path: string = '/', depth: number = 1, withParents = false): Promise<TreeNode[]> {
|
||||
const { data: tree } = await axios.get<Tree>(generateOcsUrl('/apps/files/api/v1/folder-tree'), {
|
||||
params: new URLSearchParams({ path, depth: String(depth) }),
|
||||
params: new URLSearchParams({ path, depth: String(depth), withParents: String(withParents) }),
|
||||
})
|
||||
const nodes = getTreeNodes(tree, path)
|
||||
const nodes = getTreeNodes(tree, withParents ? '/' : path)
|
||||
return nodes
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -23057,6 +23057,15 @@
|
|||
"default": 1
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "withParents",
|
||||
"in": "query",
|
||||
"description": "Whether to include parent folders in the response",
|
||||
"schema": {
|
||||
"type": "boolean",
|
||||
"default": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "OCS-APIRequest",
|
||||
"in": "header",
|
||||
|
|
|
|||
Loading…
Reference in a new issue