-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathgroupfolders.ts
97 lines (83 loc) Β· 2.5 KB
/
groupfolders.ts
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
/**
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import type { DAVResultResponseProps, FileStat, ResponseDataDetailed } from 'webdav'
import { getCurrentUser } from '@nextcloud/auth'
import { File, Folder, Permission } from '@nextcloud/files'
import { generateRemoteUrl, generateUrl } from '@nextcloud/router'
import client, { rootPath } from './client'
type ContentsWithRoot = {
folder: Folder,
contents: (Folder | File)[]
}
interface Props extends DAVResultResponseProps {
permissions: string
fileid: number
size: number
'mount-point': string
}
const data = `<?xml version="1.0"?>
<d:propfind xmlns:d="DAV:"
xmlns:oc="http://owncloud.org/ns"
xmlns:nc="http://nextcloud.org/ns">
<d:prop>
<d:getcontentlength />
<d:getcontenttype />
<d:getetag />
<d:getlastmodified />
<d:resourcetype />
<oc:fileid />
<oc:owner-id />
<oc:permissions />
<oc:size />
<nc:has-preview />
<nc:mount-point />
</d:prop>
</d:propfind>`
const resultToNode = function(node: FileStat): File | Folder {
const props = node.props as Props
// force no permissions as we just want one action: to redirect to files
// TODO: implement real navigation with full support of files actions
const permissions = Permission.NONE
const owner = getCurrentUser()?.uid as string
const previewUrl = generateUrl('/core/preview?fileId={fileid}&x=32&y=32&forceIcon=0', node.props)
const mountPoint = (props?.['mount-point'] || '').replace(`/files/${getCurrentUser()?.uid}`, '')
const nodeData = {
id: props?.fileid || 0,
source: generateRemoteUrl('dav' + rootPath + node.filename),
mtime: new Date(node.lastmod),
mime: node.mime as string,
size: props?.size || 0,
permissions,
owner,
root: rootPath,
attributes: {
...node,
...node.props,
'mount-type': 'group',
mountPoint,
previewUrl,
},
}
delete nodeData.attributes.props
return node.type === 'file'
? new File(nodeData)
: new Folder(nodeData)
}
export const getContents = async (path = '/'): Promise<ContentsWithRoot> => {
const contentsResponse = await client.getDirectoryContents(path, {
details: true,
data,
includeSelf: true,
}) as ResponseDataDetailed<FileStat[]>
const root = contentsResponse.data.find(node => node.filename === path)
if (!root) {
throw new Error('Could not find root in response')
}
const contents = contentsResponse.data.filter(node => node !== root)
return {
folder: resultToNode(root) as Folder,
contents: contents.map(resultToNode),
}
}