mirror of
https://github.com/MoonTechLab/LunaTV.git
synced 2026-03-01 08:34:43 +08:00
44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
|
|
import { getAvailableApiSites, getCacheTime } from '@/lib/config';
|
|
import { getDetailFromApi } from '@/lib/downstream';
|
|
|
|
export const runtime = 'edge';
|
|
|
|
export async function GET(request: Request) {
|
|
const { searchParams } = new URL(request.url);
|
|
const id = searchParams.get('id');
|
|
const sourceCode = searchParams.get('source');
|
|
|
|
if (!id || !sourceCode) {
|
|
return NextResponse.json({ error: '缺少必要参数' }, { status: 400 });
|
|
}
|
|
|
|
if (!/^[\w-]+$/.test(id)) {
|
|
return NextResponse.json({ error: '无效的视频ID格式' }, { status: 400 });
|
|
}
|
|
|
|
try {
|
|
const apiSites = await getAvailableApiSites();
|
|
const apiSite = apiSites.find((site) => site.key === sourceCode);
|
|
|
|
if (!apiSite) {
|
|
return NextResponse.json({ error: '无效的API来源' }, { status: 400 });
|
|
}
|
|
|
|
const result = await getDetailFromApi(apiSite, id);
|
|
const cacheTime = await getCacheTime();
|
|
|
|
return NextResponse.json(result, {
|
|
headers: {
|
|
'Cache-Control': `public, max-age=${cacheTime}`,
|
|
},
|
|
});
|
|
} catch (error) {
|
|
return NextResponse.json(
|
|
{ error: (error as Error).message },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|