-
Notifications
You must be signed in to change notification settings - Fork 12
/
davProperties.ts
223 lines (207 loc) · 5.75 KB
/
davProperties.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**
* @copyright Copyright (c) 2023 John Molakvoæ <skjnldsv@protonmail.com>
*
* @author John Molakvoæ <skjnldsv@protonmail.com>
* @author Ferdinand Thiessen <opensource@fthiessen.de>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
import { getCurrentUser } from '@nextcloud/auth'
import logger from '../utils/logger'
export type DavProperty = { [key: string]: string }
export const defaultDavProperties = [
'd:getcontentlength',
'd:getcontenttype',
'd:getetag',
'd:getlastmodified',
'd:quota-available-bytes',
'd:resourcetype',
'nc:has-preview',
'nc:is-encrypted',
'nc:mount-type',
'nc:share-attributes',
'oc:comments-unread',
'oc:favorite',
'oc:fileid',
'oc:owner-display-name',
'oc:owner-id',
'oc:permissions',
'oc:share-types',
'oc:size',
'ocs:share-permissions',
]
export const defaultDavNamespaces = {
d: 'DAV:',
nc: 'http://nextcloud.org/ns',
oc: 'http://owncloud.org/ns',
ocs: 'http://open-collaboration-services.org/ns',
}
/**
* Register custom DAV properties
*
* Can be used if your app introduces custom DAV properties, so e.g. the files app can make use of it.
*
* @param prop The property
* @param namespace The namespace of the property
*/
export const registerDavProperty = function(prop: string, namespace: DavProperty = { nc: 'http://nextcloud.org/ns' }): boolean {
if (typeof window._nc_dav_properties === 'undefined') {
window._nc_dav_properties = [...defaultDavProperties]
window._nc_dav_namespaces = { ...defaultDavNamespaces }
}
const namespaces = { ...window._nc_dav_namespaces, ...namespace }
// Check duplicates
if (window._nc_dav_properties.find((search) => search === prop)) {
logger.error(`${prop} already registered`, { prop })
return false
}
if (prop.startsWith('<') || prop.split(':').length !== 2) {
logger.error(`${prop} is not valid. See example: 'oc:fileid'`, { prop })
return false
}
const ns = prop.split(':')[0]
if (!namespaces[ns]) {
logger.error(`${prop} namespace unknown`, { prop, namespaces })
return false
}
window._nc_dav_properties.push(prop)
window._nc_dav_namespaces = namespaces
return true
}
/**
* Get the registered dav properties
*/
export const getDavProperties = function(): string {
if (typeof window._nc_dav_properties === 'undefined') {
window._nc_dav_properties = [...defaultDavProperties]
}
return window._nc_dav_properties.map((prop) => `<${prop} />`).join(' ')
}
/**
* Get the registered dav namespaces
*/
export const getDavNameSpaces = function(): string {
if (typeof window._nc_dav_namespaces === 'undefined') {
window._nc_dav_namespaces = { ...defaultDavNamespaces }
}
return Object.keys(window._nc_dav_namespaces)
.map((ns) => `xmlns:${ns}="${window._nc_dav_namespaces?.[ns]}"`)
.join(' ')
}
/**
* Get the default PROPFIND request body
*/
export const davGetDefaultPropfind = function(): string {
return `<?xml version="1.0"?>
<d:propfind ${getDavNameSpaces()}>
<d:prop>
${getDavProperties()}
</d:prop>
</d:propfind>`
}
/**
* Get the REPORT body to filter for favorite nodes
*/
export const davGetFavoritesReport = function(): string {
return `<?xml version="1.0"?>
<oc:filter-files ${getDavNameSpaces()}>
<d:prop>
${getDavProperties()}
</d:prop>
<oc:filter-rules>
<oc:favorite>1</oc:favorite>
</oc:filter-rules>
</oc:filter-files>`
}
/**
* Get the SEARCH body to search for recently modified files
*
* @param lastModified Oldest timestamp to include (Unix timestamp)
* @example
* ```ts
* // SEARCH for recent files need a different DAV endpoint
* const client = davGetClient(generateRemoteUrl('dav'))
* // Timestamp of last week
* const lastWeek = Math.round(Date.now() / 1000) - (60 * 60 * 24 * 7)
* const contentsResponse = await client.getDirectoryContents(path, {
* details: true,
* data: davGetRecentSearch(lastWeek),
* headers: {
* method: 'SEARCH',
* 'Content-Type': 'application/xml; charset=utf-8',
* },
* deep: true,
* }) as ResponseDataDetailed<FileStat[]>
* ```
*/
export const davGetRecentSearch = function(lastModified: number): string {
return `<?xml version="1.0" encoding="UTF-8"?>
<d:searchrequest ${getDavNameSpaces()}
xmlns:ns="https://github.com/icewind1991/SearchDAV/ns">
<d:basicsearch>
<d:select>
<d:prop>
${getDavProperties()}
</d:prop>
</d:select>
<d:from>
<d:scope>
<d:href>/files/${getCurrentUser()?.uid}/</d:href>
<d:depth>infinity</d:depth>
</d:scope>
</d:from>
<d:where>
<d:and>
<d:or>
<d:not>
<d:eq>
<d:prop>
<d:getcontenttype/>
</d:prop>
<d:literal>httpd/unix-directory</d:literal>
</d:eq>
</d:not>
<d:eq>
<d:prop>
<oc:size/>
</d:prop>
<d:literal>0</d:literal>
</d:eq>
</d:or>
<d:gt>
<d:prop>
<d:getlastmodified/>
</d:prop>
<d:literal>${lastModified}</d:literal>
</d:gt>
</d:and>
</d:where>
<d:orderby>
<d:order>
<d:prop>
<d:getlastmodified/>
</d:prop>
<d:descending/>
</d:order>
</d:orderby>
<d:limit>
<d:nresults>100</d:nresults>
<ns:firstresult>0</ns:firstresult>
</d:limit>
</d:basicsearch>
</d:searchrequest>`
}