openlist离线下载增加额外源选择
This commit is contained in:
@@ -3,7 +3,11 @@ import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
import { getAuthInfoFromCookie } from '@/lib/auth';
|
||||
import { getConfig } from '@/lib/config';
|
||||
import { OpenListClient } from '@/lib/openlist.client';
|
||||
import {
|
||||
addOpenListOfflineDownload,
|
||||
getOfflineDownloadBasePath,
|
||||
joinOpenListPath,
|
||||
} from '@/lib/openlist-offline-download';
|
||||
import { hasFeaturePermission } from '@/lib/permissions';
|
||||
|
||||
export const runtime = 'nodejs';
|
||||
@@ -55,56 +59,13 @@ export async function POST(req: NextRequest) {
|
||||
|
||||
// 获取 OpenList 配置
|
||||
const config = await getConfig();
|
||||
const openlistConfig = config.OpenListConfig;
|
||||
|
||||
if (!openlistConfig?.Enabled) {
|
||||
return NextResponse.json(
|
||||
{ error: '私人影库功能未启用' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
if (!openlistConfig.URL || !openlistConfig.Username || !openlistConfig.Password) {
|
||||
return NextResponse.json(
|
||||
{ error: 'OpenList 配置不完整' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// 构建下载路径(使用离线下载目录)
|
||||
const offlineDownloadPath = openlistConfig.OfflineDownloadPath || '/';
|
||||
const downloadPath = `${offlineDownloadPath.replace(/\/$/, '')}/${name}`;
|
||||
|
||||
// 使用 OpenListClient 添加离线下载任务
|
||||
const client = new OpenListClient(
|
||||
openlistConfig.URL,
|
||||
openlistConfig.Username,
|
||||
openlistConfig.Password
|
||||
const downloadPath = joinOpenListPath(
|
||||
getOfflineDownloadBasePath(config),
|
||||
name
|
||||
);
|
||||
|
||||
// 获取 Token 并调用 API
|
||||
const token = await (client as any).getToken();
|
||||
const openlistUrl = `${openlistConfig.URL.replace(/\/$/, '')}/api/fs/add_offline_download`;
|
||||
|
||||
const response = await fetch(openlistUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': token,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
path: downloadPath,
|
||||
urls: [url],
|
||||
tool,
|
||||
}),
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
// 检查响应状态
|
||||
if (!response.ok || data.code !== 200) {
|
||||
throw new Error(data.message || '添加离线下载任务失败');
|
||||
}
|
||||
await addOpenListOfflineDownload(config, downloadPath, url, tool);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
|
||||
@@ -45,7 +45,22 @@ export async function POST(request: NextRequest) {
|
||||
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { action, Enabled, URL, Username, Password, RootPaths, OfflineDownloadPath, ScanInterval, ScanMode, DisableVideoPreview } = body;
|
||||
const {
|
||||
action,
|
||||
Enabled,
|
||||
URL,
|
||||
Username,
|
||||
Password,
|
||||
RootPaths,
|
||||
OfflineDownloadPath,
|
||||
OfflineDownloadUseCustomSource,
|
||||
OfflineDownloadURL,
|
||||
OfflineDownloadUsername,
|
||||
OfflineDownloadPassword,
|
||||
ScanInterval,
|
||||
ScanMode,
|
||||
DisableVideoPreview,
|
||||
} = body;
|
||||
|
||||
const authInfo = getAuthInfoFromCookie(request);
|
||||
if (!authInfo || !authInfo.username) {
|
||||
@@ -74,6 +89,10 @@ export async function POST(request: NextRequest) {
|
||||
Password: Password || '',
|
||||
RootPaths: RootPaths || ['/'],
|
||||
OfflineDownloadPath: OfflineDownloadPath || '/',
|
||||
OfflineDownloadUseCustomSource: OfflineDownloadUseCustomSource || false,
|
||||
OfflineDownloadURL: OfflineDownloadURL || '',
|
||||
OfflineDownloadUsername: OfflineDownloadUsername || '',
|
||||
OfflineDownloadPassword: OfflineDownloadPassword || '',
|
||||
LastRefreshTime: adminConfig.OpenListConfig?.LastRefreshTime,
|
||||
ResourceCount: adminConfig.OpenListConfig?.ResourceCount,
|
||||
ScanInterval: 0,
|
||||
@@ -97,6 +116,16 @@ export async function POST(request: NextRequest) {
|
||||
);
|
||||
}
|
||||
|
||||
if (
|
||||
OfflineDownloadUseCustomSource &&
|
||||
(!OfflineDownloadURL || !OfflineDownloadUsername || !OfflineDownloadPassword)
|
||||
) {
|
||||
return NextResponse.json(
|
||||
{ error: '请提供离线下载 OpenList URL、账号和密码' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// 验证 RootPaths
|
||||
if (!Array.isArray(RootPaths) || RootPaths.length === 0) {
|
||||
return NextResponse.json(
|
||||
@@ -130,6 +159,24 @@ export async function POST(request: NextRequest) {
|
||||
);
|
||||
}
|
||||
|
||||
if (OfflineDownloadUseCustomSource) {
|
||||
try {
|
||||
console.log('[OpenList Config] 验证离线下载 OpenList 账号密码');
|
||||
await OpenListClient.login(
|
||||
OfflineDownloadURL,
|
||||
OfflineDownloadUsername,
|
||||
OfflineDownloadPassword
|
||||
);
|
||||
console.log('[OpenList Config] 离线下载 OpenList 账号密码验证成功');
|
||||
} catch (error) {
|
||||
console.error('[OpenList Config] 离线下载 OpenList 账号密码验证失败:', error);
|
||||
return NextResponse.json(
|
||||
{ error: '离线下载 OpenList 账号密码验证失败: ' + (error as Error).message },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
adminConfig.OpenListConfig = {
|
||||
Enabled: true,
|
||||
URL,
|
||||
@@ -137,6 +184,10 @@ export async function POST(request: NextRequest) {
|
||||
Password,
|
||||
RootPaths: cleanedRootPaths,
|
||||
OfflineDownloadPath: OfflineDownloadPath || '/',
|
||||
OfflineDownloadUseCustomSource: OfflineDownloadUseCustomSource || false,
|
||||
OfflineDownloadURL: OfflineDownloadURL || '',
|
||||
OfflineDownloadUsername: OfflineDownloadUsername || '',
|
||||
OfflineDownloadPassword: OfflineDownloadPassword || '',
|
||||
LastRefreshTime: adminConfig.OpenListConfig?.LastRefreshTime,
|
||||
ResourceCount: adminConfig.OpenListConfig?.ResourceCount,
|
||||
ScanInterval: scanInterval,
|
||||
|
||||
Reference in New Issue
Block a user