Files
Showy/backend/services/configService.js
T
X4Lo 0a79ee9768 Add named-token auth support to middleware and configService
authMiddleware now falls back to checking named tokens from settings
when JWT verification fails. configService.getNamedTokens() filters by
endDate, skipping expired tokens. Tokens without an endDate never expire.
2026-06-30 23:54:04 +01:00

160 lines
4.1 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;
}
getNamedTokens() {
const tokens = this.settings.namedTokens || [];
const now = new Date();
return tokens.filter((token) => {
if (!token.value) return false;
if (token.endDate) {
const end = new Date(token.endDate);
if (end < now) return false;
}
return true;
});
}
}
// const instance = new ConfigService();
// Object.freeze(instance);
module.exports = new ConfigService();