forked from malu2335/sharelist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.js
189 lines (141 loc) · 3.83 KB
/
config.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
const fs = require('fs')
const os = require('os')
const { createFiledb } = require('./utils/db/filedb');
const configPath = process.cwd() +'/cache/config.json'
const port = process.env.PORT || 33001
var runtime = {}
const db = createFiledb(configPath , {raw:true} , {
port ,
proxy_enable : 0 ,
preview_enable : 1,
index_enable:1,
webdav_path : '/webdav/',
//目录刷新时间 15分钟getDrive
max_age_dir: 15 * 60 * 1000,
//外链 10分钟
max_age_file: 5 * 60 * 1000,
max_age_download:0,
skin:'default',
//忽略文件(扩展名)
ignore_file_extensions:'',
ignore_files:'.passwd',
readme_enable:1,
ignore_paths:{
'__root__':['/readme.md']
},
max_age_download_sign:'sl_'+Date.now(),
anonymous_uplod_enable:0,
anonymous_download:'',
plugin_option:[],
custom_style:'',
custom_script:'',
//代理路径
proxy_paths:[],
proxy_server:'',
ocr_server:'https://api.reruin.net/ocr',
smb_server_enable: false,
smb_server_port:8445,
smb_anonymous_enable:true,
theme:'default',
});
if(process.env.PORT){
db.set('port' , port)
}
const save = async (d) => db.set(d)
const installed = () => {
return !!db.get('token')
}
const getConfig = (key) => db.get(key)
const setIgnorePaths = (key , paths) => {
let p = db.get('ignore_paths')
p[key] = paths
db.save()
}
const getIgnorePaths = (key , paths) => {
return [].concat(...Object.values(db.get('ignore_paths') || {})).filter(i => !!i)
}
const getAllConfig = (key) => db.all
const getPath = () => [].concat( db.get('path') || [] )
const getRuntime = () => runtime
const setRuntime = (value) => {
runtime = value
}
const getSkin = (key) => {
return db.get('skin') || 'default'
}
const setSkin = (name) => {
if( name != getSkin('skin') ){
save({skin:key})
}
}
const getPluginOption = (key) => {
let p = db.get('plugin_option') || []
let hit = p.find(i => i.key == key )
return hit ? hit.value : null
}
const setPluginOption = (key , value) => {
let p = db.get('plugin_option') || []
let hit = p.find(i => i.key == key )
if( hit ){
hit.value = value
}else{
p.push({ key , value})
}
db.save()
}
const saveDrive = (value , name) => {
if(!name) name = decodeURIComponent(runtime.req.path.replace(/^\//g,''))
const path = getPath()
let hit = path.find( i => i.name == name)
if(hit){
hit.path = value
db.save()
}
}
//获取当前路径drive
const getDrive = () => {
const path = getPath()
const name = decodeURIComponent(runtime.req.path.replace(/^\//g,'').split('/')[0])
const hit = path.find( i => i.name == name)
if(hit){
return hit.path
}else{
return false
}
}
//获取使用特定协议的drive
const getDrives = (protocols) => {
const path = getPath()
let ret = path.filter(i => protocols.includes(i.path.split(':')[0]))
// issue:68
// 如果只有一个目录则直接列出,导致挂载路径不一致 更新失败。
if(path.length == 1){
if(ret.length){
ret[0] = {...ret[0] , root:true}
}
}
return ret
}
const getProxyServer = () => {
let servers = (getConfig('proxy_server') || '').split(';').map( i => i.split('|') )
if( servers.length == 0 ) {
return ''
}else if(servers.length == 1){
return servers[0][0]
}
else{
let weight = servers.map(i => parseInt(i[1]))
let sum = weight.reduce((t,c) => t + c, 0)
let rnd = Math.floor(Math.random() * sum)
for(let i = 0; i < weight.length ; i++){
rnd -= weight[i]
if( rnd < 0 ){
return servers[i][0]
}
}
}
}
const checkAccess = (token) => {
return token === db.get('token')
}
module.exports = { getConfig, setIgnorePaths, getIgnorePaths, getAllConfig, setConfig:save, save , installed , getPath , setRuntime , getRuntime , saveDrive , getDrive , getSkin , getDrives , getPluginOption , setPluginOption , checkAccess , getProxyServer }