forked from reruin/sharelist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drive.sld.js
94 lines (73 loc) · 2.09 KB
/
drive.sld.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
/*
* xdrive 是 sharelist 内置的使用yaml描述的网盘系统 , 没有缓存
* xd: rootId(yaml文件所在路径) + yaml文件名 + ':' + 排序
*/
const name = 'ShareListDrive'
const version = '1.0'
const protocols = ['sld','xd']
const defaultProtocol = 'xd'
const yaml = require('yaml').default
module.exports = ({isObject , isArray , cache}) => {
/* 递归生成 索引 id */
const createId = (d, rootId) => {
d.forEach((i, index) => {
if (isObject(i)) {
i.id = rootId + '/' + i.name.replace(/\.d\.ln$/, '').replace(/\.ln$/, '')
i.protocol = defaultProtocol
if (i.children) {
i.type = 'folder'
i.protocol = defaultProtocol
createId(i.children, i.id)
} else {
i.ext = i.name.split('.').pop()
}
} else if (isArray(i)) {
createId(i, rootId)
}
})
return d
}
const mount = async (rootId, data) => {
let resid = `${defaultProtocol}:${rootId}`
let resp = { id: rootId, type: 'folder', protocol: defaultProtocol }
if (data) {
let json = yaml.parse(data)
json = createId(json, rootId + ':')
resp.children = json
resp.updated_at = Date.now()
cache(resid, resp)
return resp
} else {
return undefined
}
}
const findById = (id) => {
let rootId = id.split(':').slice(0, -1).join(':')
let disk = cache(`${defaultProtocol}:${rootId}`)
let path = id.split(':/')[1].split('/')
for (let i = 0; i < path.length && disk; i++) {
disk = disk.children
disk = disk.find(j => {
return `${j.name}` == path[i]
if (j.type == 'folder') {
return `${j.name}.${j.ext}` == path[i]
} else {
return `${j.name}` == path[i]
}
}) //[ parseInt(path[i]) ]
}
return disk
}
const folder = async (id, data) => {
if (data.content) {
return mount(id, data.content)
} else {
return findById(id)
}
}
const file = async (id) => {
let data = findById(id)
return data
}
return { name, version, drive: { protocols, folder, file } }
}