Major Backend Single Responsibility Update part 1
This commit is contained in:
1 parent
65177c7553
commit
cbdffb21e3
41 files changed
+1723
-888
No files matched your search
@@ -0,0 +1,95 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const defaultSettings = {
|
||||
userDataPath: '',
|
||||
tmdbApiUrl: '',
|
||||
tmdbApiKey: '',
|
||||
frontPort: '',
|
||||
backPort: '',
|
||||
portableMode: '',
|
||||
|
||||
libraryViewMode: '',
|
||||
favoritViewMode: '',
|
||||
watchlistViewMode: '',
|
||||
CollectionsViewMode: '',
|
||||
CollectionItemsViewMode: '',
|
||||
};
|
||||
|
||||
class ConfigService {
|
||||
constructor() {
|
||||
if (!ConfigService.instance) {
|
||||
ConfigService.instance = this;
|
||||
}
|
||||
return ConfigService.instance;
|
||||
}
|
||||
|
||||
getUserDataPath() {
|
||||
return path.join(__dirname, '..', '..');
|
||||
}
|
||||
|
||||
getSettingsFilePath() {
|
||||
return path.join(this.getUserDataPath(), 'settings.json');
|
||||
}
|
||||
|
||||
getSettings() {
|
||||
const settingsFilePath = this.getSettingsFilePath();
|
||||
|
||||
try {
|
||||
const settingsData = fs.readFileSync(settingsFilePath);
|
||||
// console.log(`Loaded settings file.`);
|
||||
return JSON.parse(settingsData);
|
||||
} catch (err) {
|
||||
console.error('Error reading settings file:', err);
|
||||
console.log('Creating default settings file...');
|
||||
|
||||
let newSettingsFile = { ...defaultSettings };
|
||||
|
||||
let defaultDataPath = path.join(this.getUserDataPath(), 'data')
|
||||
|
||||
newSettingsFile.userDataPath = defaultDataPath;
|
||||
newSettingsFile.backPort = 4000;
|
||||
|
||||
fs.mkdirSync(defaultDataPath, { recursive: true });
|
||||
fs.writeFileSync(settingsFilePath, JSON.stringify(newSettingsFile, null, 2));
|
||||
console.log('Default settings file created.');
|
||||
|
||||
return defaultSettings;
|
||||
}
|
||||
}
|
||||
|
||||
updateSettings(newSettings) {
|
||||
const settingsFilePath = this.getSettingsFilePath();
|
||||
|
||||
const oldSettings = this.getSettings();
|
||||
let updatedSettings = { ...defaultSettings, ...oldSettings, ...newSettings };
|
||||
fs.mkdirSync(updatedSettings.userDataPath, { recursive: true }); // making sure the newly set data path is created before saving the new config
|
||||
fs.writeFileSync(settingsFilePath, JSON.stringify(updatedSettings, null, 2));
|
||||
}
|
||||
|
||||
getMediaFolder() {
|
||||
return this.getSettings().userDataPath;
|
||||
}
|
||||
|
||||
createMediaFolders() {
|
||||
const settings = this.getSettings();
|
||||
const dataPath = settings.userDataPath;
|
||||
|
||||
const posterDir = path.join(dataPath, 'images', 'entries', 'posters');
|
||||
const backdropDir = path.join(dataPath, 'images', 'entries', 'backdrops');
|
||||
const logoDir = path.join(dataPath, 'images', 'entries', 'logos');
|
||||
const collectionsPostersDir = path.join(dataPath, 'images', 'collections', 'posters');
|
||||
const collectionsBackdropsDir = path.join(dataPath, 'images', 'collections', 'backdrops');
|
||||
|
||||
fs.mkdirSync(posterDir, { recursive: true });
|
||||
fs.mkdirSync(backdropDir, { recursive: true });
|
||||
fs.mkdirSync(logoDir, { recursive: true });
|
||||
fs.mkdirSync(collectionsPostersDir, { recursive: true });
|
||||
fs.mkdirSync(collectionsBackdropsDir, { recursive: true });
|
||||
}
|
||||
}
|
||||
|
||||
// const instance = new ConfigService();
|
||||
// Object.freeze(instance);
|
||||
|
||||
module.exports = new ConfigService();
|
||||
Reference in new issue
Block a user