夸克网盘在线播放移除openlist依赖
This commit is contained in:
@@ -2,21 +2,13 @@ import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
import { getAuthInfoFromCookie } from '@/lib/auth';
|
||||
import { getConfig } from '@/lib/config';
|
||||
import { createQuarkInstantPlayFolder } from '@/lib/netdisk/quark.client';
|
||||
import { listQuarkShareVideos } from '@/lib/netdisk/quark.client';
|
||||
import { createQuarkNetdiskSession } from '@/lib/netdisk/quark-session-cache';
|
||||
import { NETDISK_QUARK_SOURCE } from '@/lib/netdisk/source';
|
||||
import { hasFeaturePermission } from '@/lib/permissions';
|
||||
import { base58Encode } from '@/lib/utils';
|
||||
|
||||
export const runtime = 'nodejs';
|
||||
|
||||
function joinPath(...parts: string[]) {
|
||||
const joined = parts
|
||||
.filter(Boolean)
|
||||
.join('/')
|
||||
.replace(/\/+/g, '/');
|
||||
return joined.startsWith('/') ? joined : `/${joined}`;
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const authInfo = getAuthInfoFromCookie(request);
|
||||
@@ -34,54 +26,26 @@ export async function POST(request: NextRequest) {
|
||||
|
||||
const config = await getConfig();
|
||||
const quarkConfig = config.NetDiskConfig?.Quark;
|
||||
|
||||
if (!quarkConfig?.Enabled || !quarkConfig.Cookie) {
|
||||
return NextResponse.json({ error: '夸克网盘未配置或未启用' }, { status: 400 });
|
||||
}
|
||||
|
||||
const result = await createQuarkInstantPlayFolder(quarkConfig.Cookie, {
|
||||
const result = await listQuarkShareVideos(shareUrl, quarkConfig.Cookie, passcode || '');
|
||||
const session = createQuarkNetdiskSession({
|
||||
title: title || result.title,
|
||||
shareUrl,
|
||||
passcode,
|
||||
playTempSavePath: quarkConfig.PlayTempSavePath,
|
||||
title,
|
||||
shareId: result.shareId,
|
||||
shareToken: result.shareToken,
|
||||
files: result.files,
|
||||
});
|
||||
|
||||
if (!result.folderName) {
|
||||
throw new Error('未生成临时播放目录');
|
||||
}
|
||||
|
||||
const openlistFolderPath = joinPath(
|
||||
quarkConfig.OpenListTempPath,
|
||||
result.folderName
|
||||
);
|
||||
|
||||
if (
|
||||
config.OpenListConfig?.Enabled &&
|
||||
config.OpenListConfig.URL &&
|
||||
config.OpenListConfig.Username &&
|
||||
config.OpenListConfig.Password
|
||||
) {
|
||||
try {
|
||||
const { OpenListClient } = await import('@/lib/openlist.client');
|
||||
const openListClient = new OpenListClient(
|
||||
config.OpenListConfig.URL,
|
||||
config.OpenListConfig.Username,
|
||||
config.OpenListConfig.Password
|
||||
);
|
||||
await openListClient.refreshDirectory(quarkConfig.OpenListTempPath || '/');
|
||||
await openListClient.refreshDirectory(openlistFolderPath);
|
||||
} catch (refreshError) {
|
||||
console.warn('[quark instant-play] 刷新 OpenList 临时目录失败:', refreshError);
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
source: NETDISK_QUARK_SOURCE,
|
||||
id: base58Encode(openlistFolderPath),
|
||||
title: title || result.folderName,
|
||||
openlistFolderPath,
|
||||
...result,
|
||||
id: session.id,
|
||||
title: title || result.title,
|
||||
fileCount: result.files.length,
|
||||
});
|
||||
} catch (error) {
|
||||
return NextResponse.json(
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
import { getAuthInfoFromCookie } from '@/lib/auth';
|
||||
import { getQuarkNetdiskSession, refreshQuarkNetdiskSession } from '@/lib/netdisk/quark-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/quark/proxy?id=${encodeURIComponent(id)}&episodeIndex=${episodeIndex}`;
|
||||
refreshQuarkNetdiskSession(id) || getQuarkNetdiskSession(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,95 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
import { getAuthInfoFromCookie } from '@/lib/auth';
|
||||
import { ensureQuarkPlayFolder, getQuarkPlayHeaders, getQuarkPlayUrls, saveQuarkShareFile } from '@/lib/netdisk/quark.client';
|
||||
import { refreshQuarkNetdiskSession } from '@/lib/netdisk/quark-session-cache';
|
||||
import { resolveQuarkSession } from '@/lib/netdisk/quark-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, savePath } = await resolveQuarkSession(id);
|
||||
const file = session.files[episodeIndex];
|
||||
if (!file) {
|
||||
return NextResponse.json({ error: '播放文件不存在' }, { status: 404 });
|
||||
}
|
||||
|
||||
if (!session.playFolderFid || !session.playFolderPath) {
|
||||
const folder = await ensureQuarkPlayFolder(cookie, savePath, session.shareId, session.title);
|
||||
session.playFolderFid = folder.folderFid;
|
||||
session.playFolderPath = folder.folderPath;
|
||||
}
|
||||
|
||||
let savedFileId = session.savedFileIds[file.fid];
|
||||
if (!savedFileId) {
|
||||
savedFileId = await saveQuarkShareFile(cookie, {
|
||||
shareId: session.shareId,
|
||||
shareToken: session.shareToken,
|
||||
fileId: file.fid,
|
||||
shareFileToken: file.shareFidToken,
|
||||
playFolderFid: session.playFolderFid,
|
||||
});
|
||||
session.savedFileIds[file.fid] = savedFileId;
|
||||
}
|
||||
refreshQuarkNetdiskSession(id);
|
||||
|
||||
const playUrls = await getQuarkPlayUrls(cookie, savedFileId);
|
||||
const selected = playUrls.find((item) => item.name === quality) || playUrls[0];
|
||||
if (!selected) {
|
||||
return NextResponse.json({ error: '未获取到夸克播放地址' }, { status: 500 });
|
||||
}
|
||||
|
||||
const range = request.headers.get('range');
|
||||
const upstream = await fetch(selected.url, {
|
||||
headers: {
|
||||
...getQuarkPlayHeaders(cookie),
|
||||
...(range ? { Range: range } : {}),
|
||||
},
|
||||
cache: 'no-store',
|
||||
});
|
||||
|
||||
if (!upstream.ok || !upstream.body) {
|
||||
return NextResponse.json(
|
||||
{ error: `夸克视频代理失败 (${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');
|
||||
|
||||
return new Response(upstream.body, {
|
||||
status: range && upstream.headers.get('content-range') ? 206 : 200,
|
||||
headers: responseHeaders,
|
||||
});
|
||||
} catch (error) {
|
||||
return NextResponse.json(
|
||||
{ error: error instanceof Error ? error.message : '夸克网盘代理失败' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user