私人影库增加立即扫描和重新扫描,增加删除按钮
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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<void> {
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user