Multi-Hoster-Upload/lib/config-store.js

71 lines
1.9 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const DEFAULTS = {
hosters: {
'doodstream.com': { enabled: true, apiKey: '' },
'voe.sx': { enabled: true, apiKey: '' },
'vidmoly.me': { enabled: true, authType: 'login', username: '', password: '' },
'byse.sx': { enabled: true, apiKey: '' }
},
history: []
};
const MAX_HISTORY = 100;
class ConfigStore {
constructor(app) {
const dir = app && app.isPackaged
? app.getPath('userData')
: path.join(__dirname, '..');
this.filePath = path.join(dir, 'electron-config.json');
}
load() {
try {
const raw = fs.readFileSync(this.filePath, 'utf-8');
const data = JSON.parse(raw);
// Merge with defaults so new hosters are always present
const hosters = { ...DEFAULTS.hosters };
for (const [name, val] of Object.entries(data.hosters || {})) {
if (hosters[name]) {
hosters[name] = { ...hosters[name], ...val };
}
}
return { hosters, history: data.history || [] };
} catch {
return JSON.parse(JSON.stringify(DEFAULTS));
}
}
save(config) {
const current = this.load();
// Only update hosters, keep history
current.hosters = config.hosters || current.hosters;
fs.writeFileSync(this.filePath, JSON.stringify(current, null, 2), 'utf-8');
}
loadHistory() {
const config = this.load();
return config.history || [];
}
appendHistory(entry) {
const config = this.load();
config.history.push(entry);
// Cap at MAX_HISTORY
if (config.history.length > MAX_HISTORY) {
config.history = config.history.slice(-MAX_HISTORY);
}
fs.writeFileSync(this.filePath, JSON.stringify(config, null, 2), 'utf-8');
}
clearHistory() {
const config = this.load();
config.history = [];
fs.writeFileSync(this.filePath, JSON.stringify(config, null, 2), 'utf-8');
}
}
module.exports = ConfigStore;