feat: add source validate check

This commit is contained in:
shinya
2025-08-21 21:01:33 +08:00
parent a66d83a6a1
commit 44b5915100
2 changed files with 435 additions and 6 deletions

View File

@@ -1686,6 +1686,18 @@ const VideoSourceConfig = ({
from: 'config',
});
// 有效性检测相关状态
const [showValidationModal, setShowValidationModal] = useState(false);
const [searchKeyword, setSearchKeyword] = useState('');
const [isValidating, setIsValidating] = useState(false);
const [validationResults, setValidationResults] = useState<Array<{
key: string;
name: string;
status: 'valid' | 'no_results' | 'invalid' | 'validating';
message: string;
resultCount: number;
}>>([]);
// dnd-kit 传感器
const sensors = useSensors(
useSensor(PointerSensor, {
@@ -1792,6 +1804,139 @@ const VideoSourceConfig = ({
});
};
// 有效性检测函数
const handleValidateSources = async () => {
if (!searchKeyword.trim()) {
showAlert({ type: 'warning', title: '请输入搜索关键词', message: '搜索关键词不能为空' });
return;
}
setIsValidating(true);
setValidationResults([]); // 清空之前的结果
setShowValidationModal(false); // 立即关闭弹窗
// 初始化所有视频源为检测中状态
const initialResults = sources.map(source => ({
key: source.key,
name: source.name,
status: 'validating' as const,
message: '检测中...',
resultCount: 0
}));
setValidationResults(initialResults);
try {
// 使用EventSource接收流式数据
const eventSource = new EventSource(`/api/admin/source/validate?q=${encodeURIComponent(searchKeyword.trim())}`);
eventSource.onmessage = (event) => {
try {
const data = JSON.parse(event.data);
switch (data.type) {
case 'start':
console.log(`开始检测 ${data.totalSources} 个视频源`);
break;
case 'source_result':
case 'source_error':
// 更新验证结果
setValidationResults(prev => {
const existing = prev.find(r => r.key === data.source);
if (existing) {
return prev.map(r => r.key === data.source ? {
key: data.source,
name: sources.find(s => s.key === data.source)?.name || data.source,
status: data.status,
message: data.status === 'valid' ? '搜索正常' :
data.status === 'no_results' ? '无法搜索到结果' : '连接失败',
resultCount: data.status === 'valid' ? 1 : 0
} : r);
} else {
return [...prev, {
key: data.source,
name: sources.find(s => s.key === data.source)?.name || data.source,
status: data.status,
message: data.status === 'valid' ? '搜索正常' :
data.status === 'no_results' ? '无法搜索到结果' : '连接失败',
resultCount: data.status === 'valid' ? 1 : 0
}];
}
});
break;
case 'complete':
console.log(`检测完成,共检测 ${data.completedSources} 个视频源`);
eventSource.close();
setIsValidating(false);
break;
}
} catch (error) {
console.error('解析EventSource数据失败:', error);
}
};
eventSource.onerror = (error) => {
console.error('EventSource错误:', error);
eventSource.close();
setIsValidating(false);
showAlert({ type: 'error', title: '验证失败', message: '连接错误,请重试' });
};
// 设置超时,防止长时间等待
setTimeout(() => {
if (eventSource.readyState === EventSource.OPEN) {
eventSource.close();
setIsValidating(false);
showAlert({ type: 'warning', title: '验证超时', message: '检测超时,请重试' });
}
}, 60000); // 60秒超时
} catch (error) {
setIsValidating(false);
showAlert({ type: 'error', title: '验证失败', message: error instanceof Error ? error.message : '未知错误' });
}
};
// 获取有效性状态显示
const getValidationStatus = (sourceKey: string) => {
const result = validationResults.find(r => r.key === sourceKey);
if (!result) return null;
switch (result.status) {
case 'validating':
return {
text: '检测中',
className: 'bg-blue-100 dark:bg-blue-900/20 text-blue-800 dark:text-blue-300',
icon: '⟳',
message: result.message
};
case 'valid':
return {
text: '有效',
className: 'bg-green-100 dark:bg-green-900/20 text-green-800 dark:text-green-300',
icon: '✓',
message: result.message
};
case 'no_results':
return {
text: '无法搜索',
className: 'bg-yellow-100 dark:bg-yellow-900/20 text-yellow-800 dark:text-yellow-300',
icon: '⚠',
message: result.message
};
case 'invalid':
return {
text: '无效',
className: 'bg-red-100 dark:bg-red-900/20 text-red-800 dark:text-red-300',
icon: '✗',
message: result.message
};
default:
return null;
}
};
// 可拖拽行封装 (dnd-kit)
const DraggableRow = ({ source }: { source: DataSource }) => {
const { attributes, listeners, setNodeRef, transform, transition } =
@@ -1844,6 +1989,23 @@ const VideoSourceConfig = ({
{!source.disabled ? '启用中' : '已禁用'}
</span>
</td>
<td className='px-6 py-4 whitespace-nowrap max-w-[1rem]'>
{(() => {
const status = getValidationStatus(source.key);
if (!status) {
return (
<span className='px-2 py-1 text-xs rounded-full bg-gray-100 dark:bg-gray-900/20 text-gray-600 dark:text-gray-400'>
</span>
);
}
return (
<span className={`px-2 py-1 text-xs rounded-full ${status.className}`} title={status.message}>
{status.icon} {status.text}
</span>
);
})()}
</td>
<td className='px-6 py-4 whitespace-nowrap text-right text-sm font-medium space-x-2'>
<button
onClick={() => handleToggleEnable(source.key)}
@@ -1882,12 +2044,31 @@ const VideoSourceConfig = ({
<h4 className='text-sm font-medium text-gray-700 dark:text-gray-300'>
</h4>
<button
onClick={() => setShowAddForm(!showAddForm)}
className='px-3 py-1 bg-green-600 hover:bg-green-700 text-white text-sm rounded-lg transition-colors'
>
{showAddForm ? '取消' : '添加视频源'}
</button>
<div className='flex items-center space-x-2'>
<button
onClick={() => setShowValidationModal(true)}
disabled={isValidating}
className={`px-3 py-1 text-sm rounded-lg transition-colors flex items-center space-x-1 ${isValidating
? 'bg-gray-400 cursor-not-allowed text-gray-200'
: 'bg-blue-600 hover:bg-blue-700 text-white'
}`}
>
{isValidating ? (
<>
<div className='w-3 h-3 border border-white border-t-transparent rounded-full animate-spin'></div>
<span>...</span>
</>
) : (
'有效性检测'
)}
</button>
<button
onClick={() => setShowAddForm(!showAddForm)}
className='px-3 py-1 bg-green-600 hover:bg-green-700 text-white text-sm rounded-lg transition-colors'
>
{showAddForm ? '取消' : '添加视频源'}
</button>
</div>
</div>
{showAddForm && (
@@ -1963,6 +2144,9 @@ const VideoSourceConfig = ({
<th className='px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider'>
</th>
<th className='px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider'>
</th>
<th className='px-6 py-3 text-right text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider'>
</th>
@@ -2001,6 +2185,46 @@ const VideoSourceConfig = ({
</div>
)}
{/* 有效性检测弹窗 */}
{showValidationModal && createPortal(
<div className='fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50'>
<div className='bg-white dark:bg-gray-800 rounded-lg p-6 w-full max-w-md mx-4'>
<h3 className='text-lg font-medium text-gray-900 dark:text-gray-100 mb-4'>
</h3>
<p className='text-sm text-gray-600 dark:text-gray-400 mb-4'>
</p>
<div className='space-y-4'>
<input
type='text'
placeholder='请输入搜索关键词'
value={searchKeyword}
onChange={(e) => setSearchKeyword(e.target.value)}
className='w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100'
onKeyPress={(e) => e.key === 'Enter' && handleValidateSources()}
/>
<div className='flex justify-end space-x-3'>
<button
onClick={() => setShowValidationModal(false)}
className='px-4 py-2 text-gray-600 dark:text-gray-400 hover:text-gray-800 dark:hover:text-gray-200 transition-colors'
>
</button>
<button
onClick={handleValidateSources}
disabled={isValidating || !searchKeyword.trim()}
className='px-4 py-2 bg-blue-600 hover:bg-blue-700 disabled:bg-gray-400 text-white rounded-lg transition-colors'
>
{isValidating ? `检测中... (${validationResults.length}/${sources.length})` : '开始检测'}
</button>
</div>
</div>
</div>
</div>,
document.body
)}
{/* 通用弹窗组件 */}
<AlertModal
isOpen={alertModal.isOpen}

View File

@@ -0,0 +1,205 @@
import { NextRequest, NextResponse } from 'next/server';
import { getAuthInfoFromCookie } from '@/lib/auth';
import { getConfig } from '@/lib/config';
import { API_CONFIG } from '@/lib/config';
export const runtime = 'nodejs';
export async function GET(request: NextRequest) {
const authInfo = getAuthInfoFromCookie(request);
if (!authInfo || !authInfo.username) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const { searchParams } = new URL(request.url);
const searchKeyword = searchParams.get('q');
if (!searchKeyword) {
return new Response(
JSON.stringify({ error: '搜索关键词不能为空' }),
{
status: 400,
headers: {
'Content-Type': 'application/json',
},
}
);
}
const config = await getConfig();
const apiSites = config.SourceConfig;
// 共享状态
let streamClosed = false;
// 创建可读流
const stream = new ReadableStream({
async start(controller) {
const encoder = new TextEncoder();
// 辅助函数:安全地向控制器写入数据
const safeEnqueue = (data: Uint8Array) => {
try {
if (streamClosed || (!controller.desiredSize && controller.desiredSize !== 0)) {
return false;
}
controller.enqueue(data);
return true;
} catch (error) {
console.warn('Failed to enqueue data:', error);
streamClosed = true;
return false;
}
};
// 发送开始事件
const startEvent = `data: ${JSON.stringify({
type: 'start',
totalSources: apiSites.length
})}\n\n`;
if (!safeEnqueue(encoder.encode(startEvent))) {
return;
}
// 记录已完成的源数量
let completedSources = 0;
// 为每个源创建验证 Promise
const validationPromises = apiSites.map(async (site) => {
try {
// 构建搜索URL只获取第一页
const searchUrl = `${site.api}?ac=videolist&wd=${encodeURIComponent(searchKeyword)}`;
// 设置超时控制
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 10000);
try {
const response = await fetch(searchUrl, {
headers: API_CONFIG.search.headers,
signal: controller.signal,
});
clearTimeout(timeoutId);
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
const data = await response.json() as any;
// 检查结果是否有效
let status: 'valid' | 'no_results' | 'invalid';
let message: string;
let resultCount: number;
if (
data &&
data.list &&
Array.isArray(data.list) &&
data.list.length > 0
) {
// 检查是否有标题包含搜索词的结果
const validResults = data.list.filter((item: any) => {
const title = item.vod_name || '';
return title.toLowerCase().includes(searchKeyword.toLowerCase());
});
if (validResults.length > 0) {
status = 'valid';
message = `搜索正常,找到 ${validResults.length} 个相关结果`;
resultCount = validResults.length;
} else {
status = 'no_results';
message = '搜索结果中无相关标题';
resultCount = 0;
}
} else {
status = 'no_results';
message = '无法搜索到结果';
resultCount = 0;
}
// 发送该源的验证结果
completedSources++;
if (!streamClosed) {
const sourceEvent = `data: ${JSON.stringify({
type: 'source_result',
source: site.key,
status
})}\n\n`;
if (!safeEnqueue(encoder.encode(sourceEvent))) {
streamClosed = true;
return;
}
}
} finally {
clearTimeout(timeoutId);
}
} catch (error) {
console.warn(`验证失败 ${site.name}:`, error);
// 发送源错误事件
completedSources++;
if (!streamClosed) {
const errorEvent = `data: ${JSON.stringify({
type: 'source_error',
source: site.key,
status: 'invalid'
})}\n\n`;
if (!safeEnqueue(encoder.encode(errorEvent))) {
streamClosed = true;
return;
}
}
}
// 检查是否所有源都已完成
if (completedSources === apiSites.length) {
if (!streamClosed) {
// 发送最终完成事件
const completeEvent = `data: ${JSON.stringify({
type: 'complete',
completedSources
})}\n\n`;
if (safeEnqueue(encoder.encode(completeEvent))) {
try {
controller.close();
} catch (error) {
console.warn('Failed to close controller:', error);
}
}
}
}
});
// 等待所有验证完成
await Promise.allSettled(validationPromises);
},
cancel() {
streamClosed = true;
console.log('Client disconnected, cancelling validation stream');
},
});
// 返回流式响应
return new Response(stream, {
headers: {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET',
'Access-Control-Allow-Headers': 'Content-Type',
},
});
}