uc网盘在线播放
This commit is contained in:
@@ -21,6 +21,11 @@ import {
|
||||
validateQuarkCookieReadable,
|
||||
} from '@/lib/netdisk/quark.client';
|
||||
import { normalizeTianyiAccount, normalizeTianyiPassword, validateTianyiCredentials } from '@/lib/netdisk/tianyi.client';
|
||||
import {
|
||||
assertUCCookieHeaderSafe,
|
||||
normalizeUCCookie,
|
||||
validateUCCookieReadable,
|
||||
} from '@/lib/netdisk/uc.client';
|
||||
|
||||
export const runtime = 'nodejs';
|
||||
|
||||
@@ -51,7 +56,7 @@ export async function POST(request: NextRequest) {
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const { action, Quark, Mobile, Baidu, Tianyi, Pan123, provider } = body;
|
||||
const { action, Quark, Mobile, Baidu, Tianyi, Pan123, UC, provider } = body;
|
||||
const adminConfig = await getConfig();
|
||||
|
||||
if (action === 'save') {
|
||||
@@ -64,6 +69,8 @@ export async function POST(request: NextRequest) {
|
||||
const normalizedTianyiPassword = Tianyi?.Password ? normalizeTianyiPassword(Tianyi.Password) : '';
|
||||
const normalizedPan123Account = Pan123?.Account ? normalizePan123Account(Pan123.Account) : '';
|
||||
const normalizedPan123Password = Pan123?.Password ? normalizePan123Password(Pan123.Password) : '';
|
||||
const normalizedUCCookie = UC?.Cookie ? assertUCCookieHeaderSafe(UC.Cookie) : '';
|
||||
const normalizedUCToken = UC?.Token ? String(UC.Token).trim() : '';
|
||||
|
||||
adminConfig.NetDiskConfig = adminConfig.NetDiskConfig || {};
|
||||
adminConfig.NetDiskConfig.Quark = {
|
||||
@@ -89,6 +96,12 @@ export async function POST(request: NextRequest) {
|
||||
Account: normalizedPan123Account,
|
||||
Password: normalizedPan123Password,
|
||||
};
|
||||
adminConfig.NetDiskConfig.UC = {
|
||||
Enabled: Boolean(UC?.Enabled),
|
||||
Cookie: normalizedUCCookie,
|
||||
Token: normalizedUCToken,
|
||||
SavePath: UC?.SavePath || '/',
|
||||
};
|
||||
|
||||
await db.saveAdminConfig(adminConfig);
|
||||
await setCachedConfig(adminConfig);
|
||||
@@ -144,6 +157,16 @@ export async function POST(request: NextRequest) {
|
||||
message: '123网盘账号密码可用',
|
||||
});
|
||||
}
|
||||
if (provider === 'uc') {
|
||||
if (!UC?.Cookie) {
|
||||
return NextResponse.json({ error: '请先填写UC Cookie' }, { status: 400 });
|
||||
}
|
||||
await validateUCCookieReadable(normalizeUCCookie(UC.Cookie));
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: 'UC Cookie 可读',
|
||||
});
|
||||
}
|
||||
|
||||
if (!Quark?.Cookie) {
|
||||
return NextResponse.json({ error: '请先填写夸克 Cookie' }, { status: 400 });
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
import { getAuthInfoFromCookie } from '@/lib/auth';
|
||||
import { getConfig } from '@/lib/config';
|
||||
import { NETDISK_UC_SOURCE } from '@/lib/netdisk/source';
|
||||
import { listUCShareVideos } from '@/lib/netdisk/uc.client';
|
||||
import { createUCNetdiskSession } from '@/lib/netdisk/uc-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 ucConfig = config.NetDiskConfig?.UC;
|
||||
if (!ucConfig?.Enabled || !ucConfig.Cookie) {
|
||||
return NextResponse.json({ error: 'UC网盘未配置或未启用' }, { status: 400 });
|
||||
}
|
||||
|
||||
const result = await listUCShareVideos(shareUrl, ucConfig.Cookie, passcode || '');
|
||||
const session = createUCNetdiskSession({
|
||||
title: title || result.title,
|
||||
shareUrl,
|
||||
passcode,
|
||||
shareId: result.shareId,
|
||||
shareToken: result.shareToken,
|
||||
files: result.files,
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
source: NETDISK_UC_SOURCE,
|
||||
id: session.id,
|
||||
title: title || result.title,
|
||||
fileCount: result.files.length,
|
||||
});
|
||||
} catch (error) {
|
||||
return NextResponse.json(
|
||||
{ error: error instanceof Error ? error.message : '立即播放失败' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
import { getAuthInfoFromCookie } from '@/lib/auth';
|
||||
import { getUCNetdiskSession, refreshUCNetdiskSession } from '@/lib/netdisk/uc-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 id = searchParams.get('id');
|
||||
const episodeIndexRaw = searchParams.get('episodeIndex');
|
||||
const format = searchParams.get('format');
|
||||
if (!id || 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 proxyUrl = `/api/netdisk/uc/proxy?id=${encodeURIComponent(id)}&episodeIndex=${episodeIndex}`;
|
||||
refreshUCNetdiskSession(id) || getUCNetdiskSession(id);
|
||||
|
||||
if (format === 'json') {
|
||||
return NextResponse.json({ url: proxyUrl, headers: {} });
|
||||
}
|
||||
|
||||
return NextResponse.redirect(new URL(proxyUrl, request.url));
|
||||
} catch (error) {
|
||||
return NextResponse.json(
|
||||
{ error: error instanceof Error ? error.message : '获取播放地址失败' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
import { getAuthInfoFromCookie } from '@/lib/auth';
|
||||
import { ensureUCPlayFolder, getUCPlayUrls, saveUCShareFile } from '@/lib/netdisk/uc.client';
|
||||
import { refreshUCNetdiskSession } from '@/lib/netdisk/uc-session-cache';
|
||||
import { resolveUCSession } from '@/lib/netdisk/uc-session-resolver';
|
||||
|
||||
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 id = searchParams.get('id');
|
||||
const episodeIndexRaw = searchParams.get('episodeIndex');
|
||||
const quality = searchParams.get('quality') || '';
|
||||
if (!id || 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 { session, cookie, token, savePath } = await resolveUCSession(id);
|
||||
const file = session.files[episodeIndex];
|
||||
if (!file) {
|
||||
return NextResponse.json({ error: '播放文件不存在' }, { status: 404 });
|
||||
}
|
||||
|
||||
if (!session.playFolderFid || !session.playFolderPath) {
|
||||
const folder = await ensureUCPlayFolder(cookie, savePath, session.shareId, session.title);
|
||||
session.playFolderFid = folder.folderFid;
|
||||
session.playFolderPath = folder.folderPath;
|
||||
}
|
||||
|
||||
let savedFileId = session.savedFileIds[file.fid];
|
||||
if (!savedFileId) {
|
||||
savedFileId = await saveUCShareFile(cookie, {
|
||||
shareId: session.shareId,
|
||||
shareToken: session.shareToken,
|
||||
fileId: file.fid,
|
||||
shareFileToken: file.shareFidToken,
|
||||
playFolderFid: session.playFolderFid,
|
||||
});
|
||||
session.savedFileIds[file.fid] = savedFileId;
|
||||
}
|
||||
refreshUCNetdiskSession(id);
|
||||
|
||||
const playUrls = await getUCPlayUrls(cookie, savedFileId, token);
|
||||
const selected = playUrls.find((item) => item.name === quality) || playUrls[0];
|
||||
if (!selected) {
|
||||
return NextResponse.json({ error: '未获取到 UC 播放地址' }, { status: 500 });
|
||||
}
|
||||
|
||||
const range = request.headers.get('range');
|
||||
const abortController = new AbortController();
|
||||
const timeoutId = setTimeout(() => abortController.abort(), 300000);
|
||||
|
||||
try {
|
||||
const upstream = await fetch(selected.url, {
|
||||
headers: {
|
||||
...(selected.headers || {}),
|
||||
...(range ? { Range: range } : {}),
|
||||
},
|
||||
cache: 'no-store',
|
||||
signal: abortController.signal,
|
||||
});
|
||||
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
if (!upstream.ok || !upstream.body) {
|
||||
return NextResponse.json(
|
||||
{ error: `UC视频代理失败 (${upstream.status})` },
|
||||
{ status: upstream.status || 500 }
|
||||
);
|
||||
}
|
||||
|
||||
const responseHeaders = new Headers();
|
||||
const copyHeaders = ['content-type', 'content-length', 'content-range', 'accept-ranges', 'etag', 'last-modified'];
|
||||
copyHeaders.forEach((name) => {
|
||||
const value = upstream.headers.get(name);
|
||||
if (value) responseHeaders.set(name, value);
|
||||
});
|
||||
responseHeaders.set('Cache-Control', 'private, no-store');
|
||||
|
||||
const { readable, writable } = new TransformStream();
|
||||
const reader = upstream.body.getReader();
|
||||
|
||||
void (async () => {
|
||||
const writer = writable.getWriter();
|
||||
try {
|
||||
let streamDone = false;
|
||||
while (!streamDone) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) {
|
||||
streamDone = true;
|
||||
} else {
|
||||
await writer.write(value);
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
try {
|
||||
await reader.cancel();
|
||||
} catch {
|
||||
void 0;
|
||||
}
|
||||
} finally {
|
||||
try {
|
||||
reader.releaseLock();
|
||||
} catch {
|
||||
void 0;
|
||||
}
|
||||
try {
|
||||
await writer.close();
|
||||
} catch {
|
||||
void 0;
|
||||
}
|
||||
}
|
||||
})();
|
||||
|
||||
return new Response(readable, {
|
||||
status: range && upstream.headers.get('content-range') ? 206 : upstream.status,
|
||||
headers: responseHeaders,
|
||||
});
|
||||
} catch (error) {
|
||||
clearTimeout(timeoutId);
|
||||
if (error instanceof Error && error.name === 'AbortError') {
|
||||
return NextResponse.json({ error: 'UC网盘代理超时' }, { status: 504 });
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
} catch (error) {
|
||||
return NextResponse.json(
|
||||
{ error: error instanceof Error ? error.message : 'UC网盘代理失败' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -18,12 +18,28 @@ import {
|
||||
parseMobileNetdiskId,
|
||||
refreshMobileNetdiskSession,
|
||||
} from '@/lib/netdisk/mobile-session-cache';
|
||||
import {
|
||||
createPan123NetdiskSession,
|
||||
getPan123NetdiskSession,
|
||||
parsePan123NetdiskId,
|
||||
refreshPan123NetdiskSession,
|
||||
} from '@/lib/netdisk/pan123-session-cache';
|
||||
import {
|
||||
createQuarkNetdiskSession,
|
||||
getQuarkNetdiskSession,
|
||||
parseQuarkNetdiskId,
|
||||
refreshQuarkNetdiskSession,
|
||||
} from '@/lib/netdisk/quark-session-cache';
|
||||
import {
|
||||
LEGACY_QUARK_TEMP_SOURCE,
|
||||
NETDISK_123_SOURCE,
|
||||
NETDISK_BAIDU_SOURCE,
|
||||
NETDISK_MOBILE_SOURCE,
|
||||
NETDISK_QUARK_SOURCE,
|
||||
NETDISK_TIANYI_SOURCE,
|
||||
NETDISK_UC_SOURCE,
|
||||
normalizeNetdiskSource,
|
||||
} from '@/lib/netdisk/source';
|
||||
import {
|
||||
createTianyiNetdiskSession,
|
||||
getTianyiNetdiskSession,
|
||||
@@ -31,12 +47,11 @@ import {
|
||||
refreshTianyiNetdiskSession,
|
||||
} from '@/lib/netdisk/tianyi-session-cache';
|
||||
import {
|
||||
createPan123NetdiskSession,
|
||||
getPan123NetdiskSession,
|
||||
parsePan123NetdiskId,
|
||||
refreshPan123NetdiskSession,
|
||||
} from '@/lib/netdisk/pan123-session-cache';
|
||||
import { LEGACY_QUARK_TEMP_SOURCE, NETDISK_123_SOURCE, NETDISK_BAIDU_SOURCE, NETDISK_MOBILE_SOURCE, NETDISK_QUARK_SOURCE, NETDISK_TIANYI_SOURCE, normalizeNetdiskSource } from '@/lib/netdisk/source';
|
||||
createUCNetdiskSession,
|
||||
getUCNetdiskSession,
|
||||
parseUCNetdiskId,
|
||||
refreshUCNetdiskSession,
|
||||
} from '@/lib/netdisk/uc-session-cache';
|
||||
import {
|
||||
executeSavedSourceScript,
|
||||
normalizeScriptDetailResult,
|
||||
@@ -685,6 +700,76 @@ export async function GET(request: NextRequest) {
|
||||
}
|
||||
}
|
||||
|
||||
if (sourceCode === NETDISK_UC_SOURCE) {
|
||||
try {
|
||||
const config = await getConfig();
|
||||
const ucConfig = config.NetDiskConfig?.UC;
|
||||
if (!ucConfig?.Enabled || !ucConfig.Cookie) {
|
||||
throw new Error('UC网盘未配置或未启用');
|
||||
}
|
||||
const { parseVideoFileName } = await import('@/lib/video-parser');
|
||||
|
||||
let session = refreshUCNetdiskSession(id) || getUCNetdiskSession(id);
|
||||
if (!session) {
|
||||
const payload = parseUCNetdiskId(id);
|
||||
const { listUCShareVideos } = await import('@/lib/netdisk/uc.client');
|
||||
const result = await listUCShareVideos(payload.shareUrl, ucConfig.Cookie, payload.passcode || '');
|
||||
session = createUCNetdiskSession({
|
||||
title: title || result.title,
|
||||
shareUrl: payload.shareUrl,
|
||||
passcode: payload.passcode,
|
||||
shareId: result.shareId,
|
||||
shareToken: result.shareToken,
|
||||
files: result.files,
|
||||
});
|
||||
}
|
||||
if (!session) {
|
||||
throw new Error('UC网盘播放信息恢复失败');
|
||||
}
|
||||
|
||||
const ucSession = session;
|
||||
const episodes = ucSession.files
|
||||
.map((file, index) => {
|
||||
const parsed = parseVideoFileName(file.name);
|
||||
return {
|
||||
originalIndex: index,
|
||||
fileName: file.name,
|
||||
episode: parsed.episode || index + 1,
|
||||
title: formatNetdiskEpisodeTitle(parsed, file.name),
|
||||
isOVA: parsed.isOVA,
|
||||
};
|
||||
})
|
||||
.sort((a, b) => {
|
||||
if (a.isOVA && !b.isOVA) return 1;
|
||||
if (!a.isOVA && b.isOVA) return -1;
|
||||
return a.episode !== b.episode
|
||||
? a.episode - b.episode
|
||||
: a.fileName.localeCompare(b.fileName);
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
source: NETDISK_UC_SOURCE,
|
||||
source_name: 'UC网盘',
|
||||
id: ucSession.id,
|
||||
title: title || ucSession.title,
|
||||
poster: '',
|
||||
year: '',
|
||||
douban_id: 0,
|
||||
desc: `UC网盘分享:${ucSession.shareUrl}`,
|
||||
episodes: episodes.map((ep) => (
|
||||
`/api/netdisk/uc/play?id=${encodeURIComponent(ucSession.id)}&episodeIndex=${ep.originalIndex}`
|
||||
)),
|
||||
episodes_titles: episodes.map((ep) => ep.title),
|
||||
proxyMode: false,
|
||||
});
|
||||
} catch (error) {
|
||||
return NextResponse.json(
|
||||
{ error: (error as Error).message },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// 特殊处理 openlist 源 - 直接调用 /api/detail
|
||||
if (sourceCode === 'openlist') {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user