128 lines
3.2 KiB
TypeScript
128 lines
3.2 KiB
TypeScript
// 网络诊断API - 用于测试上传/下载速度
|
|
import { NextRequest, NextResponse } from 'next/server';
|
|
|
|
export const dynamic = 'force-dynamic';
|
|
export const maxDuration = 60; // 最长60秒
|
|
|
|
// 生成测试数据
|
|
function generateTestData(size = 10 * 1024 * 1024): Uint8Array {
|
|
const data = new Uint8Array(size);
|
|
for (let i = 0; i < data.length; i++) {
|
|
data[i] = i % 251; // 使用简单的模式填充
|
|
}
|
|
return data;
|
|
}
|
|
|
|
// GET - 下载测试(返回测试数据)
|
|
export async function GET(request: NextRequest) {
|
|
const { searchParams } = new URL(request.url);
|
|
const mode = searchParams.get('mode') || 'download';
|
|
|
|
// 下载速度测试
|
|
if (mode === 'download') {
|
|
const testData = generateTestData();
|
|
return new Response(testData as any, {
|
|
status: 200,
|
|
headers: {
|
|
'Content-Type': 'application/octet-stream',
|
|
'Content-Length': testData.byteLength.toString(),
|
|
'Cache-Control': 'no-store, no-cache, must-revalidate, proxy-revalidate',
|
|
'Pragma': 'no-cache',
|
|
'Expires': '0',
|
|
},
|
|
});
|
|
}
|
|
|
|
// S3存储测试
|
|
if (mode === 'storage') {
|
|
const s3Url = getS3PublicUrl();
|
|
const startTime = Date.now();
|
|
|
|
try {
|
|
const response = await fetch(s3Url, {
|
|
method: 'GET',
|
|
redirect: 'manual',
|
|
cache: 'no-store',
|
|
});
|
|
|
|
const duration = Date.now() - startTime;
|
|
|
|
return NextResponse.json({
|
|
ok: response.ok,
|
|
status: response.status,
|
|
durationMs: duration,
|
|
url: s3Url,
|
|
contentLength: response.headers.get('content-length'),
|
|
location: response.headers.get('location'),
|
|
});
|
|
} catch (error: any) {
|
|
return NextResponse.json(
|
|
{
|
|
ok: false,
|
|
error: error?.message || String(error),
|
|
url: s3Url,
|
|
},
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
// Ping测试
|
|
if (mode === 'ping') {
|
|
return NextResponse.json({
|
|
ok: true,
|
|
message: 'pong',
|
|
timestamp: new Date().toISOString(),
|
|
});
|
|
}
|
|
|
|
// 默认返回测试数据
|
|
const testData = generateTestData();
|
|
return new Response(testData as any, {
|
|
status: 200,
|
|
headers: {
|
|
'Content-Type': 'application/octet-stream',
|
|
'Content-Length': testData.byteLength.toString(),
|
|
'Cache-Control': 'no-store, no-cache, must-revalidate, proxy-revalidate',
|
|
'Pragma': 'no-cache',
|
|
'Expires': '0',
|
|
},
|
|
});
|
|
}
|
|
|
|
// POST - 上传测试(接收测试数据)
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const data = await request.arrayBuffer();
|
|
return NextResponse.json({
|
|
ok: true,
|
|
receivedBytes: data.byteLength,
|
|
timestamp: new Date().toISOString(),
|
|
});
|
|
} catch (error: any) {
|
|
return NextResponse.json(
|
|
{
|
|
ok: false,
|
|
error: error?.message || String(error),
|
|
},
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
// 获取S3公网URL
|
|
function getS3PublicUrl(): string {
|
|
const s3Domain = process.env.S3_PUBLIC_DOMAIN || process.env.APP_PUBLIC_URL || process.env.NEXTAUTH_URL;
|
|
|
|
if (!s3Domain) {
|
|
return 'https://s3.yydd.ai';
|
|
}
|
|
|
|
// 确保URL格式正确
|
|
if (/^https?:\/\//i.test(s3Domain)) {
|
|
return s3Domain.replace(/\/$/, '');
|
|
}
|
|
|
|
return `https://${s3Domain.replace(/\/$/, '')}`;
|
|
}
|