156 lines
4.3 KiB
JavaScript
156 lines
4.3 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
const { app } = require('electron');
|
|
|
|
class ConfigManager {
|
|
constructor() {
|
|
this.configPath = path.join(app.getPath('userData'), 'config.json');
|
|
this.config = this.loadConfig();
|
|
}
|
|
|
|
loadConfig() {
|
|
try {
|
|
if (fs.existsSync(this.configPath)) {
|
|
const data = fs.readFileSync(this.configPath, 'utf8');
|
|
return JSON.parse(data);
|
|
}
|
|
} catch (error) {
|
|
console.error('Ошибка загрузки конфигурации:', error);
|
|
}
|
|
|
|
// Конфигурация по умолчанию
|
|
return {
|
|
server: {
|
|
url: 'http://localhost:3002',
|
|
autoConnect: false,
|
|
reconnectAttempts: 3,
|
|
reconnectInterval: 5000
|
|
},
|
|
ui: {
|
|
theme: 'dark',
|
|
language: 'ru',
|
|
windowBounds: {
|
|
width: 1400,
|
|
height: 900,
|
|
x: null,
|
|
y: null
|
|
},
|
|
videoFilters: {
|
|
brightness: 0,
|
|
contrast: 0,
|
|
saturation: 0,
|
|
monochrome: false,
|
|
edgeDetection: false
|
|
}
|
|
},
|
|
recording: {
|
|
quality: 'high',
|
|
format: 'webm',
|
|
maxDuration: 3600000, // 1 час в миллисекундах
|
|
autoSave: true,
|
|
saveLocation: path.join(app.getPath('videos'), 'GodEye')
|
|
}
|
|
};
|
|
}
|
|
|
|
saveConfig() {
|
|
try {
|
|
// Создаем директорию если её нет
|
|
const dir = path.dirname(this.configPath);
|
|
if (!fs.existsSync(dir)) {
|
|
fs.mkdirSync(dir, { recursive: true });
|
|
}
|
|
|
|
fs.writeFileSync(this.configPath, JSON.stringify(this.config, null, 2), 'utf8');
|
|
} catch (error) {
|
|
console.error('Ошибка сохранения конфигурации:', error);
|
|
}
|
|
}
|
|
|
|
get(key) {
|
|
if (!key || typeof key !== 'string') {
|
|
return this.config; // Возвращаем всю конфигурацию если ключ пустой
|
|
}
|
|
|
|
// Если передана пустая строка, возвращаем всю конфигурацию
|
|
if (key.trim() === '') {
|
|
return this.config;
|
|
}
|
|
|
|
const keys = key.split('.');
|
|
let value = this.config;
|
|
|
|
for (const k of keys) {
|
|
if (value && typeof value === 'object' && k in value) {
|
|
value = value[k];
|
|
} else {
|
|
return undefined;
|
|
}
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
set(key, value) {
|
|
const keys = key.split('.');
|
|
let obj = this.config;
|
|
|
|
for (let i = 0; i < keys.length - 1; i++) {
|
|
const k = keys[i];
|
|
if (!(k in obj) || typeof obj[k] !== 'object') {
|
|
obj[k] = {};
|
|
}
|
|
obj = obj[k];
|
|
}
|
|
|
|
obj[keys[keys.length - 1]] = value;
|
|
this.saveConfig();
|
|
}
|
|
|
|
// Быстрые методы для часто используемых настроек
|
|
getServerUrl() {
|
|
return this.get('server.url');
|
|
}
|
|
|
|
setServerUrl(url) {
|
|
this.set('server.url', url);
|
|
}
|
|
|
|
getAutoConnect() {
|
|
return this.get('server.autoConnect');
|
|
}
|
|
|
|
setAutoConnect(autoConnect) {
|
|
this.set('server.autoConnect', autoConnect);
|
|
}
|
|
|
|
getWindowBounds() {
|
|
return this.get('ui.windowBounds');
|
|
}
|
|
|
|
setWindowBounds(bounds) {
|
|
this.set('ui.windowBounds', bounds);
|
|
}
|
|
|
|
getVideoFilters() {
|
|
return this.get('ui.videoFilters');
|
|
}
|
|
|
|
setVideoFilters(filters) {
|
|
this.set('ui.videoFilters', filters);
|
|
}
|
|
|
|
getRecordingSettings() {
|
|
return this.get('recording');
|
|
}
|
|
|
|
ensureRecordingDirectory() {
|
|
const saveLocation = this.get('recording.saveLocation');
|
|
if (!fs.existsSync(saveLocation)) {
|
|
fs.mkdirSync(saveLocation, { recursive: true });
|
|
}
|
|
return saveLocation;
|
|
}
|
|
}
|
|
|
|
module.exports = ConfigManager; |