私人影库增加立即扫描和重新扫描,增加删除按钮

This commit is contained in:
mtvpls
2025-12-23 20:42:09 +08:00
parent 57969bf4ad
commit e116b98c96
4 changed files with 238 additions and 28 deletions

View File

@@ -120,6 +120,7 @@ interface AlertModalProps {
message?: string; message?: string;
timer?: number; timer?: number;
showConfirm?: boolean; showConfirm?: boolean;
onConfirm?: () => void;
} }
const AlertModal = ({ const AlertModal = ({
@@ -130,6 +131,7 @@ const AlertModal = ({
message, message,
timer, timer,
showConfirm = false, showConfirm = false,
onConfirm,
}: AlertModalProps) => { }: AlertModalProps) => {
const [isVisible, setIsVisible] = useState(false); const [isVisible, setIsVisible] = useState(false);
@@ -196,14 +198,38 @@ const AlertModal = ({
<p className='text-gray-600 dark:text-gray-400 mb-4'>{message}</p> <p className='text-gray-600 dark:text-gray-400 mb-4'>{message}</p>
)} )}
{showConfirm && ( {showConfirm ? (
<button onConfirm ? (
onClick={onClose} // 确认操作:显示取消和确定按钮
className={`px-4 py-2 text-sm font-medium ${buttonStyles.primary}`} <div className='flex gap-3 justify-center'>
> <button
onClick={() => {
</button> onClose();
)} }}
className={buttonStyles.secondary}
>
</button>
<button
onClick={() => {
if (onConfirm) onConfirm();
onClose();
}}
className={buttonStyles.danger}
>
</button>
</div>
) : (
// 普通提示:只显示确定按钮
<button
onClick={onClose}
className={buttonStyles.primary}
>
</button>
)
) : null}
</div> </div>
</div> </div>
</div>, </div>,
@@ -220,6 +246,7 @@ const useAlertModal = () => {
message?: string; message?: string;
timer?: number; timer?: number;
showConfirm?: boolean; showConfirm?: boolean;
onConfirm?: () => void;
}>({ }>({
isOpen: false, isOpen: false,
type: 'success', type: 'success',
@@ -2623,12 +2650,14 @@ const OpenListConfigComponent = ({
}); });
}; };
const handleRefresh = async () => { const handleRefresh = async (clearMetaInfo = false) => {
setRefreshing(true); setRefreshing(true);
setScanProgress(null); setScanProgress(null);
try { try {
const response = await fetch('/api/openlist/refresh', { const response = await fetch('/api/openlist/refresh', {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ clearMetaInfo }),
}); });
if (!response.ok) { if (!response.ok) {
@@ -2718,6 +2747,36 @@ const OpenListConfigComponent = ({
fetchVideos(true); // 强制从数据库重新读取,不使用缓存 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) => { const formatDate = (timestamp?: number) => {
if (!timestamp) return '未刷新'; if (!timestamp) return '未刷新';
return new Date(timestamp).toLocaleString('zh-CN'); return new Date(timestamp).toLocaleString('zh-CN');
@@ -2883,13 +2942,22 @@ const OpenListConfigComponent = ({
</span> </span>
</div> </div>
</div> </div>
<button <div className='flex gap-3'>
onClick={handleRefresh} <button
disabled={refreshing} onClick={() => handleRefresh(true)}
className={buttonStyles.primary} disabled={refreshing}
> className={buttonStyles.warning}
{refreshing ? '扫描中...' : '立即扫描'} >
</button> {refreshing ? '扫描中...' : '重新扫描'}
</button>
<button
onClick={() => handleRefresh(false)}
disabled={refreshing}
className={buttonStyles.primary}
>
{refreshing ? '扫描中...' : '立即扫描'}
</button>
</div>
</div> </div>
{refreshing && scanProgress && ( {refreshing && scanProgress && (
@@ -2995,6 +3063,12 @@ const OpenListConfigComponent = ({
> >
{video.failed ? '立即纠错' : '纠错'} {video.failed ? '立即纠错' : '纠错'}
</button> </button>
<button
onClick={() => handleDeleteVideo(video.folder, video.title)}
className={buttonStyles.dangerSmall}
>
</button>
</div> </div>
</td> </td>
</tr> </tr>
@@ -3018,6 +3092,7 @@ const OpenListConfigComponent = ({
message={alertModal.message} message={alertModal.message}
timer={alertModal.timer} timer={alertModal.timer}
showConfirm={alertModal.showConfirm} showConfirm={alertModal.showConfirm}
onConfirm={alertModal.onConfirm}
/> />
{/* 纠错对话框 */} {/* 纠错对话框 */}

View File

@@ -330,12 +330,13 @@ async function refreshOpenList() {
console.log(`开始 OpenList 定时扫描(间隔: ${scanInterval} 分钟)`); console.log(`开始 OpenList 定时扫描(间隔: ${scanInterval} 分钟)`);
// 调用扫描接口 // 调用扫描接口(立即扫描模式,不清空 metainfo
const response = await fetch(`${process.env.SITE_BASE || 'http://localhost:3000'}/api/openlist/refresh`, { const response = await fetch(`${process.env.SITE_BASE || 'http://localhost:3000'}/api/openlist/refresh`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
body: JSON.stringify({ clearMetaInfo: false }),
}); });
if (!response.ok) { if (!response.ok) {

View File

@@ -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 }
);
}
}

View File

@@ -35,6 +35,10 @@ export async function POST(request: NextRequest) {
return NextResponse.json({ error: '未授权' }, { status: 401 }); return NextResponse.json({ error: '未授权' }, { status: 401 });
} }
// 获取请求参数
const body = await request.json().catch(() => ({}));
const clearMetaInfo = body.clearMetaInfo === true; // 是否清空 metainfo
// 获取配置 // 获取配置
const config = await getConfig(); const config = await getConfig();
const openListConfig = config.OpenListConfig; const openListConfig = config.OpenListConfig;
@@ -70,7 +74,8 @@ export async function POST(request: NextRequest) {
tmdbApiKey, tmdbApiKey,
tmdbProxy, tmdbProxy,
openListConfig.Username, openListConfig.Username,
openListConfig.Password openListConfig.Password,
clearMetaInfo
).catch((error) => { ).catch((error) => {
console.error('[OpenList Refresh] 后台扫描失败:', error); console.error('[OpenList Refresh] 后台扫描失败:', error);
failScanTask(taskId, (error as Error).message); failScanTask(taskId, (error as Error).message);
@@ -100,22 +105,47 @@ async function performScan(
tmdbApiKey: string, tmdbApiKey: string,
tmdbProxy?: string, tmdbProxy?: string,
username?: string, username?: string,
password?: string password?: string,
clearMetaInfo?: boolean
): Promise<void> { ): Promise<void> {
const client = new OpenListClient(url, username!, password!); const client = new OpenListClient(url, username!, password!);
// 立即清除缓存,确保后续读取的是新数据
invalidateMetaInfoCache(rootPath);
// 立即更新进度,确保任务可被查询 // 立即更新进度,确保任务可被查询
updateScanTaskProgress(taskId, 0, 0); updateScanTaskProgress(taskId, 0, 0);
try { try {
// 1. 不读取现有数据,直接创建新的 metainfo // 1. 根据参数决定是否读取现有数据
const metaInfo: MetaInfo = { let metaInfo: MetaInfo;
folders: {},
last_refresh: Date.now(), 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 缓存) // 2. 列出根目录下的所有文件夹(强制刷新 OpenList 缓存)
// 循环获取所有页的数据 // 循环获取所有页的数据
@@ -148,6 +178,7 @@ async function performScan(
// 3. 遍历文件夹,搜索 TMDB // 3. 遍历文件夹,搜索 TMDB
let newCount = 0; let newCount = 0;
let existingCount = 0;
let errorCount = 0; let errorCount = 0;
for (let i = 0; i < folders.length; i++) { for (let i = 0; i < folders.length; i++) {
@@ -156,6 +187,12 @@ async function performScan(
// 更新进度 // 更新进度
updateScanTaskProgress(taskId, i + 1, folders.length, folder.name); updateScanTaskProgress(taskId, i + 1, folders.length, folder.name);
// 如果是立即扫描(不清空 metainfo且文件夹已存在跳过
if (!clearMetaInfo && metaInfo.folders[folder.name]) {
existingCount++;
continue;
}
try { try {
// 搜索 TMDB // 搜索 TMDB
const searchResult = await searchTMDB( const searchResult = await searchTMDB(
@@ -236,7 +273,7 @@ async function performScan(
completeScanTask(taskId, { completeScanTask(taskId, {
total: folders.length, total: folders.length,
new: newCount, new: newCount,
existing: 0, existing: existingCount,
errors: errorCount, errors: errorCount,
}); });
} catch (error) { } catch (error) {