-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Expand file tree
/
Copy pathFileInfo.ts
More file actions
89 lines (80 loc) · 2.08 KB
/
Copy pathFileInfo.ts
File metadata and controls
89 lines (80 loc) · 2.08 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
/*!
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import type { Attribute, INode } from '@nextcloud/files'
import { Permission } from '@nextcloud/files'
interface RawLegacyFileInfo {
id: number
path: string
name: string
mtime: number | undefined
etag: string
size: number
hasPreview: boolean
isEncrypted: boolean
isFavourited: boolean
mimetype: string
permissions: number
mountType: null | string
sharePermissions: string
shareAttributes: object
type: 'file' | 'dir'
attributes: Attribute
}
export type LegacyFileInfo = RawLegacyFileInfo & {
get: (key: keyof RawLegacyFileInfo) => unknown
isDirectory: () => boolean
canEdit: () => boolean
node: INode
canDownload: () => boolean
}
/**
* Convert Node to legacy file info
*
* @param node - The Node to convert
*/
export default function(node: INode): LegacyFileInfo {
const rawFileInfo: RawLegacyFileInfo = {
id: node.fileid!,
path: node.dirname,
name: node.basename,
mtime: node.mtime?.getTime(),
etag: node.attributes.etag,
size: node.size!,
hasPreview: node.attributes.hasPreview,
isEncrypted: node.attributes.isEncrypted === 1,
isFavourited: node.attributes.favorite === 1,
mimetype: node.mime,
permissions: node.permissions,
mountType: node.attributes['mount-type'],
sharePermissions: node.attributes['share-permissions'],
shareAttributes: JSON.parse(node.attributes['share-attributes'] || '[]'),
type: node.type === 'file' ? 'file' : 'dir',
attributes: node.attributes,
}
// TODO remove when no more legacy backbone is used
const fileInfo: LegacyFileInfo = {
...rawFileInfo,
node,
get(key) {
return this[key]
},
isDirectory() {
return this.mimetype === 'httpd/unix-directory'
},
canEdit() {
return Boolean(this.permissions & Permission.UPDATE)
},
canDownload() {
for (const i in this.shareAttributes) {
const attr = this.shareAttributes[i]
if (attr.scope === 'permissions' && attr.key === 'download') {
return attr.value === true
}
}
return true
},
}
return fileInfo
}