diff --git a/src/app/admin/page.tsx b/src/app/admin/page.tsx index f447b10..6d3fa4e 100644 --- a/src/app/admin/page.tsx +++ b/src/app/admin/page.tsx @@ -120,6 +120,7 @@ interface AlertModalProps { message?: string; timer?: number; showConfirm?: boolean; + onConfirm?: () => void; } const AlertModal = ({ @@ -130,6 +131,7 @@ const AlertModal = ({ message, timer, showConfirm = false, + onConfirm, }: AlertModalProps) => { const [isVisible, setIsVisible] = useState(false); @@ -196,14 +198,38 @@ const AlertModal = ({

{message}

)} - {showConfirm && ( - - )} + {showConfirm ? ( + onConfirm ? ( + // 确认操作:显示取消和确定按钮 +
+ + +
+ ) : ( + // 普通提示:只显示确定按钮 + + ) + ) : null} , @@ -220,6 +246,7 @@ const useAlertModal = () => { message?: string; timer?: number; showConfirm?: boolean; + onConfirm?: () => void; }>({ isOpen: false, type: 'success', @@ -2623,12 +2650,14 @@ const OpenListConfigComponent = ({ }); }; - const handleRefresh = async () => { + const handleRefresh = async (clearMetaInfo = false) => { setRefreshing(true); setScanProgress(null); try { const response = await fetch('/api/openlist/refresh', { method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ clearMetaInfo }), }); if (!response.ok) { @@ -2718,6 +2747,36 @@ const OpenListConfigComponent = ({ fetchVideos(true); // 强制从数据库重新读取,不使用缓存 }; + const handleDeleteVideo = async (folder: string, title: string) => { + // 显示确认对话框,直接在 onConfirm 中执行删除操作 + showAlert({ + type: 'warning', + title: '确认删除', + message: `确定要删除视频记录"${title}"吗?此操作不会删除实际文件,只会从列表中移除。`, + showConfirm: true, + onConfirm: async () => { + try { + const response = await fetch('/api/openlist/delete', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ folder }), + }); + + if (!response.ok) { + const data = await response.json(); + throw new Error(data.error || '删除失败'); + } + + showSuccess('删除成功', showAlert); + await fetchVideos(true); // 强制从数据库重新读取 + refreshConfig(); // 异步刷新配置以更新资源数量(不等待,避免重复刷新) + } catch (error) { + showError(error instanceof Error ? error.message : '删除失败', showAlert); + } + }, + }); + }; + const formatDate = (timestamp?: number) => { if (!timestamp) return '未刷新'; return new Date(timestamp).toLocaleString('zh-CN'); @@ -2883,13 +2942,22 @@ const OpenListConfigComponent = ({ - +
+ + +
{refreshing && scanProgress && ( @@ -2995,6 +3063,12 @@ const OpenListConfigComponent = ({ > {video.failed ? '立即纠错' : '纠错'} + @@ -3018,6 +3092,7 @@ const OpenListConfigComponent = ({ message={alertModal.message} timer={alertModal.timer} showConfirm={alertModal.showConfirm} + onConfirm={alertModal.onConfirm} /> {/* 纠错对话框 */} diff --git a/src/app/api/cron/route.ts b/src/app/api/cron/route.ts index 2e4a692..9fa6a02 100644 --- a/src/app/api/cron/route.ts +++ b/src/app/api/cron/route.ts @@ -330,12 +330,13 @@ async function refreshOpenList() { console.log(`开始 OpenList 定时扫描(间隔: ${scanInterval} 分钟)`); - // 调用扫描接口 + // 调用扫描接口(立即扫描模式,不清空 metainfo) const response = await fetch(`${process.env.SITE_BASE || 'http://localhost:3000'}/api/openlist/refresh`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, + body: JSON.stringify({ clearMetaInfo: false }), }); if (!response.ok) { diff --git a/src/app/api/openlist/delete/route.ts b/src/app/api/openlist/delete/route.ts new file mode 100644 index 0000000..7a04e04 --- /dev/null +++ b/src/app/api/openlist/delete/route.ts @@ -0,0 +1,97 @@ +/* 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 { + getCachedMetaInfo, + invalidateMetaInfoCache, + MetaInfo, + setCachedMetaInfo, +} from '@/lib/openlist-cache'; + +export const runtime = 'nodejs'; + +/** + * POST /api/openlist/delete + * 删除私人影库中的视频记录 + */ +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: '缺少 folder 参数' }, { status: 400 }); + } + + // 获取配置 + const config = await getConfig(); + const openListConfig = config.OpenListConfig; + + if (!openListConfig || !openListConfig.URL) { + return NextResponse.json( + { error: 'OpenList 未配置' }, + { status: 400 } + ); + } + + const rootPath = openListConfig.RootPath || '/'; + + // 从数据库读取 metainfo + const metainfoContent = await db.getGlobalValue('video.metainfo'); + if (!metainfoContent) { + return NextResponse.json( + { error: '未找到视频元数据' }, + { status: 404 } + ); + } + + const metaInfo: MetaInfo = JSON.parse(metainfoContent); + + // 检查文件夹是否存在 + if (!metaInfo.folders[folder]) { + return NextResponse.json( + { error: '未找到该视频记录' }, + { status: 404 } + ); + } + + // 删除文件夹记录 + delete metaInfo.folders[folder]; + + // 保存到数据库 + const updatedMetainfoContent = JSON.stringify(metaInfo); + await db.setGlobalValue('video.metainfo', updatedMetainfoContent); + + // 更新缓存 + invalidateMetaInfoCache(rootPath); + setCachedMetaInfo(rootPath, metaInfo); + + // 更新配置中的资源数量 + if (config.OpenListConfig) { + config.OpenListConfig.ResourceCount = Object.keys(metaInfo.folders).length; + await db.saveAdminConfig(config); + } + + return NextResponse.json({ + success: true, + message: '删除成功', + }); + } catch (error) { + console.error('删除视频记录失败:', error); + return NextResponse.json( + { error: '删除失败', details: (error as Error).message }, + { status: 500 } + ); + } +} diff --git a/src/app/api/openlist/refresh/route.ts b/src/app/api/openlist/refresh/route.ts index a7acad3..4fa3527 100644 --- a/src/app/api/openlist/refresh/route.ts +++ b/src/app/api/openlist/refresh/route.ts @@ -35,6 +35,10 @@ export async function POST(request: NextRequest) { return NextResponse.json({ error: '未授权' }, { status: 401 }); } + // 获取请求参数 + const body = await request.json().catch(() => ({})); + const clearMetaInfo = body.clearMetaInfo === true; // 是否清空 metainfo + // 获取配置 const config = await getConfig(); const openListConfig = config.OpenListConfig; @@ -70,7 +74,8 @@ export async function POST(request: NextRequest) { tmdbApiKey, tmdbProxy, openListConfig.Username, - openListConfig.Password + openListConfig.Password, + clearMetaInfo ).catch((error) => { console.error('[OpenList Refresh] 后台扫描失败:', error); failScanTask(taskId, (error as Error).message); @@ -100,22 +105,47 @@ async function performScan( tmdbApiKey: string, tmdbProxy?: string, username?: string, - password?: string + password?: string, + clearMetaInfo?: boolean ): Promise { const client = new OpenListClient(url, username!, password!); - // 立即清除缓存,确保后续读取的是新数据 - invalidateMetaInfoCache(rootPath); - // 立即更新进度,确保任务可被查询 updateScanTaskProgress(taskId, 0, 0); try { - // 1. 不读取现有数据,直接创建新的 metainfo - const metaInfo: MetaInfo = { - folders: {}, - last_refresh: Date.now(), - }; + // 1. 根据参数决定是否读取现有数据 + let metaInfo: MetaInfo; + + if (clearMetaInfo) { + // 重新扫描:清空现有数据 + metaInfo = { + folders: {}, + last_refresh: Date.now(), + }; + } else { + // 立即扫描:保留现有数据,从数据库读取 + try { + const metainfoContent = await db.getGlobalValue('video.metainfo'); + if (metainfoContent) { + metaInfo = JSON.parse(metainfoContent); + } else { + metaInfo = { + folders: {}, + last_refresh: Date.now(), + }; + } + } catch (error) { + console.error('[OpenList Refresh] 读取现有 metainfo 失败:', error); + metaInfo = { + folders: {}, + last_refresh: Date.now(), + }; + } + } + + // 清除缓存,确保后续读取的是新数据 + invalidateMetaInfoCache(rootPath); // 2. 列出根目录下的所有文件夹(强制刷新 OpenList 缓存) // 循环获取所有页的数据 @@ -148,6 +178,7 @@ async function performScan( // 3. 遍历文件夹,搜索 TMDB let newCount = 0; + let existingCount = 0; let errorCount = 0; for (let i = 0; i < folders.length; i++) { @@ -156,6 +187,12 @@ async function performScan( // 更新进度 updateScanTaskProgress(taskId, i + 1, folders.length, folder.name); + // 如果是立即扫描(不清空 metainfo),且文件夹已存在,跳过 + if (!clearMetaInfo && metaInfo.folders[folder.name]) { + existingCount++; + continue; + } + try { // 搜索 TMDB const searchResult = await searchTMDB( @@ -236,7 +273,7 @@ async function performScan( completeScanTask(taskId, { total: folders.length, new: newCount, - existing: 0, + existing: existingCount, errors: errorCount, }); } catch (error) {