1 | const path = require('path');
|
2 | const fs = require('fs-extra');
|
3 | const { CONST, l10n } = require('./utils');
|
4 | const { systemDownloadsFolder, defaultConfigPath } = CONST;
|
5 |
|
6 | const DEFAULTS = {
|
7 | 'downloader': 'system',
|
8 | 'aria2-jsonrpc': 'http://localhost:6800/jsonrpc/',
|
9 | 'qbittorrent-url': 'http://localhost:8080/',
|
10 | 'qbittorrent-auth': 'username:password',
|
11 | 'destination': systemDownloadsFolder,
|
12 | 'webhook-url': 'http://localhost/',
|
13 | 'webhook-token': 'DEFAULT_WEBHOOK_TOKEN',
|
14 | };
|
15 | const VALIDATORS = {
|
16 | 'downloader': (downloader) => {
|
17 | const result = { ok: true, msg: '' };
|
18 | const downloaders = fs.readdirSync(`${__dirname}/downloaders`).map((d) => path.basename(d, '.js'));
|
19 | if (!(new Set(downloaders)).has(downloader)) {
|
20 | result.ok = false;
|
21 | result.msg = l10n('CMD_CFG_VALIDATORS_DOWNLOADER_ERR');
|
22 | }
|
23 | return result;
|
24 | },
|
25 | 'destination': (destination) => {
|
26 | const result = { ok: true, msg: '' };
|
27 | if (!(fs.existsSync(destination))) {
|
28 | result.ok = false;
|
29 | result.msg = l10n('CMD_CFG_VALIDATORS_DESTINATION_ERR');
|
30 | }
|
31 | return result;
|
32 | },
|
33 | };
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | class Config {
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | constructor({ configpath = defaultConfigPath } = {}) {
|
47 | this.configpath = configpath;
|
48 |
|
49 | if (!fs.existsSync(this.configpath)) {
|
50 | fs.ensureFileSync(this.configpath);
|
51 | this.reset();
|
52 | this.save();
|
53 | }
|
54 |
|
55 | this.parameters = Object.assign({}, DEFAULTS, JSON.parse(fs.readFileSync(this.configpath, 'utf-8')));
|
56 | }
|
57 |
|
58 | |
59 |
|
60 |
|
61 | save() {
|
62 | fs.writeFileSync(this.configpath, JSON.stringify(this.parameters));
|
63 | }
|
64 |
|
65 | |
66 |
|
67 |
|
68 |
|
69 |
|
70 | get(key) {
|
71 | if (Config.isValidKey(key)) {
|
72 | return { key, value: this.parameters[key] };
|
73 | }
|
74 | return null;
|
75 | }
|
76 |
|
77 | |
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | set(key, value) {
|
85 | if (Config.isValidKey(key)) {
|
86 | this.parameters[key] = value;
|
87 | this.save();
|
88 | return { key, value };
|
89 | }
|
90 | return null;
|
91 | }
|
92 |
|
93 | |
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | reset(key = null) {
|
100 | if (Config.isValidKey(key)) {
|
101 | this.parameters[key] = DEFAULTS[key];
|
102 | } else {
|
103 | this.parameters = Object.assign({}, DEFAULTS);
|
104 | }
|
105 | this.save();
|
106 | }
|
107 |
|
108 | |
109 |
|
110 |
|
111 |
|
112 | isValid() {
|
113 | return Object.keys(this.parameters).every((key) => {
|
114 | if (VALIDATORS[key]) {
|
115 | return VALIDATORS[key](key).ok;
|
116 | }
|
117 | return true;
|
118 | });
|
119 | }
|
120 |
|
121 | |
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 | static isValidKey(key) {
|
128 | return key && Object.keys(DEFAULTS).includes(key);
|
129 | }
|
130 | }
|
131 |
|
132 | Config.DEFAULTS = DEFAULTS;
|
133 | Config.VALIDATORS = VALIDATORS;
|
134 |
|
135 | exports.Config = Config;
|