summaryrefslogtreecommitdiffstats
path: root/apps/files/src/services/DavProperties.ts
blob: 598807511caac61449d64cbc7cb4d6301c5f3fae (plain)
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
/**
 * @copyright Copyright (c) 2023 John Molakvoæ <skjnldsv@protonmail.com>
 *
 * @author John Molakvoæ <skjnldsv@protonmail.com>
 *
 * @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 logger from '../logger'

type DavProperty = { [key: string]: string }

declare global {
	interface Window {
		OC: any;
		_nc_dav_properties: string[];
		_nc_dav_namespaces: DavProperty;
	}
}

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',
]

const defaultDavNamespaces = {
	d: 'DAV:',
	nc: 'http://nextcloud.org/ns',
	oc: 'http://owncloud.org/ns',
	ocs: 'http://open-collaboration-services.org/ns',
}

/**
 * TODO: remove and move to @nextcloud/files
 */
export const registerDavProperty = function(prop: string, namespace: DavProperty = { nc: 'http://nextcloud.org/ns' }): void {
	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
	}

	if (prop.startsWith('<') || prop.split(':').length !== 2) {
		logger.error(`${prop} is not valid. See example: 'oc:fileid'`, { prop })
		return
	}

	const ns = prop.split(':')[0]
	if (!namespaces[ns]) {
		logger.error(`${prop} namespace unknown`, { prop, namespaces })
		return
	}

	window._nc_dav_properties.push(prop)
	window._nc_dav_namespaces = namespaces
}

/**
 * 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 payload
 */
export const getDefaultPropfind = function() {
	return `<?xml version="1.0"?>
		<d:propfind ${getDavNameSpaces()}>
			<d:prop>
				${getDavProperties()}
			</d:prop>
		</d:propfind>`
}