feat: set cached config when import, refactor db

This commit is contained in:
shinya
2025-08-15 18:47:51 +08:00
parent 4be9788668
commit 840487acb0
10 changed files with 56 additions and 98 deletions

View File

@@ -4,7 +4,7 @@ import { NextRequest, NextResponse } from 'next/server';
import { getAuthInfoFromCookie } from '@/lib/auth';
import { getConfig } from '@/lib/config';
import { getStorage } from '@/lib/db';
import { db } from '@/lib/db';
import { IStorage } from '@/lib/types';
export const runtime = 'edge';
@@ -45,7 +45,6 @@ export async function POST(request: NextRequest) {
// 获取配置与存储
const adminConfig = await getConfig();
const storage: IStorage | null = getStorage();
// 权限与身份校验
if (username !== process.env.USERNAME) {
@@ -176,9 +175,7 @@ export async function POST(request: NextRequest) {
}
// 持久化到存储
if (storage && typeof (storage as any).setAdminConfig === 'function') {
await (storage as any).setAdminConfig(adminConfig);
}
await db.saveAdminConfig(adminConfig);
return NextResponse.json(
{ ok: true },

View File

@@ -4,7 +4,7 @@ import { NextRequest, NextResponse } from 'next/server';
import { getAuthInfoFromCookie } from '@/lib/auth';
import { getConfig, refineConfig } from '@/lib/config';
import { getStorage } from '@/lib/db';
import { db } from '@/lib/db';
export async function POST(request: NextRequest) {
const storageType = process.env.NEXT_PUBLIC_STORAGE_TYPE || 'localstorage';
@@ -26,7 +26,6 @@ export async function POST(request: NextRequest) {
try {
// 检查用户权限
let adminConfig = await getConfig();
const storage = getStorage();
// 仅站长可以修改配置文件
if (username !== process.env.USERNAME) {
@@ -77,19 +76,11 @@ export async function POST(request: NextRequest) {
adminConfig = refineConfig(adminConfig);
// 更新配置文件
if (storage && typeof (storage as any).setAdminConfig === 'function') {
await (storage as any).setAdminConfig(adminConfig);
return NextResponse.json({
success: true,
message: '配置文件更新成功',
});
} else {
return NextResponse.json(
{ error: '存储服务不可用' },
{ status: 500 }
);
}
await db.saveAdminConfig(adminConfig);
return NextResponse.json({
success: true,
message: '配置文件更新成功',
});
} catch (error) {
console.error('更新配置文件失败:', error);
return NextResponse.json(

View File

@@ -7,6 +7,7 @@ import { gunzip } from 'zlib';
import { getAuthInfoFromCookie } from '@/lib/auth';
import { SimpleCrypto } from '@/lib/crypto';
import { db } from '@/lib/db';
import { setCachedConfig } from '@/lib/config';
const gunzipAsync = promisify(gunzip);
@@ -79,6 +80,7 @@ export async function POST(req: NextRequest) {
// 导入管理员配置
await db.saveAdminConfig(importData.data.adminConfig);
await setCachedConfig(importData.data.adminConfig);
// 导入用户数据
const userData = importData.data.userData;

View File

@@ -4,7 +4,7 @@ import { NextRequest, NextResponse } from 'next/server';
import { getAuthInfoFromCookie } from '@/lib/auth';
import { getConfig } from '@/lib/config';
import { getStorage } from '@/lib/db';
import { db } from '@/lib/db';
export const runtime = 'edge';
@@ -66,7 +66,6 @@ export async function POST(request: NextRequest) {
}
const adminConfig = await getConfig();
const storage = getStorage();
// 权限校验
if (username !== process.env.USERNAME) {
@@ -93,9 +92,7 @@ export async function POST(request: NextRequest) {
};
// 写入数据库
if (storage && typeof (storage as any).setAdminConfig === 'function') {
await (storage as any).setAdminConfig(adminConfig);
}
await db.saveAdminConfig(adminConfig);
return NextResponse.json(
{ ok: true },

View File

@@ -4,7 +4,7 @@ import { NextRequest, NextResponse } from 'next/server';
import { getAuthInfoFromCookie } from '@/lib/auth';
import { getConfig } from '@/lib/config';
import { getStorage } from '@/lib/db';
import { db } from '@/lib/db';
import { IStorage } from '@/lib/types';
export const runtime = 'edge';
@@ -45,7 +45,6 @@ export async function POST(request: NextRequest) {
// 获取配置与存储
const adminConfig = await getConfig();
const storage: IStorage | null = getStorage();
// 权限与身份校验
if (username !== process.env.USERNAME) {
@@ -144,9 +143,7 @@ export async function POST(request: NextRequest) {
}
// 持久化到存储
if (storage && typeof (storage as any).setAdminConfig === 'function') {
await (storage as any).setAdminConfig(adminConfig);
}
await db.saveAdminConfig(adminConfig);
return NextResponse.json(
{ ok: true },

View File

@@ -4,8 +4,7 @@ import { NextRequest, NextResponse } from 'next/server';
import { getAuthInfoFromCookie } from '@/lib/auth';
import { getConfig } from '@/lib/config';
import { getStorage } from '@/lib/db';
import { IStorage } from '@/lib/types';
import { db } from '@/lib/db';
export const runtime = 'edge';
@@ -75,7 +74,6 @@ export async function POST(request: NextRequest) {
// 获取配置与存储
const adminConfig = await getConfig();
const storage: IStorage | null = getStorage();
// 判定操作者角色
let operatorRole: 'owner' | 'admin';
@@ -125,13 +123,7 @@ export async function POST(request: NextRequest) {
{ status: 400 }
);
}
if (!storage || typeof storage.registerUser !== 'function') {
return NextResponse.json(
{ error: '存储未配置用户注册' },
{ status: 500 }
);
}
await storage.registerUser(targetUsername!, targetPassword);
await db.registerUser(targetUsername!, targetPassword);
// 更新配置
adminConfig.UserConfig.Users.push({
username: targetUsername!,
@@ -139,7 +131,7 @@ export async function POST(request: NextRequest) {
});
targetEntry =
adminConfig.UserConfig.Users[
adminConfig.UserConfig.Users.length - 1
adminConfig.UserConfig.Users.length - 1
];
break;
}
@@ -254,14 +246,7 @@ export async function POST(request: NextRequest) {
);
}
if (!storage || typeof storage.changePassword !== 'function') {
return NextResponse.json(
{ error: '存储未配置密码修改功能' },
{ status: 500 }
);
}
await storage.changePassword(targetUsername!, targetPassword);
await db.changePassword(targetUsername!, targetPassword);
break;
}
case 'deleteUser': {
@@ -287,14 +272,7 @@ export async function POST(request: NextRequest) {
);
}
if (!storage || typeof storage.deleteUser !== 'function') {
return NextResponse.json(
{ error: '存储未配置用户删除功能' },
{ status: 500 }
);
}
await storage.deleteUser(targetUsername!);
await db.deleteUser(targetUsername!);
// 从配置中移除用户
const userIndex = adminConfig.UserConfig.Users.findIndex(
@@ -312,9 +290,7 @@ export async function POST(request: NextRequest) {
}
// 将更新后的配置写入数据库
if (storage && typeof (storage as any).setAdminConfig === 'function') {
await (storage as any).setAdminConfig(adminConfig);
}
await db.saveAdminConfig(adminConfig);
return NextResponse.json(
{ ok: true },

View File

@@ -3,7 +3,7 @@
import { NextRequest, NextResponse } from 'next/server';
import { getAuthInfoFromCookie } from '@/lib/auth';
import { getStorage } from '@/lib/db';
import { db } from '@/lib/db';
import { IStorage } from '@/lib/types';
export const runtime = 'edge';
@@ -46,17 +46,8 @@ export async function POST(request: NextRequest) {
);
}
// 获取存储实例
const storage: IStorage | null = getStorage();
if (!storage || typeof storage.changePassword !== 'function') {
return NextResponse.json(
{ error: '存储服务不支持修改密码' },
{ status: 500 }
);
}
// 修改密码
await storage.changePassword(username, newPassword);
await db.changePassword(username, newPassword);
return NextResponse.json({ ok: true });
} catch (error) {

View File

@@ -3,7 +3,7 @@
import { NextRequest, NextResponse } from 'next/server';
import { getConfig, refineConfig } from '@/lib/config';
import { db, getStorage } from '@/lib/db';
import { db } from '@/lib/db';
import { fetchVideoDetail } from '@/lib/fetchVideoDetail';
import { SearchResult } from '@/lib/types';
@@ -72,10 +72,7 @@ async function refreshConfig() {
config.ConfigFile = decodedContent;
config.ConfigSubscribtion.LastCheck = new Date().toISOString();
config = refineConfig(config);
const storage = getStorage();
if (storage && typeof (storage as any).setAdminConfig === 'function') {
await (storage as any).setAdminConfig(config);
}
await db.saveAdminConfig(config);
} catch (e) {
console.error('刷新配置失败:', e);
}