-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathnodeData.ts
More file actions
188 lines (152 loc) · 4.65 KB
/
nodeData.ts
File metadata and controls
188 lines (152 loc) · 4.65 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
/**
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { join } from 'path'
import RegexParser from 'regex-parser'
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 {
/** Unique ID */
id?: number
/**
* 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
/** 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 absolute root of the home relative to the service.
* It is highly recommended to provide that information.
* e.g. /files/emma
*/
root?: string
/** 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 const 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.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.root && typeof data.root !== 'string') {
throw new Error('Invalid root type')
}
if (data.root && !data.root.startsWith('/')) {
throw new Error('Root must start with a leading slash')
}
if (data.root && !data.source.includes(data.root)) {
throw new Error('Root must be part of the source')
}
if (data.root && 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.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.
*/
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)
}
}
}
export const fixRegExp = (pattern: string | RegExp): RegExp => {
if (typeof pattern === 'string') {
return RegexParser(pattern)
}
return pattern
}