增加天翼网盘在线播放
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
import { getAuthInfoFromCookie } from '@/lib/auth';
|
||||
import { getConfig } from '@/lib/config';
|
||||
import { NETDISK_TIANYI_SOURCE } from '@/lib/netdisk/source';
|
||||
import { listTianyiShareVideos } from '@/lib/netdisk/tianyi.client';
|
||||
import { createTianyiNetdiskSession } from '@/lib/netdisk/tianyi-session-cache';
|
||||
import { hasFeaturePermission } from '@/lib/permissions';
|
||||
|
||||
export const runtime = 'nodejs';
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const authInfo = getAuthInfoFromCookie(request);
|
||||
if (!authInfo?.username) {
|
||||
return NextResponse.json({ error: '未登录' }, { status: 401 });
|
||||
}
|
||||
if (!(await hasFeaturePermission(authInfo.username, 'netdisk_temp_play'))) {
|
||||
return NextResponse.json({ error: '无权限使用临时播放' }, { status: 403 });
|
||||
}
|
||||
|
||||
const { shareUrl, passcode, title } = await request.json();
|
||||
if (!shareUrl) {
|
||||
return NextResponse.json({ error: '分享链接不能为空' }, { status: 400 });
|
||||
}
|
||||
|
||||
const config = await getConfig();
|
||||
const tianyiConfig = config.NetDiskConfig?.Tianyi;
|
||||
if (!tianyiConfig?.Enabled || !tianyiConfig.Account || !tianyiConfig.Password) {
|
||||
return NextResponse.json({ error: '天翼云盘未配置或未启用' }, { status: 400 });
|
||||
}
|
||||
|
||||
const result = await listTianyiShareVideos(
|
||||
shareUrl,
|
||||
tianyiConfig.Account,
|
||||
tianyiConfig.Password,
|
||||
passcode || ''
|
||||
);
|
||||
const session = createTianyiNetdiskSession({
|
||||
title: title || result.title,
|
||||
shareUrl,
|
||||
passcode,
|
||||
shareId: result.shareId,
|
||||
shareMode: result.shareMode,
|
||||
isFolder: result.isFolder,
|
||||
accessCode: result.accessCode,
|
||||
files: result.files,
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
source: NETDISK_TIANYI_SOURCE,
|
||||
id: session.id,
|
||||
title: title || result.title,
|
||||
totalFiles: result.files.length,
|
||||
expiresAt: session.expiresAt,
|
||||
});
|
||||
} catch (error) {
|
||||
return NextResponse.json(
|
||||
{ error: error instanceof Error ? error.message : '立即播放失败' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
import { getAuthInfoFromCookie } from '@/lib/auth';
|
||||
import { getConfig } from '@/lib/config';
|
||||
import { getTianyiSharePlayUrl } from '@/lib/netdisk/tianyi.client';
|
||||
import { listTianyiShareVideos } from '@/lib/netdisk/tianyi.client';
|
||||
import { createTianyiNetdiskSession, getTianyiNetdiskSession, parseTianyiNetdiskId, refreshTianyiNetdiskSession } from '@/lib/netdisk/tianyi-session-cache';
|
||||
|
||||
export const runtime = 'nodejs';
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const authInfo = getAuthInfoFromCookie(request);
|
||||
if (!authInfo?.username) {
|
||||
return NextResponse.json({ error: '未授权' }, { status: 401 });
|
||||
}
|
||||
|
||||
const { searchParams } = new URL(request.url);
|
||||
const sessionId = searchParams.get('id') || searchParams.get('session');
|
||||
const episodeIndexRaw = searchParams.get('episodeIndex');
|
||||
const format = searchParams.get('format');
|
||||
if (!sessionId || episodeIndexRaw == null) {
|
||||
return NextResponse.json({ error: '缺少参数' }, { status: 400 });
|
||||
}
|
||||
|
||||
const episodeIndex = Number.parseInt(episodeIndexRaw, 10);
|
||||
if (!Number.isInteger(episodeIndex) || episodeIndex < 0) {
|
||||
return NextResponse.json({ error: '无效的 episodeIndex' }, { status: 400 });
|
||||
}
|
||||
|
||||
const config = await getConfig();
|
||||
const tianyiConfig = config.NetDiskConfig?.Tianyi;
|
||||
if (!tianyiConfig?.Enabled || !tianyiConfig.Account || !tianyiConfig.Password) {
|
||||
return NextResponse.json({ error: '天翼云盘未配置或未启用' }, { status: 400 });
|
||||
}
|
||||
|
||||
let session = refreshTianyiNetdiskSession(sessionId) || getTianyiNetdiskSession(sessionId);
|
||||
if (!session) {
|
||||
const payload = parseTianyiNetdiskId(sessionId);
|
||||
const result = await listTianyiShareVideos(
|
||||
payload.shareUrl,
|
||||
tianyiConfig.Account,
|
||||
tianyiConfig.Password,
|
||||
payload.passcode || ''
|
||||
);
|
||||
session = createTianyiNetdiskSession({
|
||||
title: result.title,
|
||||
shareUrl: payload.shareUrl,
|
||||
passcode: payload.passcode,
|
||||
shareId: result.shareId,
|
||||
shareMode: result.shareMode,
|
||||
isFolder: result.isFolder,
|
||||
accessCode: result.accessCode,
|
||||
files: result.files,
|
||||
});
|
||||
}
|
||||
|
||||
const file = session.files[episodeIndex];
|
||||
if (!file) {
|
||||
return NextResponse.json({ error: '播放文件不存在' }, { status: 404 });
|
||||
}
|
||||
|
||||
const url = await getTianyiSharePlayUrl(
|
||||
file.fileId,
|
||||
file.shareId,
|
||||
tianyiConfig.Account,
|
||||
tianyiConfig.Password
|
||||
);
|
||||
refreshTianyiNetdiskSession(sessionId);
|
||||
|
||||
if (format === 'json') {
|
||||
return NextResponse.json({ url, headers: {} });
|
||||
}
|
||||
|
||||
return NextResponse.redirect(url);
|
||||
} catch (error) {
|
||||
return NextResponse.json(
|
||||
{ error: error instanceof Error ? error.message : '获取播放地址失败' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user