forked from reruin/sharelist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.core.js
221 lines (181 loc) · 5 KB
/
cmd.core.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
/*
* 核心命令
*/
const name = 'core'
const version = '1.0'
const diff = (a, b) => {
let ret = []
b.forEach((v, i) => {
if (v != a[i]) {
ret.push(v)
}
})
return ret
}
var nodeCache = {}
const clone = (obj) => {
let type = typeof obj
if( type == 'number' || type == 'string' || type == 'boolean' || type === undefined || type === null ){
return obj
}
if (obj instanceof Array) {
return obj.map(i => clone(i))
}
if (obj instanceof Object) {
let copy = {};
for (let attr in obj) {
if (obj.hasOwnProperty(attr)) copy[attr] = clone(obj[attr])
}
return copy
}
return obj
}
module.exports = ({ cache , getVendor , getConfig , getRuntime , updateFolder , updateLnk , updateFile , pathNormalize , createReadStream , createWriteStream }) => {
const ls = async (p , filter , iscmd) => {
//处理 协议路径 protocol:id
let vp = p.split(':')
if(vp.length){
let protocol = vp[0] , id = vp.slice(1).join(':')
let vendor = getVendor(protocol)
if( vendor ){
return await vendor.file(id)
}
}
// 处理标准路径 path
let resp = await process_fast(p , filter)
if( iscmd ){
if( resp.children ){
resp = { result: resp.children.map(i => i.name).join('\n') }
}else{
resp = 'path not ready'
}
}
let ret = clone(resp)
if(ret.children){
ret.children.forEach(i => {
if(i.ext) i.ext = i.ext.toLowerCase()
})
}else if(ret.ext){
ret.ext = ret.ext.toLowerCase()
}
return ret
}
const cat = async (p) => {
}
const cd = async (basepath , [path = '/'] = []) => {
return {cd:pathNormalize(path , basepath)}
}
const cls = async () => {
return {cls:1}
}
const root = () => {
let paths = getConfig('path'), key
let disk = paths.map((i, index) => ({
id: i.path.split(':').slice(1).join(':'),
protocol: i.path.split(':')[0],
name: i.name,
size: '-',
updated_at: '-',
type: 'folder'
}))
return { id: '$root', protocol: 'root', cache:false , type: 'folder', children: disk }
}
/*
* 1.缓存挂载节点 <-> sharelist路径
* 2.缓存passwd节点 <-> sharelist路径
* 3.正向查找
* 4.不再缓存 sharelist路径 <-> 数据 关系
*/
const process_fast = async(p, filter) => {
// paths : / => [''] ; /a/b/c => [a','b','c']
let hit = root()
let paths = (p == '' || p == '/') ? [] : p.replace(/^\//,'').split('/').map(i => decodeURIComponent(i))
let idx = 0
let isRoot = paths.length == 0
//逆向查询节点 path -> filemeta { protocol , id , type } -> filedata
while( idx >= 0 ){
let cur = '/' + paths.slice(0,idx).join('/')
let content = nodeCache[cur]
if( content ){
hit = content
//过滤器拦截
if(filter && filter(hit , paths.slice(0,idx+1))){
return hit;
}
idx--
break
}else{
idx--
}
}
if(hit.protocol == 'root'){
if( hit.children && hit.children.length == 1 ){
hit = hit.children[0]
}else if( isRoot ){
return hit
}
}
for( ; idx < paths.length; idx++ ){
if(paths[idx] && hit.children){
let children = hit.children
hit = children.find(i => i.name == paths[idx])
if( !hit ){
return false
}
}
if(hit.protocol == 'root' ){
continue
}
let vendor = getVendor(hit.protocol)
if (hit.lnk) {
let originId = hit.protocol + ':' + hit.id
await updateLnk(hit)
vendor = getVendor(hit.protocol)
//缓存 快捷方式 的实际链接
cache.set(originId, hit.protocol + ':' + hit.id)
}
//root 下不做缓存
if( vendor.cache !== false && hit.type == 'folder' && idx > 0 ){
let cacheId = '/' + paths.slice(0,idx + 1).join('/')
nodeCache[cacheId] = { protocol: hit.protocol , id : hit.id , type: hit.type }
}
if( hit.type == 'folder'){
let t = await vendor.folder(hit.id , { req : getRuntime('req')})
//console.log(t.type,t.children)
if( t ){
if( t.type == 'folder' ){
hit = await updateFolder(t)
if( vendor.createWriteStream ){
hit.writeable = true
}
}
else if(t.type == 'redir') {
hit = await process_fast(t.path)
//不再参与后续
break;
}
else{
hit = await updateFile(t)
}
}else{
return false
}
}
else{
let t = await vendor.file(hit.id, { data:hit , req : getRuntime('req') })
if( t ){
hit = await updateFile(t)
break
}else{
return false
}
}
//过滤器拦截
if(filter && filter(hit , paths.slice(0,idx+1))){
return hit;
}
}
return hit
}
return { name , version , cmd:{ ls , cd , cls }}
}