mirror of
https://github.com/MatrixSeven/file-transfer-go.git
synced 2026-05-20 12:27:30 +08:00
feat:文件传输优化
This commit is contained in:
43
chuan-next/src/app/api/create-text-room/route.ts
Normal file
43
chuan-next/src/app/api/create-text-room/route.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
try {
|
||||
const { text } = await req.json();
|
||||
|
||||
if (!text || text.trim().length === 0) {
|
||||
return NextResponse.json({ error: '文本内容不能为空' }, { status: 400 });
|
||||
}
|
||||
|
||||
if (text.length > 50000) {
|
||||
return NextResponse.json({ error: '文本内容过长,最大支持50,000字符' }, { status: 400 });
|
||||
}
|
||||
|
||||
// 调用后端API创建文字传输房间
|
||||
const response = await fetch('http://localhost:8080/api/create-text-room', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ text }),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('创建文字传输房间失败');
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
code: data.code,
|
||||
message: '文字传输房间创建成功'
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('创建文字传输房间错误:', error);
|
||||
return NextResponse.json(
|
||||
{ error: '服务器错误,请重试' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
41
chuan-next/src/app/api/get-text-content/route.ts
Normal file
41
chuan-next/src/app/api/get-text-content/route.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
try {
|
||||
const { code } = await req.json();
|
||||
|
||||
if (!code || code.length !== 6) {
|
||||
return NextResponse.json({ error: '请输入正确的6位房间码' }, { status: 400 });
|
||||
}
|
||||
|
||||
// 调用后端API获取文字内容
|
||||
const response = await fetch(`http://localhost:8080/api/get-text-content/${code}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
if (response.status === 404) {
|
||||
return NextResponse.json({ error: '房间不存在或已过期' }, { status: 404 });
|
||||
}
|
||||
throw new Error('获取文字内容失败');
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
text: data.text,
|
||||
message: '文字内容获取成功'
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('获取文字内容错误:', error);
|
||||
return NextResponse.json(
|
||||
{ error: '服务器错误,请重试' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user