This commit is contained in:
BlueSkyXN 2024-04-26 12:31:29 +08:00
parent de475c2f22
commit 207db6f1c4
2 changed files with 94 additions and 62 deletions

View File

@ -3,6 +3,7 @@ addEventListener('fetch', event => {
})
async function handleaagtoolRequest(request) {
try {
// 确认请求方法为 POST 并且内容类型正确
if (request.method !== 'POST' || !request.headers.get('Content-Type').includes('multipart/form-data')) {
return new Response('Invalid request', { status: 400 });
@ -10,7 +11,7 @@ async function handleaagtoolRequest(request) {
// 解析表单数据
const formData = await request.formData();
const imageFile = formData.get('file'); // 假设字段名为 'file'
const imageFile = formData.get('image'); // 假设字段名为 'image'
if (!imageFile) return new Response('Image file not found', { status: 400 });
// www.aagtool.top 的上传接口
@ -21,9 +22,21 @@ async function handleaagtoolRequest(request) {
method: 'POST',
body: formData,
headers: {
// 可以添加必要的自定义头部如果www.aagtool.top需要特定的头部信息
'Accept': 'application/json',
'Origin': 'https://www.aagtool.top'
'Accept': '*/*',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7',
'Cache-Control': 'no-cache',
'Content-Type': request.headers.get('Content-Type'),
'Origin': 'https://www.aagtool.top',
'Pragma': 'no-cache',
'Referer': 'https://www.aagtool.top/',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
'Sec-CH-UA': '"Chromium";v="124", "Google Chrome";v="124", "Not-A.Brand";v="99"',
'Sec-CH-UA-Mobile': '?0',
'Sec-CH-UA-Platform': '"Windows"',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'DNT': '1'
}
});
@ -31,7 +44,6 @@ async function handleaagtoolRequest(request) {
if (response.ok) {
const result = await response.json();
if (result && result.success && result.file_url) {
// 直接返回图片 URL 而不是 JSON 对象
return new Response(result.file_url, { status: 200 });
} else {
return new Response('Error: Unexpected response format or upload failed', { status: 500 });
@ -39,4 +51,8 @@ async function handleaagtoolRequest(request) {
} else {
return new Response('Error: ' + await response.text(), { status: response.status });
}
} catch (error) {
console.error('Caught an error:', error);
return new Response('Server Error', { status: 500 });
}
}

View File

@ -216,6 +216,7 @@ async function handleRequest(request) {
}
async function handleaagtoolRequest(request) {
try {
// 确认请求方法为 POST 并且内容类型正确
if (request.method !== 'POST' || !request.headers.get('Content-Type').includes('multipart/form-data')) {
return new Response('Invalid request', { status: 400 });
@ -223,7 +224,7 @@ async function handleRequest(request) {
// 解析表单数据
const formData = await request.formData();
const imageFile = formData.get('file'); // 假设字段名为 'file'
const imageFile = formData.get('image'); // 假设字段名为 'image'
if (!imageFile) return new Response('Image file not found', { status: 400 });
// www.aagtool.top 的上传接口
@ -234,9 +235,21 @@ async function handleRequest(request) {
method: 'POST',
body: formData,
headers: {
// 可以添加必要的自定义头部如果www.aagtool.top需要特定的头部信息
'Accept': 'application/json',
'Origin': 'https://www.aagtool.top'
'Accept': '*/*',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7',
'Cache-Control': 'no-cache',
'Content-Type': request.headers.get('Content-Type'),
'Origin': 'https://www.aagtool.top',
'Pragma': 'no-cache',
'Referer': 'https://www.aagtool.top/',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
'Sec-CH-UA': '"Chromium";v="124", "Google Chrome";v="124", "Not-A.Brand";v="99"',
'Sec-CH-UA-Mobile': '?0',
'Sec-CH-UA-Platform': '"Windows"',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'DNT': '1'
}
});
@ -244,7 +257,6 @@ async function handleRequest(request) {
if (response.ok) {
const result = await response.json();
if (result && result.success && result.file_url) {
// 直接返回图片 URL 而不是 JSON 对象
return new Response(result.file_url, { status: 200 });
} else {
return new Response('Error: Unexpected response format or upload failed', { status: 500 });
@ -252,6 +264,10 @@ async function handleRequest(request) {
} else {
return new Response('Error: ' + await response.text(), { status: response.status });
}
} catch (error) {
console.error('Caught an error:', error);
return new Response('Server Error', { status: 500 });
}
}