cloudapp-dl
Version:
CloudApp/Zight API client and CLI. Use as a CLI tool to download videos or as a programmatic library to interact with the Zight API.
162 lines (142 loc) • 3.4 kB
JavaScript
import fs from 'fs';
import path from 'path';
import os from 'os';
/**
* Get the config directory path based on the OS
* - macOS/Linux: ~/.config/cloudapp-dl/
* - Windows: %APPDATA%/cloudapp-dl/
*/
const getConfigDir = () => {
const platform = os.platform();
if (platform === 'win32') {
return path.join(process.env.APPDATA || path.join(os.homedir(), 'AppData', 'Roaming'), 'cloudapp-dl');
}
// macOS and Linux
return path.join(os.homedir(), '.config', 'cloudapp-dl');
};
const CONFIG_DIR = getConfigDir();
const CONFIG_FILE = path.join(CONFIG_DIR, 'config.json');
/**
* Default config structure
*/
const defaultConfig = {
email: null,
password: null,
sessionId: null,
sessionExpiry: null,
userId: null,
userName: null
};
/**
* Ensure the config directory exists
*/
const ensureConfigDir = () => {
if (!fs.existsSync(CONFIG_DIR)) {
fs.mkdirSync(CONFIG_DIR, { recursive: true });
}
};
/**
* Load config from file
* @returns {Object} The config object
*/
export const loadConfig = () => {
try {
ensureConfigDir();
if (fs.existsSync(CONFIG_FILE)) {
const data = fs.readFileSync(CONFIG_FILE, 'utf8');
return { ...defaultConfig, ...JSON.parse(data) };
}
return { ...defaultConfig };
} catch (error) {
console.error('Error loading config:', error.message);
return { ...defaultConfig };
}
};
/**
* Save config to file
* @param {Object} config - The config object to save
*/
export const saveConfig = (config) => {
try {
ensureConfigDir();
fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2), 'utf8');
return true;
} catch (error) {
console.error('Error saving config:', error.message);
return false;
}
};
/**
* Update specific config values
* @param {Object} updates - Key-value pairs to update
*/
export const updateConfig = (updates) => {
const config = loadConfig();
const newConfig = { ...config, ...updates };
return saveConfig(newConfig);
};
/**
* Get a specific config value
* @param {string} key - The config key to get
*/
export const getConfigValue = (key) => {
const config = loadConfig();
return config[key];
};
/**
* Check if user is logged in (has session)
* @returns {boolean}
*/
export const isLoggedIn = () => {
const config = loadConfig();
return !!(config.sessionId && config.email);
};
/**
* Check if session is expired
* @returns {boolean}
*/
export const isSessionExpired = () => {
const config = loadConfig();
if (!config.sessionExpiry) return true;
const expiry = new Date(config.sessionExpiry);
const now = new Date();
// Add a 5-minute buffer before actual expiry
return now >= new Date(expiry.getTime() - 5 * 60 * 1000);
};
/**
* Clear session data (logout)
*/
export const clearSession = () => {
const config = loadConfig();
const newConfig = {
...config,
sessionId: null,
sessionExpiry: null,
userId: null,
userName: null
};
return saveConfig(newConfig);
};
/**
* Clear all config (full reset)
*/
export const clearConfig = () => {
return saveConfig(defaultConfig);
};
/**
* Get the config file path (for display purposes)
*/
export const getConfigPath = () => {
return CONFIG_FILE;
};
export default {
loadConfig,
saveConfig,
updateConfig,
getConfigValue,
isLoggedIn,
isSessionExpired,
clearSession,
clearConfig,
getConfigPath
};