-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Expand file tree
/
Copy pathFolderTree.ts
More file actions
116 lines (102 loc) · 3.08 KB
/
Copy pathFolderTree.ts
File metadata and controls
116 lines (102 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import type { ContentsWithRoot } from '@nextcloud/files'
import { getCurrentUser } from '@nextcloud/auth'
import axios from '@nextcloud/axios'
import { getRemoteURL } from '@nextcloud/files/dav'
import { getCanonicalLocale, getLanguage } from '@nextcloud/l10n'
import { dirname, encodePath, join } from '@nextcloud/paths'
import { generateOcsUrl } from '@nextcloud/router'
import { getContents as getFiles } from './Files.ts'
type Tree = TreeNodeData[]
interface TreeNodeData {
id: number
basename: string
displayName?: string
children: Tree
}
export interface TreeNode {
source: string
encodedSource: string
path: string
fileid: number
basename: string
displayName?: string
}
export const folderTreeId = 'folders'
export const sourceRoot = `${getRemoteURL()}/files/${getCurrentUser()?.uid}`
const collator = Intl.Collator(
[getLanguage(), getCanonicalLocale()],
{
numeric: true,
usage: 'sort',
},
)
const compareNodes = (a: TreeNodeData, b: TreeNodeData) => collator.compare(a.displayName ?? a.basename, b.displayName ?? b.basename)
/**
* Get all tree nodes recursively
*
* @param tree - The tree to process
* @param currentPath - The current path
* @param nodes - The nodes collected so far
*/
function getTreeNodes(tree: Tree, currentPath: string = '/', nodes: TreeNode[] = []): TreeNode[] {
const sortedTree = tree.toSorted(compareNodes)
for (const { id, basename, displayName, children } of sortedTree) {
const path = join(currentPath, basename)
const source = `${sourceRoot}${path}`
const node: TreeNode = {
source,
encodedSource: encodeSource(source),
path,
fileid: id,
basename,
}
if (displayName) {
node.displayName = displayName
}
nodes.push(node)
if (children.length > 0) {
getTreeNodes(children, path, nodes)
}
}
return nodes
}
/**
* Get folder tree nodes
*
* @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, 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), withParents: String(withParents) }),
})
const nodes = getTreeNodes(tree, withParents ? '/' : path)
return nodes
}
export const getContents = (path: string, options: { signal: AbortSignal }): Promise<ContentsWithRoot> => getFiles(path, options)
/**
* Encode source URL
*
* @param source - The source URL
*/
export function encodeSource(source: string): string {
const { origin } = new URL(source)
return origin + encodePath(source.slice(origin.length))
}
/**
* Get parent source URL
*
* @param source - The source URL
*/
export function getSourceParent(source: string): string {
const parent = dirname(source)
if (parent === sourceRoot) {
return folderTreeId
}
return `${folderTreeId}::${encodeSource(parent)}`
}