-
Notifications
You must be signed in to change notification settings - Fork 155
/
Copy pathresources.js
350 lines (324 loc) · 10.7 KB
/
resources.js
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import _ from 'lodash'
import path from 'path'
import moment from 'moment'
import fileIconMappings from '../fileTypeIconMappings.json'
import { getIndicators } from './statusIndicators'
import { bitmaskToRole, checkPermission, permissionsBitmask } from './collaborators'
import { shareTypes, userShareTypes } from './shareTypes'
import { $gettext } from '../gettext'
import { getAvatarSrc } from './user'
// Should we move this to ODS?
export function getFileIcon(extension) {
const icon = fileIconMappings[extension]
if (icon) {
return icon
}
return 'file'
}
function _getFileExtension(name) {
const extension = path.extname(name)
if (!extension) {
return ''
}
return extension.replace(/^(.)/, '').toLowerCase()
}
export function buildResource(resource) {
const isFolder = resource.type === 'dir'
const extension = _getFileExtension(resource.name)
return {
id: resource.fileInfo['{http://owncloud.org/ns}fileid'],
icon: isFolder ? 'folder' : getFileIcon(extension),
name: path.basename(resource.name),
extension: isFolder ? '' : extension,
path: resource.name,
type: isFolder ? 'folder' : resource.type,
mdate: resource.fileInfo['{DAV:}getlastmodified'],
size: isFolder
? resource.fileInfo['{http://owncloud.org/ns}size']
: resource.fileInfo['{DAV:}getcontentlength'],
indicators: [],
permissions: resource.fileInfo['{http://owncloud.org/ns}permissions'] || '',
starred: resource.fileInfo['{http://owncloud.org/ns}favorite'] !== '0',
etag: resource.fileInfo['{DAV:}getetag'],
sharePermissions:
resource.fileInfo['{http://open-collaboration-services.org/ns}share-permissions'],
shareTypes: (function() {
let shareTypes = resource.fileInfo['{http://owncloud.org/ns}share-types']
if (shareTypes) {
shareTypes = _.chain(shareTypes)
.filter(
xmlvalue =>
xmlvalue.namespaceURI === 'http://owncloud.org/ns' &&
xmlvalue.nodeName.split(':')[1] === 'share-type'
)
.map(xmlvalue => parseInt(xmlvalue.textContent || xmlvalue.text, 10))
.value()
}
return shareTypes || []
})(),
privateLink: resource.fileInfo['{http://owncloud.org/ns}privatelink'],
downloadURL: resource.fileInfo['{http://owncloud.org/ns}downloadURL'],
canUpload: function() {
return this.permissions.indexOf('C') >= 0
},
canDownload: function() {
// TODO: as soon as we allow folder downloads as archive we want to return `true` here without exceptions
return !isFolder
},
canBeDeleted: function() {
return this.permissions.indexOf('D') >= 0
},
canRename: function() {
return this.permissions.indexOf('N') >= 0
},
canShare: function() {
return this.permissions.indexOf('R') >= 0
},
canCreate: function() {
return this.permissions.indexOf('C') >= 0
},
isMounted: function() {
return this.permissions.indexOf('M') >= 0
},
isReceivedShare: function() {
return this.permissions.indexOf('S') >= 0
}
}
}
export function attachIndicators(resource, sharesTree) {
return (resource.indicators = getIndicators(resource, sharesTree))
}
/**
* Transforms given shares into a resource format and returns only their unique occurences
* @param {Array} shares Shares to be transformed into unique resources
* @param {Boolean} incomingShares Asserts whether the shares are incoming
* @param {Boolean} allowSharePerm Asserts whether the reshare permission is available
* @param {String} server The url of the backend
* @param {String} token The access token of the authenticated user
*/
export async function aggregateResourceShares(
shares,
incomingShares = false,
allowSharePerm,
server,
token
) {
if (incomingShares) {
return Promise.all(
_(shares)
.orderBy(['file_target', 'permissions'], ['asc', 'desc'])
.map(
async share =>
await buildSharedResource(share, incomingShares, allowSharePerm, server, token)
)
)
}
shares.sort((a, b) => a.path.localeCompare(b.path))
const resources = []
let prev = null
for (const share of shares) {
if (prev && share.path === prev.path) {
if (userShareTypes.includes(share.share_type)) {
prev.sharedWith.push({
username: share.share_with,
displayName: share.share_with_displayname,
avatar: await getAvatarSrc(share.share_with, server, token)
})
} else if (share.share_type === shareTypes.link) {
prev.sharedWith.push({
name: share.name || share.token,
link: true
})
}
continue
}
if (userShareTypes.includes(share.share_type)) {
share.sharedWith = [
{
username: share.share_with,
displayName: share.share_with_displayname,
avatar: await getAvatarSrc(share.share_with, server, token)
}
]
} else if (share.share_type === shareTypes.link) {
share.sharedWith = [
{
name: share.name || share.token,
link: true
}
]
}
prev = share
resources.push(share)
}
return Promise.all(
resources.map(
async share => await buildSharedResource(share, incomingShares, allowSharePerm, server, token)
)
)
}
export async function buildSharedResource(
share,
incomingShares = false,
allowSharePerm,
server,
token
) {
const resource = {
id: share.item_source,
type: share.item_type
}
const isFolder = resource.type === 'folder'
if (incomingShares) {
resource.resourceOwner = {
username: share.uid_file_owner,
displayName: share.displayname_file_owner
}
resource.owner = [
{
username: share.uid_owner,
displayName: share.displayname_owner,
avatar: await getAvatarSrc(share.uid_file_owner, server, token)
}
]
resource.status = share.state
resource.name = path.basename(share.file_target)
resource.path = share.file_target
resource.isReceivedShare = () => true
} else {
resource.sharedWith = share.sharedWith
resource.shareOwner = share.uid_owner
resource.shareOwnerDisplayname = share.displayname_owner
resource.name = path.basename(share.path)
resource.path = share.path
// permissions irrelevant here
resource.isReceivedShare = () => false
}
resource.extension = isFolder ? '' : _getFileExtension(resource.name)
// FIXME: add actual permission parsing
resource.canUpload = () => true
resource.canBeDeleted = () => true
resource.canRename = () => true
resource.canShare = () => {
return checkPermission(share.permissions, 'share')
}
resource.isMounted = () => false
resource.canDownload = () => !isFolder
resource.share = buildShare(share, resource, allowSharePerm)
resource.indicators = []
resource.icon = isFolder ? 'folder' : getFileIcon(resource.extension)
resource.sdate = share.stime * 1000
return resource
}
export function buildShare(s, file, allowSharePerm) {
if (parseInt(s.share_type, 10) === shareTypes.link) {
return _buildLink(s)
}
return buildCollaboratorShare(s, file, allowSharePerm)
}
function _buildLink(link) {
let description = ''
// FIXME: use bitmask matching with constants
switch (link.permissions) {
case '1': // read (1)
description = $gettext('Viewer')
break
case '5': // read (1) + create (4)
description = $gettext('Contributor')
break
case '4': // create (4)
description = $gettext('Uploader')
break
case '15': // read (1) + update (2) + create (4) + delete (8)
description = $gettext('Editor')
break
}
return {
shareType: parseInt(link.share_type, 10),
id: link.id,
token: link.token,
url: link.url,
path: link.path,
permissions: link.permissions,
description,
stime: link.stime,
name: typeof link.name === 'string' ? link.name : '',
password: !!(link.share_with && link.share_with_displayname),
expiration:
typeof link.expiration === 'string' ? moment(link.expiration).format('YYYY-MM-DD') : null,
itemSource: link.item_source,
file: {
parent: link.file_parent,
source: link.file_source,
target: link.file_target
}
}
}
function _fixAdditionalInfo(data) {
if (typeof data !== 'string') {
return null
}
return data
}
export function buildCollaboratorShare(s, file, allowSharePerm) {
const share = {
shareType: parseInt(s.share_type, 10),
id: s.id
}
switch (share.shareType) {
case shareTypes.user: // user share
// TODO differentiate groups from users?
// fall through
case shareTypes.remote:
// fall through
case shareTypes.group: // group share
share.role = bitmaskToRole(s.permissions, file.type === 'folder', allowSharePerm)
share.permissions = s.permissions
// FIXME: SDK is returning empty object for additional info when empty
share.collaborator = {
name: s.share_with,
displayName: s.share_with_displayname,
additionalInfo: _fixAdditionalInfo(s.share_with_additional_info)
}
share.owner = {
name: s.uid_owner,
displayName: s.displayname_owner,
additionalInfo: _fixAdditionalInfo(s.additional_info_owner)
}
share.fileOwner = {
name: s.uid_file_owner,
displayName: s.displayname_file_owner,
additionalInfo: _fixAdditionalInfo(s.additional_info_file_owner)
}
// TODO: Refactor to work with roles / prepare for roles API
share.customPermissions = {
update: s.permissions & permissionsBitmask.update,
create: s.permissions & permissionsBitmask.create,
delete: s.permissions & permissionsBitmask.delete,
share: s.permissions & permissionsBitmask.share
}
// share.email = '[email protected]' // hm, where do we get the mail from? share_with_additional_info:Object?
break
}
// expiration:Object if unset, or string "2019-04-24 00:00:00"
if (typeof s.expiration === 'string' || s.expiration instanceof String) {
share.expires = new Date(s.expiration)
}
share.path = s.path
return share
}
export function buildDeletedResource(resource) {
const isFolder = resource.type === 'dir'
const fullName = resource.fileInfo['{http://owncloud.org/ns}trashbin-original-filename']
const extension = isFolder ? '' : _getFileExtension(fullName)
return {
type: isFolder ? 'folder' : resource.type,
ddate: resource.fileInfo['{http://owncloud.org/ns}trashbin-delete-datetime'],
name: path.basename(fullName),
extension,
path: resource.fileInfo['{http://owncloud.org/ns}trashbin-original-location'],
id: path.basename(resource.name),
icon: isFolder ? 'folder' : getFileIcon(extension),
indicators: []
}
}