Files
Showy/backend/services/configService.js
T

147 lines
3.7 KiB
JavaScript

const fs = require("fs");
const path = require("path");
const defaultSettings = {
userDataPath: "",
tmdbApiUrl: "",
tmdbApiKey: "",
frontPort: "",
backPort: "",
portableMode: "",
libraryViewMode: "grid",
favoritViewMode: "grid",
watchlistViewMode: "grid",
CollectionsViewMode: "grid",
CollectionItemsViewMode: "grid",
episodesViewMode: "grid",
};
class ConfigService {
constructor() {
if (!ConfigService.instance) {
ConfigService.instance = this;
this.settings = this.getSettings();
}
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.`);
const parsedSettings = JSON.parse(settingsData);
return { ...defaultSettings, ...parsedSettings };
} 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",
);
const seasonsPostersDir = path.join(
dataPath,
"images",
"seasons",
"posters",
);
const episodesStillsDir = path.join(
dataPath,
"images",
"episodes",
"stills",
);
const peopleProfilesDir = path.join(
dataPath,
"images",
"peoples",
"profiles",
);
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 });
fs.mkdirSync(seasonsPostersDir, { recursive: true });
fs.mkdirSync(episodesStillsDir, { recursive: true });
fs.mkdirSync(peopleProfilesDir, { recursive: true });
}
getTmdbApiUrl() {
return this.settings.tmdbApiUrl;
}
getTmdbApiKey() {
return this.settings.tmdbApiKey;
}
}
// const instance = new ConfigService();
// Object.freeze(instance);
module.exports = new ConfigService();