From b8c31d12ab439adbb93de24f758970b6472ae341 Mon Sep 17 00:00:00 2001 From: mtvpls Date: Wed, 4 Feb 2026 16:42:05 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E6=97=A7=E7=89=88emby?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/api/admin/emby/import/route.ts | 5 +- src/app/api/admin/emby/route.ts | 90 ++------------------------ 2 files changed, 8 insertions(+), 87 deletions(-) diff --git a/src/app/api/admin/emby/import/route.ts b/src/app/api/admin/emby/import/route.ts index 99c828a..6fcc7cc 100644 --- a/src/app/api/admin/emby/import/route.ts +++ b/src/app/api/admin/emby/import/route.ts @@ -1,7 +1,7 @@ import { NextRequest, NextResponse } from 'next/server'; import { getAuthInfoFromCookie } from '@/lib/auth'; -import { getConfig } from '@/lib/config'; +import { getConfig, setCachedConfig } from '@/lib/config'; import { db } from '@/lib/db'; export const runtime = 'nodejs'; @@ -64,6 +64,9 @@ export async function POST(request: NextRequest) { await db.saveAdminConfig(adminConfig); + // 更新内存缓存 + await setCachedConfig(adminConfig); + return NextResponse.json({ success: true, message: '导入成功', diff --git a/src/app/api/admin/emby/route.ts b/src/app/api/admin/emby/route.ts index a4b5c9b..a7a224e 100644 --- a/src/app/api/admin/emby/route.ts +++ b/src/app/api/admin/emby/route.ts @@ -12,7 +12,9 @@ export const runtime = 'nodejs'; /** * POST /api/admin/emby - * 保存 Emby 配置 + * Emby 配置管理接口 + * - test: 测试 Emby 连接 + * - clearCache: 清除 Emby 缓存 */ export async function POST(request: NextRequest) { const storageType = process.env.NEXT_PUBLIC_STORAGE_TYPE || 'localstorage'; @@ -25,7 +27,7 @@ export async function POST(request: NextRequest) { try { const body = await request.json(); - const { action, Enabled, ServerURL, ApiKey, Username, Password, UserId, Libraries } = body; + const { action, ServerURL, ApiKey, Username, Password } = body; const authInfo = getAuthInfoFromCookie(request); if (!authInfo || !authInfo.username) { @@ -44,90 +46,6 @@ export async function POST(request: NextRequest) { } } - if (action === 'save') { - // 如果功能未启用,允许保存空配置 - if (!Enabled) { - adminConfig.EmbyConfig = { - Enabled: false, - ServerURL: ServerURL || '', - ApiKey: ApiKey || '', - Username: Username || '', - Password: Password || '', - Libraries: Libraries || [], - }; - await db.saveAdminConfig(adminConfig); - return NextResponse.json({ success: true, message: 'Emby 配置已保存(未启用)' }); - } - - // 验证必填字段 - if (!ServerURL) { - return NextResponse.json({ error: '请填写 Emby 服务器地址' }, { status: 400 }); - } - - if (!ApiKey && (!Username || !Password)) { - return NextResponse.json( - { error: '请填写 API Key 或用户名密码' }, - { status: 400 } - ); - } - - // 测试连接 - const testConfig = { - ServerURL, - ApiKey, - Username, - Password, - UserId, - }; - - const client = new EmbyClient(testConfig); - - // 如果使用用户名密码,先认证 - let finalUserId: string | undefined = UserId; // 使用用户提供的 UserId - let authToken: string | undefined; - if (!ApiKey && Username && Password) { - try { - const authResult = await client.authenticate(Username, Password); - finalUserId = authResult.User.Id; - authToken = authResult.AccessToken; - } catch (error) { - return NextResponse.json( - { error: 'Emby 认证失败: ' + (error as Error).message }, - { status: 400 } - ); - } - } - - // ��试连接 - const isConnected = await client.checkConnectivity(); - if (!isConnected) { - return NextResponse.json( - { error: 'Emby 连接失败,请检查服务器地址和认证信息' }, - { status: 400 } - ); - } - - // 保存配置 - adminConfig.EmbyConfig = { - Enabled: true, - ServerURL, - ApiKey: ApiKey || undefined, - Username: Username || undefined, - Password: Password || undefined, - UserId: finalUserId, - AuthToken: authToken, - Libraries: Libraries || [], - LastSyncTime: Date.now(), - }; - - await db.saveAdminConfig(adminConfig); - - return NextResponse.json({ - success: true, - message: 'Emby 配置已保存并测试成功', - }); - } - if (action === 'test') { // 测试连接 if (!ServerURL) {