forked from kahosan/kikoeru-express
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.js
192 lines (172 loc) · 6.11 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
190
191
192
const fs = require('fs');
const path = require('path');
const crypto = require('crypto')
const configFolderDir = process.pkg ? path.join(process.execPath, '..', 'config') : path.join(__dirname, 'config');
const configPath = path.join(configFolderDir, 'config.json');
const pjson = require('./package.json');
const compareVersions = require('compare-versions');
// Before the following version, there is no version tracking
const versionWithoutVerTracking = '0.4.1';
// Before the following version, db path is using the absolute path in databaseFolderDir of config.json
const versionDbRelativePath = '0.5.8';
let config = {};
const voiceWorkDefaultPath = () => {
if (process.env.IS_DOCKER) {
return '/usr/src/kikoeru/VoiceWork';
} else if (process.pkg) {
return path.join(process.execPath, '..', 'VoiceWork');
} else {
return path.join(__dirname, 'VoiceWork');
}
}
const defaultConfig = {
version: pjson.version,
production: process.env.NODE_ENV === 'production' ? true : false,
dbBusyTimeout: 1000,
checkUpdate: true,
checkBetaUpdate: false,
maxParallelism: 16,
rootFolders: [
// {
// name: '',
// path: ''
// }
],
coverFolderDir: process.pkg ? path.join(process.execPath, '..', 'covers') : path.join(__dirname, 'covers'),
databaseFolderDir: process.pkg ? path.join(process.execPath, '..', 'sqlite') : path.join(__dirname, 'sqlite'),
coverUseDefaultPath: false, // Ignores coverFolderDir if set to true
dbUseDefaultPath: true, // Ignores databaseFolderDir if set to true
voiceWorkDefaultPath: voiceWorkDefaultPath(),
auth: process.env.NODE_ENV === 'production' ? true : false,
md5secret: crypto.randomBytes(32).toString('hex'),
jwtsecret: crypto.randomBytes(32).toString('hex'),
expiresIn: 2592000,
scannerMaxRecursionDepth: 2,
pageSize: 12,
tagLanguage: 'zh-cn',
retry: 5,
dlsiteTimeout: 10000,
hvdbTimeout: 10000,
retryDelay: 2000,
httpProxyHost: '',
httpProxyPort: 0,
listenPort: 8888,
blockRemoteConnection: false,
behindProxy: false,
httpsEnabled: false,
httpsPrivateKey: 'kikoeru.key',
httpsCert: 'kikoeru.crt',
httpsPort: 8443,
skipCleanup: false,
enableGzip: true,
rewindSeekTime: 5,
forwardSeekTime: 30,
offloadMedia: false,
offloadStreamPath: '/media/stream/', // /media/stream/RJ123456/subdirs/track.mp3
offloadDownloadPath: '/media/download/' // /media/download/RJ123456/subdirs/track.mp3
};
const initConfig = (writeConfigToFile = !process.env.FREEZE_CONFIG_FILE) => {
config = Object.assign(config, defaultConfig);
if (writeConfigToFile) {
fs.writeFileSync(configPath, JSON.stringify(defaultConfig, null, "\t"));
}
}
const setConfig = (newConfig, writeConfigToFile = !process.env.FREEZE_CONFIG_FILE) => {
// Prevent changing some values, overwrite with old ones
newConfig.production = config.production;
if (process.env.NODE_ENV === 'production' || config.production) {
newConfig.auth = true;
}
newConfig.md5secret = config.md5secret;
newConfig.jwtsecret = config.jwtsecret;
// Merge config
config = Object.assign(config, newConfig);
if (writeConfigToFile) {
fs.writeFileSync(configPath, JSON.stringify(config, null, "\t"));
}
}
// Get or use default value
const readConfig = () => {
config = JSON.parse(fs.readFileSync(configPath));
for (let key in defaultConfig) {
if (!config.hasOwnProperty(key)) {
if (key === 'version') {
config['version'] = versionWithoutVerTracking;
} else {
config[key] = defaultConfig[key];
}
}
}
// Support reading relative path
// When config is saved in admin panel, it will still be stored as absolute path
if(!path.isAbsolute(config.coverFolderDir)) {
config.coverFolderDir = process.pkg ? path.join(process.execPath, '..', config.coverFolderDir) : path.join(__dirname, config.coverFolderDir);
}
if(!path.isAbsolute(config.databaseFolderDir)) {
config.databaseFolderDir = process.pkg ? path.join(process.execPath, '..', config.databaseFolderDir) : path.join(__dirname, config.databaseFolderDir);
}
// Use ./covers and ./sqlite to override settings, ignoring corresponding fields in config
if (config.coverUseDefaultPath) {
config.coverFolderDir = process.pkg ? path.join(process.execPath, '..', 'covers') : path.join(__dirname, 'covers');
}
if (config.dbUseDefaultPath) {
config.databaseFolderDir = process.pkg ? path.join(process.execPath, '..', 'sqlite') : path.join(__dirname, 'sqlite');
}
if (process.env.NODE_ENV === 'production' || config.production) {
config.auth = true;
config.production = true;
}
};
// Migrate config
const updateConfig = (writeConfigToFile = !process.env.FREEZE_CONFIG_FILE) => {
let cfg = JSON.parse(fs.readFileSync(configPath));
let countChanged = 0;
for (let key in defaultConfig) {
if (!cfg.hasOwnProperty(key)) {
console.log('写入设置', key);
cfg[key] = defaultConfig[key];
countChanged += 1;
}
}
if (compareVersions.compare(cfg.version, versionDbRelativePath, '<')) {
console.log('数据库位置已设置为程序目录下的sqlite文件夹');
console.log('如需指定其它位置,请阅读0.6.0-rc.0更新说明');
}
if (countChanged || cfg.version !== pjson.version) {
cfg.version = pjson.version;
setConfig(cfg, writeConfigToFile)
}
}
class publicConfig {
get rewindSeekTime() {
return config.rewindSeekTime;
}
get forwardSeekTime() {
return config.forwardSeekTime;
}
export() {
return {
rewindSeekTime: this.rewindSeekTime,
forwardSeekTime: this.forwardSeekTime
}
}
}
const sharedConfigHandle = new publicConfig();
// This part runs when the module is initialized
// TODO: refactor global side effect
if (!fs.existsSync(configPath)) {
if (!fs.existsSync(configFolderDir)) {
try {
fs.mkdirSync(configFolderDir, { recursive: true });
} catch(err) {
console.error(` ! 在创建存放配置文件的文件夹时出错: ${err.message}`);
}
}
const writeConfigToFile = !process.env.FREEZE_CONFIG_FILE;
initConfig(writeConfigToFile);
} else {
readConfig();
}
module.exports = {
setConfig, updateConfig, config, sharedConfigHandle, configFolderDir
};