openlist配置增加连通性检查

This commit is contained in:
mtvpls
2025-12-23 20:48:15 +08:00
parent e116b98c96
commit 1a0ec53452
3 changed files with 131 additions and 0 deletions

View File

@@ -2747,6 +2747,33 @@ const OpenListConfigComponent = ({
fetchVideos(true); // 强制从数据库重新读取,不使用缓存
};
const handleCheckConnectivity = async () => {
await withLoading('checkOpenList', async () => {
try {
const response = await fetch('/api/openlist/check', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
url,
username,
password,
}),
});
const data = await response.json();
if (response.ok && data.success) {
showSuccess('连接成功', showAlert);
} else {
throw new Error(data.error || '连接失败');
}
} catch (error) {
showError(error instanceof Error ? error.message : '连接失败', showAlert);
throw error;
}
});
};
const handleDeleteVideo = async (folder: string, title: string) => {
// 显示确认对话框,直接在 onConfirm 中执行删除操作
showAlert({
@@ -2916,6 +2943,13 @@ const OpenListConfigComponent = ({
</div>
<div className='flex gap-3'>
<button
onClick={handleCheckConnectivity}
disabled={!enabled || !url || !username || !password || isLoading('checkOpenList')}
className={buttonStyles.primary}
>
{isLoading('checkOpenList') ? '检查中...' : '检查连通性'}
</button>
<button
onClick={handleSave}
disabled={isLoading('saveOpenList')}

View File

@@ -0,0 +1,61 @@
/* eslint-disable @typescript-eslint/no-explicit-any, no-console */
import { NextRequest, NextResponse } from 'next/server';
import { getAuthInfoFromCookie } from '@/lib/auth';
import { OpenListClient } from '@/lib/openlist.client';
export const runtime = 'nodejs';
/**
* POST /api/openlist/check
* 检查 OpenList 连通性
*/
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 { url, username, password } = body;
if (!url || !username || !password) {
return NextResponse.json(
{ error: '缺少必要参数' },
{ status: 400 }
);
}
// 创建客户端并检查连通性
const client = new OpenListClient(url, username, password);
const result = await client.checkConnectivity();
if (result.success) {
return NextResponse.json({
success: true,
message: result.message,
});
} else {
return NextResponse.json(
{
success: false,
error: result.message,
},
{ status: 400 }
);
}
} catch (error) {
console.error('检查 OpenList 连通性失败:', error);
return NextResponse.json(
{
success: false,
error: error instanceof Error ? error.message : '检查失败',
},
{ status: 500 }
);
}
}

View File

@@ -279,4 +279,40 @@ export class OpenListClient {
throw new Error(`OpenList 删除失败: ${response.status}`);
}
}
// 检查连通性
async checkConnectivity(): Promise<{ success: boolean; message: string }> {
try {
const response = await this.fetchWithRetry(`${this.baseURL}/api/me`, {
method: 'GET',
headers: await this.getHeaders(),
});
if (response.status !== 200) {
return {
success: false,
message: `HTTP 状态码错误: ${response.status}`,
};
}
const data = await response.json();
if (data.code !== 200) {
return {
success: false,
message: `响应码错误: ${data.code}`,
};
}
return {
success: true,
message: '连接成功',
};
} catch (error) {
return {
success: false,
message: error instanceof Error ? error.message : '连接失败',
};
}
}
}