/** * 存储配置管理器 * 负责加载、验证和管理存储配置,支持从文件加载配置 */ import { StorageConfig } from '../interfaces/StorageAdapter'; import { StorageType } from './StorageFactory'; import * as fs from 'fs'; import * as path from 'path'; /** * 配置文件接口 */ export interface StorageConfigFile { /** * 默认存储配置 */ default: StorageConfig; /** * 命名存储配置集合 */ namedConfigs?: Record; /** * 插件配置 */ plugins?: { /** * 插件目录路径 */ directory?: string; /** * 预加载的插件列表 */ preload?: string[]; }; } /** * 存储配置管理器类 */ export class StorageConfigManager { // 已加载的命名配置缓存 private static configCache: Record = {}; // 默认配置 private static defaultConfig: StorageConfig = { type: StorageType.Memory // 默认使用内存存储 }; /** * 从配置文件加载存储配置 * @param configPath 配置文件路径 * @returns 配置文件内容 */ public static loadConfigFile(configPath: string): StorageConfigFile { try { // 检查文件是否存在 if (!fs.existsSync(configPath)) { throw new Error(`Config file not found: ${configPath}`); } // 读取文件内容 const fileContent = fs.readFileSync(configPath, 'utf8'); // 解析JSON配置 const config = JSON.parse(fileContent) as StorageConfigFile; // 验证配置结构 if (!config.default || !config.default.type) { throw new Error('Invalid config file: Missing default storage configuration'); } // 更新默认配置和配置缓存 this.defaultConfig = config.default; // 缓存命名配置 if (config.namedConfigs) { for (const [name, cfg] of Object.entries(config.namedConfigs)) { this.configCache[name] = cfg; } } return config; } catch (error) { console.error(`Failed to load config file from ${configPath}:`, error); throw new Error(`Config loading error: ${error instanceof Error ? error.message : 'Unknown error'}`); } } /** * 获取默认存储配置 * @returns 默认存储配置 */ public static getDefaultConfig(): StorageConfig { return { ...this.defaultConfig }; } /** * 根据名称获取命名存储配置 * @param name 配置名称 * @returns 存储配置或默认配置 */ public static getConfig(name?: string): StorageConfig { if (name && this.configCache[name]) { return { ...this.configCache[name] }; } return this.getDefaultConfig(); } /** * 注册命名存储配置 * @param name 配置名称 * @param config 存储配置 */ public static registerConfig(name: string, config: StorageConfig): void { this.validateConfig(config); this.configCache[name] = { ...config }; } /** * 设置默认存储配置 * @param config 存储配置 */ public static setDefaultConfig(config: StorageConfig): void { this.validateConfig(config); this.defaultConfig = { ...config }; } /** * 验证存储配置的有效性 * @param config 存储配置 * @throws Error 配置无效时抛出错误 */ public static validateConfig(config: StorageConfig): void { if (!config) { throw new Error('Storage config cannot be null or undefined'); } if (!config.type || typeof config.type !== 'string') { throw new Error('Storage config must have a valid type property'); } // 验证connection是否为对象类型 if (config.connection && typeof config.connection !== 'object') { throw new Error('Storage config connection must be an object'); } // 验证options是否为对象类型 if (config.options && typeof config.options !== 'object') { throw new Error('Storage config options must be an object'); } } /** * 获取所有已注册的命名配置名称 * @returns 配置名称数组 */ public static getRegisteredConfigNames(): string[] { return Object.keys(this.configCache); } /** * 清除命名配置缓存 */ public static clearConfigCache(): void { this.configCache = {}; } /** * 创建默认配置文件 * @param filePath 目标文件路径 */ public static createDefaultConfigFile(filePath: string): void { const defaultConfig: StorageConfigFile = { default: { type: StorageType.Memory, options: { autoInitialize: true } }, namedConfigs: { development: { type: StorageType.FileSystem, connection: { basePath: './data' }, options: { prettyPrint: true } }, production: { type: StorageType.MongoDB, connection: { url: 'mongodb://localhost:27017', database: 'assets-management' } } }, plugins: { directory: './plugins', preload: [] } }; // 确保目录存在 const dirPath = path.dirname(filePath); if (!fs.existsSync(dirPath)) { fs.mkdirSync(dirPath, { recursive: true }); } // 写入配置文件 fs.writeFileSync(filePath, JSON.stringify(defaultConfig, null, 2)); } }