emby增加导入导出

This commit is contained in:
mtvpls
2026-01-13 09:43:21 +08:00
parent 02ba79f3d8
commit 89be4aab87
3 changed files with 195 additions and 0 deletions

View File

@@ -0,0 +1,44 @@
import { NextRequest, NextResponse } from 'next/server';
import { getAuthInfoFromCookie } from '@/lib/auth';
import { getConfig } from '@/lib/config';
export const runtime = 'nodejs';
export async function GET(request: NextRequest) {
const storageType = process.env.NEXT_PUBLIC_STORAGE_TYPE || 'localstorage';
if (storageType === 'localstorage') {
return NextResponse.json(
{ error: '不支持本地存储进行管理员配置' },
{ status: 400 }
);
}
try {
const authInfo = getAuthInfoFromCookie(request);
if (!authInfo || !authInfo.username) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
// 仅站长可用
if (authInfo.username !== process.env.USERNAME) {
return NextResponse.json({ error: '权限不足,仅站长可用' }, { status: 403 });
}
const adminConfig = await getConfig();
const embyConfig = adminConfig.EmbyConfig || {};
const exportData = JSON.stringify(embyConfig, null, 2);
return new NextResponse(exportData, {
headers: {
'Content-Type': 'application/json',
'Content-Disposition': `attachment; filename="emby-config-${Date.now()}.json"`,
},
});
} catch (error) {
return NextResponse.json(
{ error: '导出失败: ' + (error as Error).message },
{ status: 500 }
);
}
}

View File

@@ -0,0 +1,77 @@
import { NextRequest, NextResponse } from 'next/server';
import { getAuthInfoFromCookie } from '@/lib/auth';
import { getConfig } from '@/lib/config';
import { db } from '@/lib/db';
export const runtime = 'nodejs';
export async function POST(request: NextRequest) {
const storageType = process.env.NEXT_PUBLIC_STORAGE_TYPE || 'localstorage';
if (storageType === 'localstorage') {
return NextResponse.json(
{ error: '不支持本地存储进行管理员配置' },
{ status: 400 }
);
}
try {
const authInfo = getAuthInfoFromCookie(request);
if (!authInfo || !authInfo.username) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
// 仅站长可用
if (authInfo.username !== process.env.USERNAME) {
return NextResponse.json({ error: '权限不足,仅站长可用' }, { status: 403 });
}
const body = await request.json();
const { data } = body;
if (!data) {
return NextResponse.json({ error: '缺少导入数据' }, { status: 400 });
}
const adminConfig = await getConfig();
// 追加和覆盖合并Sources数组
if (data.Sources && Array.isArray(data.Sources)) {
const existingSources = adminConfig.EmbyConfig?.Sources || [];
const existingKeys = new Set(existingSources.map(s => s.key));
// 覆盖已存在的,追加新的
const mergedSources = [...existingSources];
for (const importSource of data.Sources) {
const existingIndex = mergedSources.findIndex(s => s.key === importSource.key);
if (existingIndex >= 0) {
mergedSources[existingIndex] = importSource;
} else {
mergedSources.push(importSource);
}
}
adminConfig.EmbyConfig = {
...adminConfig.EmbyConfig,
Sources: mergedSources,
};
} else {
// 旧格式:直接覆盖
adminConfig.EmbyConfig = {
...adminConfig.EmbyConfig,
...data,
};
}
await db.saveAdminConfig(adminConfig);
return NextResponse.json({
success: true,
message: '导入成功',
});
} catch (error) {
return NextResponse.json(
{ error: '导入失败: ' + (error as Error).message },
{ status: 500 }
);
}
}