-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathnodeData.ts
More file actions
214 lines (173 loc) · 5.28 KB
/
nodeData.ts
File metadata and controls
214 lines (173 loc) · 5.28 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
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
/**
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { join } from '@nextcloud/paths'
import { Permission } from '../permissions'
import { NodeStatus } from './node'
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export interface Attribute { [key: string]: any }
export interface NodeData {
/**
* URL to the resource.
* e.g. https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg
* or https://domain.com/Photos/picture.jpg
*/
source: string
/**
* The absolute root of the home relative to the service.
* It is highly recommended to provide that information.
* e.g. /files/emma
*/
root: string
/** Unique ID */
id?: number
/** Last modified time */
mtime?: Date
/** Creation time */
crtime?: Date
/** The mime type Optional for folders only */
mime?: string
/** The node size type */
size?: number
/** The node permissions */
permissions?: Permission
/** The owner UID of this node */
owner: string|null
/** Optional the displayname of this node */
displayname?: string
/** The node attributes */
attributes?: Attribute
/** The node status */
status?: NodeStatus
}
/**
* Check if a node source is from a specific DAV service
*
* @param source The source to check
* @param davService Pattern to check if source is DAV resource
*/
export const isDavResource = function(source: string, davService: RegExp): boolean {
return source.match(davService) !== null
}
/**
* Validate Node construct data
*
* @param data The node data
* @param davService Pattern to check if source is DAV ressource
*/
export function validateData(data: NodeData, davService: RegExp) {
if (data.id && typeof data.id !== 'number') {
throw new Error('Invalid id type of value')
}
if (!data.source) {
throw new Error('Missing mandatory source')
}
try {
// eslint-disable-next-line no-new
new URL(data.source)
} catch (e) {
throw new Error('Invalid source format, source must be a valid URL')
}
if (!data.source.startsWith('http')) {
throw new Error('Invalid source format, only http(s) is supported')
}
if (!data.root) {
throw new Error('Missing mandatory root')
}
if (typeof data.root !== 'string') {
throw new Error('Invalid root type')
}
if (!data.root.startsWith('/')) {
throw new Error('Root must start with a leading slash')
}
if (!data.source.includes(data.root)) {
throw new Error('Root must be part of the source')
}
if (isDavResource(data.source, davService)) {
const service = data.source.match(davService)![0]
if (!data.source.includes(join(service, data.root))) {
throw new Error('The root must be relative to the service. e.g /files/emma')
}
}
if (data.displayname && typeof data.displayname !== 'string') {
throw new Error('Invalid displayname type')
}
if (data.mtime && !(data.mtime instanceof Date)) {
throw new Error('Invalid mtime type')
}
if (data.crtime && !(data.crtime instanceof Date)) {
throw new Error('Invalid crtime type')
}
if (!data.mime || typeof data.mime !== 'string'
|| !data.mime.match(/^[-\w.]+\/[-+\w.]+$/gi)) {
throw new Error('Missing or invalid mandatory mime')
}
// Allow size to be 0
if ('size' in data && typeof data.size !== 'number' && data.size !== undefined) {
throw new Error('Invalid size type')
}
// Allow permissions to be 0
if ('permissions' in data
&& data.permissions !== undefined
&& !(typeof data.permissions === 'number'
&& data.permissions >= Permission.NONE
&& data.permissions <= Permission.ALL
)) {
throw new Error('Invalid permissions')
}
if (data.owner
&& data.owner !== null
&& typeof data.owner !== 'string') {
throw new Error('Invalid owner type')
}
if (data.attributes && typeof data.attributes !== 'object') {
throw new Error('Invalid attributes type')
}
if (data.status && !Object.values(NodeStatus).includes(data.status)) {
throw new Error('Status must be a valid NodeStatus')
}
}
/**
* In case we try to create a node from deserialized data,
* we need to fix date types.
*
* @param data The internal node data
*/
export const fixDates = (data: NodeData) => {
if (data.mtime && typeof data.mtime === 'string') {
if (!isNaN(Date.parse(data.mtime))
&& JSON.stringify(new Date(data.mtime)) === JSON.stringify(data.mtime)) {
data.mtime = new Date(data.mtime)
}
}
if (data.crtime && typeof data.crtime === 'string') {
if (!isNaN(Date.parse(data.crtime))
&& JSON.stringify(new Date(data.crtime)) === JSON.stringify(data.crtime)) {
data.crtime = new Date(data.crtime)
}
}
}
/**
* Fix a RegExp pattern from string or RegExp to RegExp
*
* @param pattern The pattern as string or RegExp
*/
export const fixRegExp = (pattern: string | RegExp): RegExp => {
if (pattern instanceof RegExp) {
return pattern
}
// Extract the pattern and flags if it's a string
// Pulled from https://www.npmjs.com/package/regex-parser
const matches = pattern.match(/(\/?)(.+)\1([a-z]*)/i)
// If there's no match, throw an error
if (!matches) {
throw new Error('Invalid regular expression format.')
}
// Filter valid flags: 'g', 'i', 'm', 's', 'u', and 'y'
const validFlags = Array.from(new Set(matches[3]))
.filter((flag) => 'gimsuy'.includes(flag))
.join('')
// Create the regular expression
return new RegExp(matches[2], validFlags)
}