Imrpove chat with gifs, videos, music player, redesigns and improved filesharing errors
This commit is contained in:
72
server/src/config/variables.ts
Normal file
72
server/src/config/variables.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
export interface ServerVariablesConfig {
|
||||
klipyApiKey: string;
|
||||
}
|
||||
|
||||
const DATA_DIR = path.join(process.cwd(), 'data');
|
||||
const VARIABLES_FILE = path.join(DATA_DIR, 'variables.json');
|
||||
|
||||
function normalizeKlipyApiKey(value: unknown): string {
|
||||
return typeof value === 'string' ? value.trim() : '';
|
||||
}
|
||||
|
||||
function readRawVariables(): { rawContents: string; parsed: Record<string, unknown> } {
|
||||
if (!fs.existsSync(VARIABLES_FILE)) {
|
||||
return { rawContents: '', parsed: {} };
|
||||
}
|
||||
|
||||
const rawContents = fs.readFileSync(VARIABLES_FILE, 'utf8');
|
||||
|
||||
if (!rawContents.trim()) {
|
||||
return { rawContents, parsed: {} };
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(rawContents) as unknown;
|
||||
|
||||
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
|
||||
return { rawContents, parsed: parsed as Record<string, unknown> };
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('[Config] Failed to parse variables.json. Recreating it with defaults.', error);
|
||||
}
|
||||
|
||||
return { rawContents, parsed: {} };
|
||||
}
|
||||
|
||||
export function getVariablesConfigPath(): string {
|
||||
return VARIABLES_FILE;
|
||||
}
|
||||
|
||||
export function ensureVariablesConfig(): ServerVariablesConfig {
|
||||
if (!fs.existsSync(DATA_DIR)) {
|
||||
fs.mkdirSync(DATA_DIR, { recursive: true });
|
||||
}
|
||||
|
||||
const { rawContents, parsed } = readRawVariables();
|
||||
const normalized = {
|
||||
...parsed,
|
||||
klipyApiKey: normalizeKlipyApiKey(parsed.klipyApiKey)
|
||||
};
|
||||
const nextContents = JSON.stringify(normalized, null, 2) + '\n';
|
||||
|
||||
if (!fs.existsSync(VARIABLES_FILE) || rawContents !== nextContents) {
|
||||
fs.writeFileSync(VARIABLES_FILE, nextContents, 'utf8');
|
||||
}
|
||||
|
||||
return { klipyApiKey: normalized.klipyApiKey };
|
||||
}
|
||||
|
||||
export function getVariablesConfig(): ServerVariablesConfig {
|
||||
return ensureVariablesConfig();
|
||||
}
|
||||
|
||||
export function getKlipyApiKey(): string {
|
||||
return getVariablesConfig().klipyApiKey;
|
||||
}
|
||||
|
||||
export function hasKlipyApiKey(): boolean {
|
||||
return getKlipyApiKey().length > 0;
|
||||
}
|
||||
Reference in New Issue
Block a user