mirror of
https://github.com/MoonTechLab/LunaTV.git
synced 2026-03-06 03:47:30 +08:00
feat: set cached config when import, refactor db
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any, no-console, @typescript-eslint/no-non-null-assertion */
|
||||
|
||||
import { getStorage } from '@/lib/db';
|
||||
import { db } from '@/lib/db';
|
||||
|
||||
import { AdminConfig } from './admin.types';
|
||||
|
||||
@@ -178,14 +178,11 @@ async function getInitConfig(configFile: string, subConfig: {
|
||||
};
|
||||
|
||||
// 补充用户信息
|
||||
const storage = getStorage();
|
||||
let userNames: string[] = [];
|
||||
if (storage && typeof (storage as any).getAllUsers === 'function') {
|
||||
try {
|
||||
userNames = await (storage as any).getAllUsers();
|
||||
} catch (e) {
|
||||
console.error('获取用户列表失败:', e);
|
||||
}
|
||||
try {
|
||||
userNames = await db.getAllUsers();
|
||||
} catch (e) {
|
||||
console.error('获取用户列表失败:', e);
|
||||
}
|
||||
const allUsers = userNames.filter((u) => u !== process.env.USERNAME).map((u) => ({
|
||||
username: u,
|
||||
@@ -232,10 +229,11 @@ export async function getConfig(): Promise<AdminConfig> {
|
||||
}
|
||||
|
||||
// 读 db
|
||||
const storage = getStorage();
|
||||
let adminConfig: AdminConfig | null = null;
|
||||
if (storage && typeof (storage as any).getAdminConfig === 'function') {
|
||||
adminConfig = await (storage as any).getAdminConfig();
|
||||
try {
|
||||
adminConfig = await db.getAdminConfig();
|
||||
} catch (e) {
|
||||
console.error('获取管理员配置失败:', e);
|
||||
}
|
||||
|
||||
// db 中无配置,执行一次初始化
|
||||
@@ -247,20 +245,20 @@ export async function getConfig(): Promise<AdminConfig> {
|
||||
}
|
||||
|
||||
export async function resetConfig() {
|
||||
let originConfig: AdminConfig;
|
||||
const storage = getStorage();
|
||||
if (storage && typeof (storage as any).getAdminConfig === 'function') {
|
||||
originConfig = await (storage as any).getAdminConfig();
|
||||
} else {
|
||||
let originConfig: AdminConfig | null = null;
|
||||
try {
|
||||
originConfig = await db.getAdminConfig();
|
||||
} catch (e) {
|
||||
console.error('获取管理员配置失败:', e);
|
||||
}
|
||||
if (!originConfig) {
|
||||
originConfig = {} as AdminConfig;
|
||||
}
|
||||
const adminConfig = await getInitConfig(originConfig.ConfigFile, originConfig.ConfigSubscribtion);
|
||||
cachedConfig = adminConfig;
|
||||
if (storage && typeof (storage as any).setAdminConfig === 'function') {
|
||||
await (storage as any).setAdminConfig(adminConfig);
|
||||
}
|
||||
cachedConfig = adminConfig;
|
||||
await db.saveAdminConfig(adminConfig);
|
||||
|
||||
return;
|
||||
return;
|
||||
}
|
||||
|
||||
export async function getCacheTime(): Promise<number> {
|
||||
@@ -277,3 +275,7 @@ export async function getAvailableApiSites(): Promise<ApiSite[]> {
|
||||
detail: s.detail,
|
||||
}));
|
||||
}
|
||||
|
||||
export async function setCachedConfig(config: AdminConfig) {
|
||||
cachedConfig = config;
|
||||
}
|
||||
@@ -29,7 +29,7 @@ function createStorage(): IStorage {
|
||||
// 单例存储实例
|
||||
let storageInstance: IStorage | null = null;
|
||||
|
||||
export function getStorage(): IStorage {
|
||||
function getStorage(): IStorage {
|
||||
if (!storageInstance) {
|
||||
storageInstance = createStorage();
|
||||
}
|
||||
@@ -142,6 +142,14 @@ export class DbManager {
|
||||
return this.storage.checkUserExist(userName);
|
||||
}
|
||||
|
||||
async changePassword(userName: string, newPassword: string): Promise<void> {
|
||||
await this.storage.changePassword(userName, newPassword);
|
||||
}
|
||||
|
||||
async deleteUser(userName: string): Promise<void> {
|
||||
await this.storage.deleteUser(userName);
|
||||
}
|
||||
|
||||
// ---------- 搜索历史 ----------
|
||||
async getSearchHistory(userName: string): Promise<string[]> {
|
||||
return this.storage.getSearchHistory(userName);
|
||||
|
||||
Reference in New Issue
Block a user