新增私人影视库功能
This commit is contained in:
79
src/app/api/admin/openlist/route.ts
Normal file
79
src/app/api/admin/openlist/route.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
/* eslint-disable no-console */
|
||||
|
||||
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';
|
||||
|
||||
/**
|
||||
* POST /api/admin/openlist
|
||||
* 保存 OpenList 配置
|
||||
*/
|
||||
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 body = await request.json();
|
||||
const { action, URL, Token, RootPath } = body;
|
||||
|
||||
const authInfo = getAuthInfoFromCookie(request);
|
||||
if (!authInfo || !authInfo.username) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
const username = authInfo.username;
|
||||
|
||||
// 获取配置
|
||||
const adminConfig = await getConfig();
|
||||
|
||||
// 权限检查
|
||||
if (username !== process.env.USERNAME) {
|
||||
const userEntry = adminConfig.UserConfig.Users.find(
|
||||
(u) => u.username === username
|
||||
);
|
||||
if (!userEntry || userEntry.role !== 'admin' || userEntry.banned) {
|
||||
return NextResponse.json({ error: '权限不足' }, { status: 401 });
|
||||
}
|
||||
}
|
||||
|
||||
if (action === 'save') {
|
||||
// 保存配置
|
||||
if (!URL || !Token) {
|
||||
return NextResponse.json({ error: '缺少必要参数' }, { status: 400 });
|
||||
}
|
||||
|
||||
adminConfig.OpenListConfig = {
|
||||
URL,
|
||||
Token,
|
||||
RootPath: RootPath || '/',
|
||||
LastRefreshTime: adminConfig.OpenListConfig?.LastRefreshTime,
|
||||
ResourceCount: adminConfig.OpenListConfig?.ResourceCount,
|
||||
};
|
||||
|
||||
await db.saveAdminConfig(adminConfig);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '保存成功',
|
||||
});
|
||||
}
|
||||
|
||||
return NextResponse.json({ error: '未知操作' }, { status: 400 });
|
||||
} catch (error) {
|
||||
console.error('OpenList 配置操作失败:', error);
|
||||
return NextResponse.json(
|
||||
{ error: '操作失败', details: (error as Error).message },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -66,6 +66,13 @@ export async function POST(request: NextRequest) {
|
||||
if (!key || !name || !api) {
|
||||
return NextResponse.json({ error: '缺少必要参数' }, { status: 400 });
|
||||
}
|
||||
// 禁止添加 openlist 保留关键字
|
||||
if (key === 'openlist') {
|
||||
return NextResponse.json(
|
||||
{ error: 'openlist 是保留关键字,不能作为视频源 key' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
if (adminConfig.SourceConfig.some((s) => s.key === key)) {
|
||||
return NextResponse.json({ error: '该源已存在' }, { status: 400 });
|
||||
}
|
||||
|
||||
@@ -1,7 +1,16 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any, no-console */
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
import { getConfig } from '@/lib/config';
|
||||
import { OpenListClient } from '@/lib/openlist.client';
|
||||
import {
|
||||
getCachedMetaInfo,
|
||||
MetaInfo,
|
||||
setCachedMetaInfo,
|
||||
} from '@/lib/openlist-cache';
|
||||
import { getTMDBImageUrl } from '@/lib/tmdb.search';
|
||||
|
||||
export const runtime = 'nodejs';
|
||||
|
||||
/**
|
||||
@@ -21,6 +30,11 @@ export async function GET(request: NextRequest) {
|
||||
);
|
||||
}
|
||||
|
||||
// 特殊处理 openlist
|
||||
if (apiUrl === 'openlist') {
|
||||
return handleOpenListProxy(request);
|
||||
}
|
||||
|
||||
// 构建完整的 API 请求 URL,包含所有查询参数
|
||||
const targetUrl = new URL(apiUrl);
|
||||
|
||||
@@ -237,3 +251,177 @@ function processUrl(url: string, playFrom: string, proxyOrigin: string, tokenPar
|
||||
// 非 m3u8 链接不处理
|
||||
return url;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理 OpenList 代理请求
|
||||
*/
|
||||
async function handleOpenListProxy(request: NextRequest) {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const wd = searchParams.get('wd'); // 搜索关键词
|
||||
const ids = searchParams.get('ids'); // 详情ID
|
||||
|
||||
const config = await getConfig();
|
||||
const openListConfig = config.OpenListConfig;
|
||||
|
||||
if (!openListConfig || !openListConfig.URL || !openListConfig.Token) {
|
||||
return NextResponse.json(
|
||||
{ code: 0, msg: 'OpenList 未配置', list: [] },
|
||||
{ status: 200 }
|
||||
);
|
||||
}
|
||||
|
||||
const rootPath = openListConfig.RootPath || '/';
|
||||
const client = new OpenListClient(openListConfig.URL, openListConfig.Token);
|
||||
|
||||
// 读取 metainfo.json
|
||||
let metaInfo: MetaInfo | null = getCachedMetaInfo(rootPath);
|
||||
|
||||
if (!metaInfo) {
|
||||
try {
|
||||
const metainfoPath = `${rootPath}${rootPath.endsWith('/') ? '' : '/'}metainfo.json`;
|
||||
const fileResponse = await client.getFile(metainfoPath);
|
||||
|
||||
if (fileResponse.code === 200 && fileResponse.data.raw_url) {
|
||||
const downloadUrl = fileResponse.data.raw_url;
|
||||
const contentResponse = await fetch(downloadUrl, {
|
||||
headers: {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
||||
'Accept': '*/*',
|
||||
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
|
||||
},
|
||||
});
|
||||
const content = await contentResponse.text();
|
||||
metaInfo = JSON.parse(content);
|
||||
setCachedMetaInfo(rootPath, metaInfo);
|
||||
}
|
||||
} catch (error) {
|
||||
return NextResponse.json(
|
||||
{ code: 0, msg: 'metainfo.json 不存在', list: [] },
|
||||
{ status: 200 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (!metaInfo) {
|
||||
return NextResponse.json(
|
||||
{ code: 0, msg: '无数据', list: [] },
|
||||
{ status: 200 }
|
||||
);
|
||||
}
|
||||
|
||||
// 搜索模式
|
||||
if (wd) {
|
||||
const results = Object.entries(metaInfo.folders)
|
||||
.filter(
|
||||
([folderName, info]) =>
|
||||
folderName.toLowerCase().includes(wd.toLowerCase()) ||
|
||||
info.title.toLowerCase().includes(wd.toLowerCase())
|
||||
)
|
||||
.map(([folderName, info]) => ({
|
||||
vod_id: folderName,
|
||||
vod_name: info.title,
|
||||
vod_pic: getTMDBImageUrl(info.poster_path),
|
||||
vod_remarks: info.media_type === 'movie' ? '电影' : '剧集',
|
||||
vod_year: info.release_date.split('-')[0] || '',
|
||||
type_name: info.media_type === 'movie' ? '电影' : '电视剧',
|
||||
}));
|
||||
|
||||
return NextResponse.json({
|
||||
code: 1,
|
||||
msg: '数据列表',
|
||||
page: 1,
|
||||
pagecount: 1,
|
||||
limit: results.length,
|
||||
total: results.length,
|
||||
list: results,
|
||||
});
|
||||
}
|
||||
|
||||
// 详情模式
|
||||
if (ids) {
|
||||
const folderName = ids;
|
||||
const info = metaInfo.folders[folderName];
|
||||
|
||||
if (!info) {
|
||||
return NextResponse.json(
|
||||
{ code: 0, msg: '视频不存在', list: [] },
|
||||
{ status: 200 }
|
||||
);
|
||||
}
|
||||
|
||||
// 获取视频详情
|
||||
try {
|
||||
const detailResponse = await fetch(
|
||||
`${request.headers.get('x-forwarded-proto') || 'http'}://${request.headers.get('host')}/api/openlist/detail?folder=${encodeURIComponent(folderName)}`
|
||||
);
|
||||
|
||||
if (!detailResponse.ok) {
|
||||
throw new Error('获取视频详情失败');
|
||||
}
|
||||
|
||||
const detailData = await detailResponse.json();
|
||||
|
||||
if (!detailData.success) {
|
||||
throw new Error('获取视频详情失败');
|
||||
}
|
||||
|
||||
// 构建播放列表
|
||||
const playUrls = detailData.episodes
|
||||
.map((ep: any) => {
|
||||
const title = ep.title || `第${ep.episode}集`;
|
||||
return `${title}$${ep.playUrl}`;
|
||||
})
|
||||
.join('#');
|
||||
|
||||
return NextResponse.json({
|
||||
code: 1,
|
||||
msg: '数据列表',
|
||||
page: 1,
|
||||
pagecount: 1,
|
||||
limit: 1,
|
||||
total: 1,
|
||||
list: [
|
||||
{
|
||||
vod_id: folderName,
|
||||
vod_name: info.title,
|
||||
vod_pic: getTMDBImageUrl(info.poster_path),
|
||||
vod_remarks: info.media_type === 'movie' ? '电影' : '剧集',
|
||||
vod_year: info.release_date.split('-')[0] || '',
|
||||
vod_content: info.overview,
|
||||
vod_play_from: 'OpenList',
|
||||
vod_play_url: playUrls,
|
||||
type_name: info.media_type === 'movie' ? '电影' : '电视剧',
|
||||
},
|
||||
],
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('获取 OpenList 视频详情失败:', error);
|
||||
return NextResponse.json(
|
||||
{ code: 0, msg: '获取详情失败', list: [] },
|
||||
{ status: 200 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// 默认返回所有视频
|
||||
const results = Object.entries(metaInfo.folders).map(
|
||||
([folderName, info]) => ({
|
||||
vod_id: folderName,
|
||||
vod_name: info.title,
|
||||
vod_pic: getTMDBImageUrl(info.poster_path),
|
||||
vod_remarks: info.media_type === 'movie' ? '电影' : '剧集',
|
||||
vod_year: info.release_date.split('-')[0] || '',
|
||||
type_name: info.media_type === 'movie' ? '电影' : '电视剧',
|
||||
})
|
||||
);
|
||||
|
||||
return NextResponse.json({
|
||||
code: 1,
|
||||
msg: '数据列表',
|
||||
page: 1,
|
||||
pagecount: 1,
|
||||
limit: results.length,
|
||||
total: results.length,
|
||||
list: results,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
import { getAuthInfoFromCookie } from '@/lib/auth';
|
||||
import { getAvailableApiSites, getCacheTime } from '@/lib/config';
|
||||
import { getAvailableApiSites, getCacheTime, getConfig } from '@/lib/config';
|
||||
import { getDetailFromApi } from '@/lib/downstream';
|
||||
|
||||
export const runtime = 'nodejs';
|
||||
@@ -20,6 +20,100 @@ export async function GET(request: NextRequest) {
|
||||
return NextResponse.json({ error: '缺少必要参数' }, { status: 400 });
|
||||
}
|
||||
|
||||
// 特殊处理 openlist 源
|
||||
if (sourceCode === 'openlist') {
|
||||
try {
|
||||
const config = await getConfig();
|
||||
const openListConfig = config.OpenListConfig;
|
||||
|
||||
if (!openListConfig || !openListConfig.URL || !openListConfig.Token) {
|
||||
throw new Error('OpenList 未配置');
|
||||
}
|
||||
|
||||
const rootPath = openListConfig.RootPath || '/';
|
||||
|
||||
// 1. 读取 metainfo.json 获取元数据
|
||||
let metaInfo: any = null;
|
||||
try {
|
||||
const { OpenListClient } = await import('@/lib/openlist.client');
|
||||
const { getCachedMetaInfo } = await import('@/lib/openlist-cache');
|
||||
const { getTMDBImageUrl } = await import('@/lib/tmdb.search');
|
||||
|
||||
const client = new OpenListClient(openListConfig.URL, openListConfig.Token);
|
||||
metaInfo = getCachedMetaInfo(rootPath);
|
||||
|
||||
if (!metaInfo) {
|
||||
const metainfoPath = `${rootPath}${rootPath.endsWith('/') ? '' : '/'}metainfo.json`;
|
||||
const fileResponse = await client.getFile(metainfoPath);
|
||||
|
||||
if (fileResponse.code === 200 && fileResponse.data.raw_url) {
|
||||
const downloadUrl = fileResponse.data.raw_url;
|
||||
const contentResponse = await fetch(downloadUrl, {
|
||||
headers: {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
||||
'Accept': '*/*',
|
||||
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
|
||||
},
|
||||
});
|
||||
const content = await contentResponse.text();
|
||||
metaInfo = JSON.parse(content);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[Detail] 读取 metainfo.json 失败:', error);
|
||||
}
|
||||
|
||||
// 2. 调用 openlist detail API
|
||||
const openlistResponse = await fetch(
|
||||
`${request.headers.get('x-forwarded-proto') || 'http'}://${request.headers.get('host')}/api/openlist/detail?folder=${encodeURIComponent(id)}`,
|
||||
{
|
||||
headers: {
|
||||
Cookie: request.headers.get('cookie') || '',
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
if (!openlistResponse.ok) {
|
||||
throw new Error('获取 OpenList 视频详情失败');
|
||||
}
|
||||
|
||||
const openlistData = await openlistResponse.json();
|
||||
|
||||
if (!openlistData.success) {
|
||||
throw new Error(openlistData.error || '获取视频详情失败');
|
||||
}
|
||||
|
||||
// 3. 从 metainfo 中获取元数据
|
||||
const folderMeta = metaInfo?.folders?.[id];
|
||||
const { getTMDBImageUrl } = await import('@/lib/tmdb.search');
|
||||
|
||||
// 转换为标准格式(使用懒加载 URL)
|
||||
const result = {
|
||||
source: 'openlist',
|
||||
source_name: '私人影库',
|
||||
id: openlistData.folder,
|
||||
title: folderMeta?.title || openlistData.folder,
|
||||
poster: folderMeta?.poster_path ? getTMDBImageUrl(folderMeta.poster_path) : '',
|
||||
year: folderMeta?.release_date ? folderMeta.release_date.split('-')[0] : '',
|
||||
douban_id: 0,
|
||||
desc: folderMeta?.overview || '',
|
||||
episodes: openlistData.episodes.map((ep: any) =>
|
||||
`/api/openlist/play?folder=${encodeURIComponent(openlistData.folder)}&fileName=${encodeURIComponent(ep.fileName)}`
|
||||
),
|
||||
episodes_titles: openlistData.episodes.map((ep: any) => ep.title || `第${ep.episode}集`),
|
||||
};
|
||||
|
||||
console.log('[Detail] result.episodes_titles:', result.episodes_titles);
|
||||
|
||||
return NextResponse.json(result);
|
||||
} catch (error) {
|
||||
return NextResponse.json(
|
||||
{ error: (error as Error).message },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (!/^[\w-]+$/.test(id)) {
|
||||
return NextResponse.json({ error: '无效的视频ID格式' }, { status: 400 });
|
||||
}
|
||||
|
||||
228
src/app/api/openlist/detail/route.ts
Normal file
228
src/app/api/openlist/detail/route.ts
Normal file
@@ -0,0 +1,228 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any, no-console */
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
import { getAuthInfoFromCookie } from '@/lib/auth';
|
||||
import { getConfig } from '@/lib/config';
|
||||
import { OpenListClient } from '@/lib/openlist.client';
|
||||
import {
|
||||
getCachedVideoInfo,
|
||||
setCachedVideoInfo,
|
||||
VideoInfo,
|
||||
} from '@/lib/openlist-cache';
|
||||
import { parseVideoFileName } from '@/lib/video-parser';
|
||||
|
||||
export const runtime = 'nodejs';
|
||||
|
||||
/**
|
||||
* GET /api/openlist/detail?folder=xxx
|
||||
* 获取视频文件夹的详细信息
|
||||
*/
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const authInfo = getAuthInfoFromCookie(request);
|
||||
if (!authInfo || !authInfo.username) {
|
||||
return NextResponse.json({ error: '未授权' }, { status: 401 });
|
||||
}
|
||||
|
||||
const { searchParams } = new URL(request.url);
|
||||
const folderName = searchParams.get('folder');
|
||||
|
||||
if (!folderName) {
|
||||
return NextResponse.json({ error: '缺少参数' }, { status: 400 });
|
||||
}
|
||||
|
||||
const config = await getConfig();
|
||||
const openListConfig = config.OpenListConfig;
|
||||
|
||||
if (!openListConfig || !openListConfig.URL || !openListConfig.Token) {
|
||||
return NextResponse.json({ error: 'OpenList 未配置' }, { status: 400 });
|
||||
}
|
||||
|
||||
const rootPath = openListConfig.RootPath || '/';
|
||||
const folderPath = `${rootPath}${rootPath.endsWith('/') ? '' : '/'}${folderName}`;
|
||||
const client = new OpenListClient(openListConfig.URL, openListConfig.Token);
|
||||
|
||||
// 1. 尝试读取缓存的 videoinfo.json
|
||||
let videoInfo: VideoInfo | null = getCachedVideoInfo(folderPath);
|
||||
|
||||
if (!videoInfo) {
|
||||
// 2. 尝试从 OpenList 读取 videoinfo.json
|
||||
try {
|
||||
const videoinfoPath = `${folderPath}/videoinfo.json`;
|
||||
const fileResponse = await client.getFile(videoinfoPath);
|
||||
|
||||
if (fileResponse.code === 200 && fileResponse.data.raw_url) {
|
||||
const downloadUrl = fileResponse.data.raw_url;
|
||||
const contentResponse = await fetch(downloadUrl, {
|
||||
headers: {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
||||
'Accept': '*/*',
|
||||
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
|
||||
},
|
||||
});
|
||||
const content = await contentResponse.text();
|
||||
videoInfo = JSON.parse(content);
|
||||
|
||||
// 缓存
|
||||
if (videoInfo) {
|
||||
setCachedVideoInfo(folderPath, videoInfo);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.log('videoinfo.json 不存在,将解析文件名');
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 如果没有 videoinfo.json,列出文件夹并解析
|
||||
if (!videoInfo) {
|
||||
const listResponse = await client.listDirectory(folderPath);
|
||||
|
||||
if (listResponse.code !== 200) {
|
||||
return NextResponse.json(
|
||||
{ error: 'OpenList 列表获取失败' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
|
||||
// 过滤视频文件
|
||||
const videoFiles = listResponse.data.content.filter(
|
||||
(item) =>
|
||||
!item.is_dir &&
|
||||
!item.name.endsWith('.json') && // 排除 JSON 文件
|
||||
!item.name.startsWith('.') && // 排除隐藏文件
|
||||
(item.name.endsWith('.mp4') ||
|
||||
item.name.endsWith('.mkv') ||
|
||||
item.name.endsWith('.avi') ||
|
||||
item.name.endsWith('.m3u8') ||
|
||||
item.name.endsWith('.flv') ||
|
||||
item.name.endsWith('.ts'))
|
||||
);
|
||||
|
||||
videoInfo = {
|
||||
episodes: {},
|
||||
last_updated: Date.now(),
|
||||
};
|
||||
|
||||
// 按文件名排序,确保顺序一致
|
||||
videoFiles.sort((a, b) => a.name.localeCompare(b.name));
|
||||
|
||||
// 解析文件名
|
||||
for (let i = 0; i < videoFiles.length; i++) {
|
||||
const file = videoFiles[i];
|
||||
const parsed = parseVideoFileName(file.name);
|
||||
|
||||
videoInfo.episodes[file.name] = {
|
||||
episode: parsed.episode || (i + 1), // 如果解析失败,使用索引+1作为集数
|
||||
season: parsed.season,
|
||||
title: parsed.title,
|
||||
parsed_from: 'filename',
|
||||
};
|
||||
}
|
||||
|
||||
// 保存 videoinfo.json
|
||||
const videoinfoPath = `${folderPath}/videoinfo.json`;
|
||||
await client.uploadFile(
|
||||
videoinfoPath,
|
||||
JSON.stringify(videoInfo, null, 2)
|
||||
);
|
||||
|
||||
// 缓存
|
||||
setCachedVideoInfo(folderPath, videoInfo);
|
||||
}
|
||||
|
||||
// 4. 获取视频文件列表(不获取播放链接,使用懒加载)
|
||||
const listResponse = await client.listDirectory(folderPath);
|
||||
|
||||
// 定义视频文件扩展名(不区分大小写)
|
||||
const videoExtensions = [
|
||||
'.mp4', '.mkv', '.avi', '.m3u8', '.flv', '.ts',
|
||||
'.mov', '.wmv', '.webm', '.rmvb', '.rm', '.mpg',
|
||||
'.mpeg', '.3gp', '.f4v', '.m4v', '.vob'
|
||||
];
|
||||
|
||||
const videoFiles = listResponse.data.content.filter((item) => {
|
||||
// 排除文件夹
|
||||
if (item.is_dir) return false;
|
||||
|
||||
// 排除隐藏文件
|
||||
if (item.name.startsWith('.')) return false;
|
||||
|
||||
// 排除 JSON 文件
|
||||
if (item.name.endsWith('.json')) return false;
|
||||
|
||||
// 检查是否是视频文件(不区分大小写)
|
||||
const lowerName = item.name.toLowerCase();
|
||||
return videoExtensions.some(ext => lowerName.endsWith(ext));
|
||||
});
|
||||
|
||||
// 5. 构建集数信息(不包含播放链接)
|
||||
// 确保所有视频文件都被显示,即使 videoInfo 中没有记录
|
||||
const episodes = videoFiles
|
||||
.map((file, index) => {
|
||||
// 总是重新解析文件名,确保使用最新的解析逻辑
|
||||
const parsed = parseVideoFileName(file.name);
|
||||
|
||||
// 如果解析成功,使用解析结果;否则使用 videoInfo 中的记录或索引
|
||||
let episodeInfo;
|
||||
if (parsed.episode) {
|
||||
episodeInfo = {
|
||||
episode: parsed.episode,
|
||||
season: parsed.season,
|
||||
title: parsed.title,
|
||||
parsed_from: 'filename',
|
||||
};
|
||||
} else {
|
||||
// 如果解析失败,尝试从 videoInfo 获取
|
||||
episodeInfo = videoInfo!.episodes[file.name];
|
||||
if (!episodeInfo) {
|
||||
// 如果 videoInfo 中也没有,使用索引
|
||||
episodeInfo = {
|
||||
episode: index + 1,
|
||||
season: undefined,
|
||||
title: undefined,
|
||||
parsed_from: 'filename',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// 优先使用解析出的标题,其次是"第X集"格式,最后才是文件名
|
||||
let displayTitle = episodeInfo.title;
|
||||
if (!displayTitle && episodeInfo.episode) {
|
||||
// 支持小数集数显示
|
||||
displayTitle = `第${episodeInfo.episode}集`;
|
||||
}
|
||||
if (!displayTitle) {
|
||||
displayTitle = file.name;
|
||||
}
|
||||
|
||||
return {
|
||||
fileName: file.name,
|
||||
episode: episodeInfo.episode || 0,
|
||||
season: episodeInfo.season,
|
||||
title: displayTitle,
|
||||
size: file.size,
|
||||
};
|
||||
})
|
||||
.sort((a, b) => {
|
||||
// 确保排序稳定,即使 episode 相同也按文件名排序
|
||||
if (a.episode !== b.episode) {
|
||||
return a.episode - b.episode;
|
||||
}
|
||||
return a.fileName.localeCompare(b.fileName);
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
folder: folderName,
|
||||
episodes,
|
||||
videoInfo,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('获取视频详情失败:', error);
|
||||
return NextResponse.json(
|
||||
{ error: '获取失败', details: (error as Error).message },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
193
src/app/api/openlist/list/route.ts
Normal file
193
src/app/api/openlist/list/route.ts
Normal file
@@ -0,0 +1,193 @@
|
||||
/* eslint-disable no-console */
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
import { getAuthInfoFromCookie } from '@/lib/auth';
|
||||
import { getConfig } from '@/lib/config';
|
||||
import { OpenListClient } from '@/lib/openlist.client';
|
||||
import {
|
||||
getCachedMetaInfo,
|
||||
MetaInfo,
|
||||
setCachedMetaInfo,
|
||||
} from '@/lib/openlist-cache';
|
||||
import { getTMDBImageUrl } from '@/lib/tmdb.search';
|
||||
|
||||
export const runtime = 'nodejs';
|
||||
|
||||
/**
|
||||
* GET /api/openlist/list?page=1&pageSize=20
|
||||
* 获取私人影库视频列表
|
||||
*/
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const authInfo = getAuthInfoFromCookie(request);
|
||||
if (!authInfo || !authInfo.username) {
|
||||
return NextResponse.json({ error: '未授权' }, { status: 401 });
|
||||
}
|
||||
|
||||
const { searchParams } = new URL(request.url);
|
||||
const page = parseInt(searchParams.get('page') || '1');
|
||||
const pageSize = parseInt(searchParams.get('pageSize') || '20');
|
||||
|
||||
const config = await getConfig();
|
||||
const openListConfig = config.OpenListConfig;
|
||||
|
||||
if (!openListConfig || !openListConfig.URL || !openListConfig.Token) {
|
||||
return NextResponse.json(
|
||||
{ error: 'OpenList 未配置', list: [], total: 0 },
|
||||
{ status: 200 }
|
||||
);
|
||||
}
|
||||
|
||||
const rootPath = openListConfig.RootPath || '/';
|
||||
const client = new OpenListClient(openListConfig.URL, openListConfig.Token);
|
||||
|
||||
// 读取 metainfo.json
|
||||
let metaInfo: MetaInfo | null = getCachedMetaInfo(rootPath);
|
||||
|
||||
console.log('[OpenList List] 缓存检查:', {
|
||||
rootPath,
|
||||
hasCachedMetaInfo: !!metaInfo,
|
||||
});
|
||||
|
||||
if (!metaInfo) {
|
||||
try {
|
||||
const metainfoPath = `${rootPath}${rootPath.endsWith('/') ? '' : '/'}metainfo.json`;
|
||||
console.log('[OpenList List] 尝试读取 metainfo.json:', metainfoPath);
|
||||
|
||||
const fileResponse = await client.getFile(metainfoPath);
|
||||
console.log('[OpenList List] getFile 完整响应:', JSON.stringify(fileResponse, null, 2));
|
||||
|
||||
if (fileResponse.code === 200 && fileResponse.data.raw_url) {
|
||||
console.log('[OpenList List] 使用 raw_url 获取文件内容');
|
||||
|
||||
const downloadUrl = fileResponse.data.raw_url;
|
||||
console.log('[OpenList List] 下载 URL:', downloadUrl);
|
||||
|
||||
const contentResponse = await fetch(downloadUrl, {
|
||||
headers: {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
||||
'Accept': '*/*',
|
||||
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
|
||||
},
|
||||
});
|
||||
console.log('[OpenList List] fetch 响应:', {
|
||||
status: contentResponse.status,
|
||||
ok: contentResponse.ok,
|
||||
});
|
||||
|
||||
if (!contentResponse.ok) {
|
||||
throw new Error(`获取文件内容失败: ${contentResponse.status}`);
|
||||
}
|
||||
|
||||
const content = await contentResponse.text();
|
||||
console.log('[OpenList List] 文件内容长度:', content.length);
|
||||
console.log('[OpenList List] 文件内容预览:', content.substring(0, 200));
|
||||
|
||||
try {
|
||||
metaInfo = JSON.parse(content);
|
||||
console.log('[OpenList List] JSON 解析成功');
|
||||
console.log('[OpenList List] metaInfo 结构:', {
|
||||
hasfolders: !!metaInfo?.folders,
|
||||
foldersType: typeof metaInfo?.folders,
|
||||
keys: metaInfo?.folders ? Object.keys(metaInfo.folders) : [],
|
||||
});
|
||||
|
||||
// 验证数据结构
|
||||
if (!metaInfo || typeof metaInfo !== 'object') {
|
||||
throw new Error('metaInfo 不是有效对象');
|
||||
}
|
||||
if (!metaInfo.folders || typeof metaInfo.folders !== 'object') {
|
||||
throw new Error('metaInfo.folders 不存在或不是对象');
|
||||
}
|
||||
|
||||
console.log('[OpenList List] 解析成功,视频数量:', Object.keys(metaInfo.folders).length);
|
||||
setCachedMetaInfo(rootPath, metaInfo);
|
||||
} catch (parseError) {
|
||||
console.error('[OpenList List] JSON 解析或验证失败:', parseError);
|
||||
throw new Error(`JSON 解析失败: ${(parseError as Error).message}`);
|
||||
}
|
||||
} else {
|
||||
console.error('[OpenList List] getFile 失败或无 sign:', {
|
||||
code: fileResponse.code,
|
||||
message: fileResponse.message,
|
||||
data: fileResponse.data,
|
||||
});
|
||||
throw new Error(`getFile 返回错误: code=${fileResponse.code}, message=${fileResponse.message}`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[OpenList List] 读取 metainfo.json 失败:', error);
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'metainfo.json 读取失败',
|
||||
details: (error as Error).message,
|
||||
list: [],
|
||||
total: 0,
|
||||
},
|
||||
{ status: 200 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (!metaInfo) {
|
||||
console.error('[OpenList List] metaInfo 为 null');
|
||||
return NextResponse.json(
|
||||
{ error: '无数据', list: [], total: 0 },
|
||||
{ status: 200 }
|
||||
);
|
||||
}
|
||||
|
||||
// 验证 metaInfo 结构
|
||||
if (!metaInfo.folders || typeof metaInfo.folders !== 'object') {
|
||||
console.error('[OpenList List] metaInfo.folders 无效:', {
|
||||
hasfolders: !!metaInfo.folders,
|
||||
foldersType: typeof metaInfo.folders,
|
||||
metaInfoKeys: Object.keys(metaInfo),
|
||||
});
|
||||
return NextResponse.json(
|
||||
{ error: 'metainfo.json 结构无效', list: [], total: 0 },
|
||||
{ status: 200 }
|
||||
);
|
||||
}
|
||||
|
||||
console.log('[OpenList List] 开始转换视频列表,视频数:', Object.keys(metaInfo.folders).length);
|
||||
|
||||
// 转换为数组并分页
|
||||
const allVideos = Object.entries(metaInfo.folders).map(
|
||||
([folderName, info]) => ({
|
||||
id: folderName,
|
||||
folder: folderName,
|
||||
title: info.title,
|
||||
poster: getTMDBImageUrl(info.poster_path),
|
||||
releaseDate: info.release_date,
|
||||
overview: info.overview,
|
||||
voteAverage: info.vote_average,
|
||||
mediaType: info.media_type,
|
||||
lastUpdated: info.last_updated,
|
||||
})
|
||||
);
|
||||
|
||||
// 按更新时间倒序排序
|
||||
allVideos.sort((a, b) => b.lastUpdated - a.lastUpdated);
|
||||
|
||||
const total = allVideos.length;
|
||||
const start = (page - 1) * pageSize;
|
||||
const end = start + pageSize;
|
||||
const list = allVideos.slice(start, end);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
list,
|
||||
total,
|
||||
page,
|
||||
pageSize,
|
||||
totalPages: Math.ceil(total / pageSize),
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('获取视频列表失败:', error);
|
||||
return NextResponse.json(
|
||||
{ error: '获取失败', details: (error as Error).message, list: [], total: 0 },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
68
src/app/api/openlist/play/route.ts
Normal file
68
src/app/api/openlist/play/route.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any, no-console */
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
import { getAuthInfoFromCookie } from '@/lib/auth';
|
||||
import { getConfig } from '@/lib/config';
|
||||
import { OpenListClient } from '@/lib/openlist.client';
|
||||
|
||||
export const runtime = 'nodejs';
|
||||
|
||||
/**
|
||||
* GET /api/openlist/play?folder=xxx&fileName=xxx
|
||||
* 获取单个视频文件的播放链接(懒加载)
|
||||
* 返回重定向到真实播放 URL
|
||||
*/
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const authInfo = getAuthInfoFromCookie(request);
|
||||
if (!authInfo || !authInfo.username) {
|
||||
return NextResponse.json({ error: '未授权' }, { status: 401 });
|
||||
}
|
||||
|
||||
const { searchParams } = new URL(request.url);
|
||||
const folderName = searchParams.get('folder');
|
||||
const fileName = searchParams.get('fileName');
|
||||
|
||||
if (!folderName || !fileName) {
|
||||
return NextResponse.json({ error: '缺少参数' }, { status: 400 });
|
||||
}
|
||||
|
||||
const config = await getConfig();
|
||||
const openListConfig = config.OpenListConfig;
|
||||
|
||||
if (!openListConfig || !openListConfig.URL || !openListConfig.Token) {
|
||||
return NextResponse.json({ error: 'OpenList 未配置' }, { status: 400 });
|
||||
}
|
||||
|
||||
const rootPath = openListConfig.RootPath || '/';
|
||||
const folderPath = `${rootPath}${rootPath.endsWith('/') ? '' : '/'}${folderName}`;
|
||||
const filePath = `${folderPath}/${fileName}`;
|
||||
|
||||
const client = new OpenListClient(openListConfig.URL, openListConfig.Token);
|
||||
|
||||
// 获取文件的播放链接
|
||||
const fileResponse = await client.getFile(filePath);
|
||||
|
||||
if (fileResponse.code !== 200 || !fileResponse.data.raw_url) {
|
||||
console.error('[OpenList Play] 获取播放URL失败:', {
|
||||
fileName,
|
||||
code: fileResponse.code,
|
||||
message: fileResponse.message,
|
||||
});
|
||||
return NextResponse.json(
|
||||
{ error: '获取播放链接失败' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
|
||||
// 返回重定向到真实播放 URL
|
||||
return NextResponse.redirect(fileResponse.data.raw_url);
|
||||
} catch (error) {
|
||||
console.error('获取播放链接失败:', error);
|
||||
return NextResponse.json(
|
||||
{ error: '获取失败', details: (error as Error).message },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
64
src/app/api/openlist/refresh-video/route.ts
Normal file
64
src/app/api/openlist/refresh-video/route.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
/* eslint-disable no-console */
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
import { getAuthInfoFromCookie } from '@/lib/auth';
|
||||
import { getConfig } from '@/lib/config';
|
||||
import { OpenListClient } from '@/lib/openlist.client';
|
||||
import { invalidateVideoInfoCache } from '@/lib/openlist-cache';
|
||||
|
||||
export const runtime = 'nodejs';
|
||||
|
||||
/**
|
||||
* POST /api/openlist/refresh-video
|
||||
* 刷新单个视频的 videoinfo.json
|
||||
*/
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const authInfo = getAuthInfoFromCookie(request);
|
||||
if (!authInfo || !authInfo.username) {
|
||||
return NextResponse.json({ error: '未授权' }, { status: 401 });
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const { folder } = body;
|
||||
|
||||
if (!folder) {
|
||||
return NextResponse.json({ error: '缺少参数' }, { status: 400 });
|
||||
}
|
||||
|
||||
const config = await getConfig();
|
||||
const openListConfig = config.OpenListConfig;
|
||||
|
||||
if (!openListConfig || !openListConfig.URL || !openListConfig.Token) {
|
||||
return NextResponse.json({ error: 'OpenList 未配置' }, { status: 400 });
|
||||
}
|
||||
|
||||
const rootPath = openListConfig.RootPath || '/';
|
||||
const folderPath = `${rootPath}${rootPath.endsWith('/') ? '' : '/'}${folder}`;
|
||||
const client = new OpenListClient(openListConfig.URL, openListConfig.Token);
|
||||
|
||||
// 删除 videoinfo.json
|
||||
const videoinfoPath = `${folderPath}/videoinfo.json`;
|
||||
|
||||
try {
|
||||
await client.deleteFile(videoinfoPath);
|
||||
} catch (error) {
|
||||
console.log('videoinfo.json 不存在或删除失败');
|
||||
}
|
||||
|
||||
// 清除缓存
|
||||
invalidateVideoInfoCache(folderPath);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '刷新成功',
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('刷新视频失败:', error);
|
||||
return NextResponse.json(
|
||||
{ error: '刷新失败', details: (error as Error).message },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
280
src/app/api/openlist/refresh/route.ts
Normal file
280
src/app/api/openlist/refresh/route.ts
Normal file
@@ -0,0 +1,280 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any, no-console */
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
import { getAuthInfoFromCookie } from '@/lib/auth';
|
||||
import { getConfig } from '@/lib/config';
|
||||
import { db } from '@/lib/db';
|
||||
import { OpenListClient } from '@/lib/openlist.client';
|
||||
import {
|
||||
getCachedMetaInfo,
|
||||
invalidateMetaInfoCache,
|
||||
MetaInfo,
|
||||
setCachedMetaInfo,
|
||||
} from '@/lib/openlist-cache';
|
||||
import { searchTMDB } from '@/lib/tmdb.search';
|
||||
|
||||
export const runtime = 'nodejs';
|
||||
|
||||
/**
|
||||
* POST /api/openlist/refresh
|
||||
* 刷新私人影库元数据
|
||||
*/
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
// 权限检查
|
||||
const authInfo = getAuthInfoFromCookie(request);
|
||||
if (!authInfo || !authInfo.username) {
|
||||
return NextResponse.json({ error: '未授权' }, { status: 401 });
|
||||
}
|
||||
|
||||
// 获取配置
|
||||
const config = await getConfig();
|
||||
const openListConfig = config.OpenListConfig;
|
||||
|
||||
if (!openListConfig || !openListConfig.URL || !openListConfig.Token) {
|
||||
return NextResponse.json(
|
||||
{ error: 'OpenList 未配置' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const tmdbApiKey = config.SiteConfig.TMDBApiKey;
|
||||
const tmdbProxy = config.SiteConfig.TMDBProxy;
|
||||
|
||||
if (!tmdbApiKey) {
|
||||
return NextResponse.json(
|
||||
{ error: 'TMDB API Key 未配置' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const rootPath = openListConfig.RootPath || '/';
|
||||
const client = new OpenListClient(openListConfig.URL, openListConfig.Token);
|
||||
|
||||
console.log('[OpenList Refresh] 开始刷新:', {
|
||||
rootPath,
|
||||
url: openListConfig.URL,
|
||||
hasToken: !!openListConfig.Token,
|
||||
});
|
||||
|
||||
// 1. 读取现有 metainfo.json (如果存在)
|
||||
let existingMetaInfo: MetaInfo | null = getCachedMetaInfo(rootPath);
|
||||
|
||||
if (!existingMetaInfo) {
|
||||
try {
|
||||
const metainfoPath = `${rootPath}${rootPath.endsWith('/') ? '' : '/'}metainfo.json`;
|
||||
console.log('[OpenList Refresh] 尝试读取现有 metainfo.json:', metainfoPath);
|
||||
|
||||
const fileResponse = await client.getFile(metainfoPath);
|
||||
console.log('[OpenList Refresh] getFile 完整响应:', JSON.stringify(fileResponse, null, 2));
|
||||
|
||||
if (fileResponse.code === 200 && fileResponse.data.raw_url) {
|
||||
const downloadUrl = fileResponse.data.raw_url;
|
||||
console.log('[OpenList Refresh] 下载 URL:', downloadUrl);
|
||||
|
||||
const contentResponse = await fetch(downloadUrl, {
|
||||
headers: {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
||||
'Accept': '*/*',
|
||||
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
|
||||
},
|
||||
});
|
||||
|
||||
console.log('[OpenList Refresh] fetch 响应:', {
|
||||
status: contentResponse.status,
|
||||
ok: contentResponse.ok,
|
||||
});
|
||||
|
||||
if (!contentResponse.ok) {
|
||||
throw new Error(`下载失败: ${contentResponse.status}`);
|
||||
}
|
||||
|
||||
const content = await contentResponse.text();
|
||||
console.log('[OpenList Refresh] 文件内容:', {
|
||||
length: content.length,
|
||||
preview: content.substring(0, 300),
|
||||
});
|
||||
|
||||
existingMetaInfo = JSON.parse(content);
|
||||
console.log('[OpenList Refresh] 读取到现有数据:', {
|
||||
hasfolders: !!existingMetaInfo?.folders,
|
||||
foldersType: typeof existingMetaInfo?.folders,
|
||||
videoCount: Object.keys(existingMetaInfo?.folders || {}).length,
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[OpenList Refresh] 读取 metainfo.json 失败:', error);
|
||||
console.log('[OpenList Refresh] 将创建新文件');
|
||||
}
|
||||
} else {
|
||||
console.log('[OpenList Refresh] 使用缓存的 metainfo,视频数:', Object.keys(existingMetaInfo.folders).length);
|
||||
}
|
||||
|
||||
const metaInfo: MetaInfo = existingMetaInfo || {
|
||||
folders: {},
|
||||
last_refresh: Date.now(),
|
||||
};
|
||||
|
||||
// 确保 folders 对象存在
|
||||
if (!metaInfo.folders || typeof metaInfo.folders !== 'object') {
|
||||
console.warn('[OpenList Refresh] metaInfo.folders 无效,重新初始化');
|
||||
metaInfo.folders = {};
|
||||
}
|
||||
|
||||
console.log('[OpenList Refresh] metaInfo 初始化完成:', {
|
||||
hasfolders: !!metaInfo.folders,
|
||||
foldersType: typeof metaInfo.folders,
|
||||
videoCount: Object.keys(metaInfo.folders).length,
|
||||
});
|
||||
|
||||
// 2. 列出根目录下的所有文件夹
|
||||
const listResponse = await client.listDirectory(rootPath);
|
||||
|
||||
if (listResponse.code !== 200) {
|
||||
return NextResponse.json(
|
||||
{ error: 'OpenList 列表获取失败' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
|
||||
const folders = listResponse.data.content.filter((item) => item.is_dir);
|
||||
|
||||
console.log('[OpenList Refresh] 找到文件夹:', {
|
||||
total: folders.length,
|
||||
names: folders.map(f => f.name),
|
||||
});
|
||||
|
||||
// 3. 遍历文件夹,搜索 TMDB
|
||||
let newCount = 0;
|
||||
let errorCount = 0;
|
||||
|
||||
for (const folder of folders) {
|
||||
console.log('[OpenList Refresh] 处理文件夹:', folder.name);
|
||||
|
||||
// 跳过已搜索过的文件夹
|
||||
if (metaInfo.folders[folder.name]) {
|
||||
console.log('[OpenList Refresh] 跳过已存在的文件夹:', folder.name);
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
console.log('[OpenList Refresh] 搜索 TMDB:', folder.name);
|
||||
// 搜索 TMDB
|
||||
const searchResult = await searchTMDB(
|
||||
tmdbApiKey,
|
||||
folder.name,
|
||||
tmdbProxy
|
||||
);
|
||||
|
||||
console.log('[OpenList Refresh] TMDB 搜索结果:', {
|
||||
folder: folder.name,
|
||||
code: searchResult.code,
|
||||
hasResult: !!searchResult.result,
|
||||
});
|
||||
|
||||
if (searchResult.code === 200 && searchResult.result) {
|
||||
const result = searchResult.result;
|
||||
|
||||
metaInfo.folders[folder.name] = {
|
||||
tmdb_id: result.id,
|
||||
title: result.title || result.name || folder.name,
|
||||
poster_path: result.poster_path,
|
||||
release_date: result.release_date || result.first_air_date || '',
|
||||
overview: result.overview,
|
||||
vote_average: result.vote_average,
|
||||
media_type: result.media_type,
|
||||
last_updated: Date.now(),
|
||||
};
|
||||
|
||||
console.log('[OpenList Refresh] 添加成功:', {
|
||||
folder: folder.name,
|
||||
title: metaInfo.folders[folder.name].title,
|
||||
});
|
||||
|
||||
newCount++;
|
||||
} else {
|
||||
console.warn(`[OpenList Refresh] TMDB 搜索失败: ${folder.name}`);
|
||||
errorCount++;
|
||||
}
|
||||
|
||||
// 避免请求过快
|
||||
await new Promise((resolve) => setTimeout(resolve, 300));
|
||||
} catch (error) {
|
||||
console.error(`[OpenList Refresh] 处理文件夹失败: ${folder.name}`, error);
|
||||
errorCount++;
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 更新 metainfo.json
|
||||
metaInfo.last_refresh = Date.now();
|
||||
|
||||
const metainfoPath = `${rootPath}${rootPath.endsWith('/') ? '' : '/'}metainfo.json`;
|
||||
const metainfoContent = JSON.stringify(metaInfo, null, 2);
|
||||
console.log('[OpenList Refresh] 上传 metainfo.json:', {
|
||||
path: metainfoPath,
|
||||
videoCount: Object.keys(metaInfo.folders).length,
|
||||
contentLength: metainfoContent.length,
|
||||
contentPreview: metainfoContent.substring(0, 300),
|
||||
});
|
||||
|
||||
await client.uploadFile(metainfoPath, metainfoContent);
|
||||
console.log('[OpenList Refresh] 上传成功');
|
||||
|
||||
// 验证上传:立即读取文件
|
||||
try {
|
||||
console.log('[OpenList Refresh] 验证上传:读取文件');
|
||||
const verifyResponse = await client.getFile(metainfoPath);
|
||||
if (verifyResponse.code === 200 && verifyResponse.data.raw_url) {
|
||||
const downloadUrl = verifyResponse.data.raw_url;
|
||||
const verifyContentResponse = await fetch(downloadUrl, {
|
||||
headers: {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
||||
'Accept': '*/*',
|
||||
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
|
||||
},
|
||||
});
|
||||
const verifyContent = await verifyContentResponse.text();
|
||||
console.log('[OpenList Refresh] 验证读取成功:', {
|
||||
contentLength: verifyContent.length,
|
||||
contentPreview: verifyContent.substring(0, 300),
|
||||
});
|
||||
|
||||
// 尝试解析
|
||||
const verifyParsed = JSON.parse(verifyContent);
|
||||
console.log('[OpenList Refresh] 验证解析成功:', {
|
||||
hasfolders: !!verifyParsed.folders,
|
||||
foldersType: typeof verifyParsed.folders,
|
||||
videoCount: Object.keys(verifyParsed.folders || {}).length,
|
||||
});
|
||||
}
|
||||
} catch (verifyError) {
|
||||
console.error('[OpenList Refresh] 验证失败:', verifyError);
|
||||
}
|
||||
|
||||
// 5. 更新缓存
|
||||
invalidateMetaInfoCache(rootPath);
|
||||
setCachedMetaInfo(rootPath, metaInfo);
|
||||
console.log('[OpenList Refresh] 缓存已更新');
|
||||
|
||||
// 6. 更新配置
|
||||
config.OpenListConfig!.LastRefreshTime = Date.now();
|
||||
config.OpenListConfig!.ResourceCount = Object.keys(metaInfo.folders).length;
|
||||
await db.saveAdminConfig(config);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
total: folders.length,
|
||||
new: newCount,
|
||||
existing: Object.keys(metaInfo.folders).length - newCount,
|
||||
errors: errorCount,
|
||||
last_refresh: metaInfo.last_refresh,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('刷新私人影库失败:', error);
|
||||
return NextResponse.json(
|
||||
{ error: '刷新失败', details: (error as Error).message },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -33,6 +33,9 @@ export async function GET(request: NextRequest) {
|
||||
const config = await getConfig();
|
||||
const apiSites = await getAvailableApiSites(authInfo.username);
|
||||
|
||||
// 检查是否配置了 OpenList
|
||||
const hasOpenList = !!(config.OpenListConfig?.URL && config.OpenListConfig?.Token);
|
||||
|
||||
// 共享状态
|
||||
let streamClosed = false;
|
||||
|
||||
@@ -62,7 +65,7 @@ export async function GET(request: NextRequest) {
|
||||
const startEvent = `data: ${JSON.stringify({
|
||||
type: 'start',
|
||||
query,
|
||||
totalSources: apiSites.length,
|
||||
totalSources: apiSites.length + (hasOpenList ? 1 : 0),
|
||||
timestamp: Date.now()
|
||||
})}\n\n`;
|
||||
|
||||
@@ -74,6 +77,123 @@ export async function GET(request: NextRequest) {
|
||||
let completedSources = 0;
|
||||
const allResults: any[] = [];
|
||||
|
||||
// 搜索 OpenList(如果配置了)
|
||||
if (hasOpenList) {
|
||||
try {
|
||||
const { getCachedMetaInfo } = await import('@/lib/openlist-cache');
|
||||
const { getTMDBImageUrl } = await import('@/lib/tmdb.search');
|
||||
const { OpenListClient } = await import('@/lib/openlist.client');
|
||||
|
||||
const rootPath = config.OpenListConfig!.RootPath || '/';
|
||||
let metaInfo = getCachedMetaInfo(rootPath);
|
||||
|
||||
// 如果没有缓存,尝试从 OpenList 读取
|
||||
if (!metaInfo) {
|
||||
try {
|
||||
const client = new OpenListClient(
|
||||
config.OpenListConfig!.URL,
|
||||
config.OpenListConfig!.Token
|
||||
);
|
||||
const metainfoPath = `${rootPath}${rootPath.endsWith('/') ? '' : '/'}metainfo.json`;
|
||||
const fileResponse = await client.getFile(metainfoPath);
|
||||
|
||||
if (fileResponse.code === 200 && fileResponse.data.raw_url) {
|
||||
const downloadUrl = fileResponse.data.raw_url;
|
||||
const contentResponse = await fetch(downloadUrl, {
|
||||
headers: {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
||||
'Accept': '*/*',
|
||||
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
|
||||
},
|
||||
});
|
||||
const content = await contentResponse.text();
|
||||
metaInfo = JSON.parse(content);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[Search WS] 读取 metainfo.json 失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
if (metaInfo && metaInfo.folders) {
|
||||
const openlistResults = Object.entries(metaInfo.folders)
|
||||
.filter(([folderName, info]: [string, any]) => {
|
||||
const matchFolder = folderName.toLowerCase().includes(query.toLowerCase());
|
||||
const matchTitle = info.title.toLowerCase().includes(query.toLowerCase());
|
||||
return matchFolder || matchTitle;
|
||||
})
|
||||
.map(([folderName, info]: [string, any]) => ({
|
||||
id: folderName,
|
||||
source: 'openlist',
|
||||
source_name: '私人影库',
|
||||
title: info.title,
|
||||
poster: getTMDBImageUrl(info.poster_path),
|
||||
episodes: [],
|
||||
episodes_titles: [],
|
||||
year: info.release_date.split('-')[0] || '',
|
||||
desc: info.overview,
|
||||
type_name: info.media_type === 'movie' ? '电影' : '电视剧',
|
||||
douban_id: 0,
|
||||
}));
|
||||
|
||||
completedSources++;
|
||||
|
||||
if (!streamClosed) {
|
||||
const sourceEvent = `data: ${JSON.stringify({
|
||||
type: 'source_result',
|
||||
source: 'openlist',
|
||||
sourceName: '私人影库',
|
||||
results: openlistResults,
|
||||
timestamp: Date.now()
|
||||
})}\n\n`;
|
||||
|
||||
if (!safeEnqueue(encoder.encode(sourceEvent))) {
|
||||
streamClosed = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (openlistResults.length > 0) {
|
||||
allResults.push(...openlistResults);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
completedSources++;
|
||||
|
||||
if (!streamClosed) {
|
||||
const sourceEvent = `data: ${JSON.stringify({
|
||||
type: 'source_result',
|
||||
source: 'openlist',
|
||||
sourceName: '私人影库',
|
||||
results: [],
|
||||
timestamp: Date.now()
|
||||
})}\n\n`;
|
||||
|
||||
if (!safeEnqueue(encoder.encode(sourceEvent))) {
|
||||
streamClosed = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[Search WS] 搜索 OpenList 失败:', error);
|
||||
completedSources++;
|
||||
|
||||
if (!streamClosed) {
|
||||
const errorEvent = `data: ${JSON.stringify({
|
||||
type: 'source_error',
|
||||
source: 'openlist',
|
||||
sourceName: '私人影库',
|
||||
error: error instanceof Error ? error.message : '搜索失败',
|
||||
timestamp: Date.now()
|
||||
})}\n\n`;
|
||||
|
||||
if (!safeEnqueue(encoder.encode(errorEvent))) {
|
||||
streamClosed = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 为每个源创建搜索 Promise
|
||||
const searchPromises = apiSites.map(async (site) => {
|
||||
try {
|
||||
@@ -141,7 +261,7 @@ export async function GET(request: NextRequest) {
|
||||
}
|
||||
|
||||
// 检查是否所有源都已完成
|
||||
if (completedSources === apiSites.length) {
|
||||
if (completedSources === apiSites.length + (hasOpenList ? 1 : 0)) {
|
||||
if (!streamClosed) {
|
||||
// 发送最终完成事件
|
||||
const completeEvent = `data: ${JSON.stringify({
|
||||
|
||||
Reference in New Issue
Block a user