mirror of
https://github.com/BlueSkyXN/WorkerJS_CloudFlare_ImageBed.git
synced 2024-11-16 03:32:26 +08:00
0.10.0
新增AliEx接口
This commit is contained in:
parent
d858aaea50
commit
9535feaff4
|
@ -72,7 +72,7 @@
|
|||
<select class="form-control" id="apiSelect">
|
||||
<option value="ipfs">IPFS-去中心化多网关兼容</option>
|
||||
<option value="58img">58img-定期删图</option>
|
||||
<option value="aliex">Ali</option>
|
||||
<option value="aliex">AliEx-国内CDN国外Akamai</option>
|
||||
<option value="tgphimg">TGPH-Debug通道</option>
|
||||
<option value="qst8">qst8-国内CDN</option>
|
||||
<option value="vviptuangou">vviptuangou-国内CDN</option>
|
||||
|
|
|
@ -4,71 +4,126 @@ export default {
|
|||
}
|
||||
};
|
||||
|
||||
async function handleAliExpressRequest(request, env) {
|
||||
/*
|
||||
参考了即刻图床开源的阿里接口 https://jike.info/topic/36748
|
||||
需要使用美国等地访问 https://www.aliexpress.com/ 并使用第三方直接注册和登录,比如谷歌,如果访问地异常则不会出现第三方登录
|
||||
该模块需要阿里国际账号,虽然不需要实名、可以随便注册但需要Cookie。有效期不清楚,有点像一个月。
|
||||
这个试验我放在了KV库理论上也可以用D1库,不用Env直接装载是因为Cookie有点长,大概3KB,而Env最大就5KB好像(对于免费用户)
|
||||
你需要创建和绑定名为 WORKER_IMGBED 的库,其中新建 K 字段,名为 ali_express_cookie 然后在V中复制进去浏览器F12得到的完整Cookie即可
|
||||
返回图片示例为 https://ae01.alicdn.com/kf 的域名
|
||||
*/
|
||||
|
||||
async function handleAliExpressRequest(request) {
|
||||
try {
|
||||
// 确保请求方法为 POST
|
||||
if (request.method !== 'POST') {
|
||||
return new Response('Method not allowed', { status: 405 });
|
||||
return new Response('Method not allowed', {
|
||||
status: 405,
|
||||
headers: {
|
||||
'Content-Type': 'text/plain',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// 解析 multipart/form-data
|
||||
const formData = await request.formData();
|
||||
const imageFile = formData.get('image'); // 假设前端发送的字段名是 'image'
|
||||
const imageFile = formData.get('image');
|
||||
if (!imageFile) {
|
||||
return new Response('No image file found in the request', { status: 400 });
|
||||
return new Response('No image file found in the request', {
|
||||
status: 400,
|
||||
headers: {
|
||||
'Content-Type': 'text/plain',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// 从 Cloudflare KV 中读取 Cookie
|
||||
const cookie = await env.WORKER_IMGBED.get('ali_express_cookie');
|
||||
// 从 KV 中获取 Cookie
|
||||
const cookie = await WORKER_IMGBED.get('ali_express_cookie');
|
||||
if (!cookie) {
|
||||
return new Response('Missing required cookie in KV storage', { status: 500 });
|
||||
console.error('Missing required cookie in KV storage');
|
||||
return new Response('Missing required cookie in KV storage', {
|
||||
status: 500,
|
||||
headers: {
|
||||
'Content-Type': 'text/plain',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// 准备发送到 AliExpress 的 FormData
|
||||
console.log(`Retrieved Cookie from KV: ${cookie}`);
|
||||
|
||||
// 构建上传表单数据
|
||||
const uploadFormData = new FormData();
|
||||
uploadFormData.append('file', imageFile, imageFile.name);
|
||||
uploadFormData.append('bizCode', 'ae_profile_avatar_upload');
|
||||
|
||||
// AliExpress 的上传 URL
|
||||
const uploadUrl = 'https://filebroker.aliexpress.com/x/upload?jiketuchuang=1';
|
||||
|
||||
// 发送请求到 AliExpress 接口
|
||||
// 发送 POST 请求到 AliExpress API
|
||||
const response = await fetch(uploadUrl, {
|
||||
method: 'POST',
|
||||
body: uploadFormData,
|
||||
headers: {
|
||||
'Origin': 'https://filebroker.aliexpress.com',
|
||||
'Cookie': cookie
|
||||
}
|
||||
'Cookie': cookie, // 使用 KV 中的 Cookie
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
const errorText = await response.text();
|
||||
console.error(`Upload failed: Status ${response.status}, Body: ${errorText}`);
|
||||
return new Response(`Upload failed: HTTP error! Status: ${response.status}`, {
|
||||
status: 500,
|
||||
headers: {
|
||||
'Content-Type': 'text/plain',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const result = await response.json();
|
||||
const resultText = await response.text();
|
||||
let result;
|
||||
try {
|
||||
result = JSON.parse(resultText);
|
||||
} catch (parseError) {
|
||||
console.error(`Error parsing response: ${parseError.message}, Response Text: ${resultText}`);
|
||||
return new Response(`Upload failed: Error parsing response - ${parseError.message}`, {
|
||||
status: 500,
|
||||
headers: {
|
||||
'Content-Type': 'text/plain',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (result.url) {
|
||||
// 如果成功,返回图片 URL
|
||||
console.log(`Upload successful: ${result.url}`);
|
||||
return new Response(result.url, {
|
||||
status: 200,
|
||||
headers: { 'Content-Type': 'text/plain' }
|
||||
headers: {
|
||||
'Content-Type': 'text/plain',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
},
|
||||
});
|
||||
} else {
|
||||
// 如果没有返回 URL,则可能上传失败
|
||||
return new Response('Upload failed: No URL returned', { status: 500 });
|
||||
console.error(`Upload failed: No URL returned, Response: ${resultText}`);
|
||||
return new Response('Upload failed: No URL returned', {
|
||||
status: 500,
|
||||
headers: {
|
||||
'Content-Type': 'text/plain',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
},
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error in handleAliExpressRequest:', error);
|
||||
return new Response(`Upload failed: ${error.message}`, { status: 500 });
|
||||
console.error(`Error in handleAliExpressRequest: ${error.stack}`);
|
||||
return new Response(`Upload failed: ${error.message}`, {
|
||||
status: 500,
|
||||
headers: {
|
||||
'Content-Type': 'text/plain',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// KV 命名空间设计
|
||||
// 在 Cloudflare 中创建一个名为 "WORKER_IMGBED" 的 KV 命名空间,用于存储不同接口所需的 Cookie。
|
||||
// 使用 "WORKER_IMGBED" 的键 "ali_express_cookie" 来存储具体的 AliExpress Cookie 字符串。
|
||||
|
||||
// 例如,在 wrangler.toml 文件中添加:
|
||||
// [[kv_namespaces]]
|
||||
// binding = "WORKER_IMGBED"
|
||||
// id = "<your_kv_namespace_id>"
|
|
@ -63,7 +63,7 @@ async function handleRequest(request) {
|
|||
response = await handleTgphimgRequest(request);
|
||||
break;
|
||||
case '/upload/aliex':
|
||||
response = await handleAliExpressRequest(request, env);
|
||||
response = await handleAliExpressRequest(request);
|
||||
break;
|
||||
default:
|
||||
response = new Response('Not Found', { status: 404 });
|
||||
|
@ -606,64 +606,117 @@ async function handleRequest(request) {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
async function handleAliExpressRequest(request, env) {
|
||||
async function handleAliExpressRequest(request) {
|
||||
try {
|
||||
// 确保请求方法为 POST
|
||||
if (request.method !== 'POST') {
|
||||
return new Response('Method not allowed', { status: 405 });
|
||||
return new Response('Method not allowed', {
|
||||
status: 405,
|
||||
headers: {
|
||||
'Content-Type': 'text/plain',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// 解析 multipart/form-data
|
||||
const formData = await request.formData();
|
||||
const imageFile = formData.get('image'); // 假设前端发送的字段名是 'image'
|
||||
const imageFile = formData.get('image');
|
||||
if (!imageFile) {
|
||||
return new Response('No image file found in the request', { status: 400 });
|
||||
return new Response('No image file found in the request', {
|
||||
status: 400,
|
||||
headers: {
|
||||
'Content-Type': 'text/plain',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// 从 Cloudflare KV 中读取 Cookie
|
||||
const cookie = await env.WORKER_IMGBED.get('ali_express_cookie');
|
||||
// 从 KV 中获取 Cookie
|
||||
const cookie = await WORKER_IMGBED.get('ali_express_cookie');
|
||||
if (!cookie) {
|
||||
return new Response('Missing required cookie in KV storage', { status: 500 });
|
||||
console.error('Missing required cookie in KV storage');
|
||||
return new Response('Missing required cookie in KV storage', {
|
||||
status: 500,
|
||||
headers: {
|
||||
'Content-Type': 'text/plain',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// 准备发送到 AliExpress 的 FormData
|
||||
console.log(`Retrieved Cookie from KV: ${cookie}`);
|
||||
|
||||
// 构建上传表单数据
|
||||
const uploadFormData = new FormData();
|
||||
uploadFormData.append('file', imageFile, imageFile.name);
|
||||
uploadFormData.append('bizCode', 'ae_profile_avatar_upload');
|
||||
|
||||
// AliExpress 的上传 URL
|
||||
const uploadUrl = 'https://filebroker.aliexpress.com/x/upload?jiketuchuang=1';
|
||||
|
||||
// 发送请求到 AliExpress 接口
|
||||
// 发送 POST 请求到 AliExpress API
|
||||
const response = await fetch(uploadUrl, {
|
||||
method: 'POST',
|
||||
body: uploadFormData,
|
||||
headers: {
|
||||
'Origin': 'https://filebroker.aliexpress.com',
|
||||
'Cookie': cookie
|
||||
}
|
||||
'Cookie': cookie, // 使用 KV 中的 Cookie
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
const errorText = await response.text();
|
||||
console.error(`Upload failed: Status ${response.status}, Body: ${errorText}`);
|
||||
return new Response(`Upload failed: HTTP error! Status: ${response.status}`, {
|
||||
status: 500,
|
||||
headers: {
|
||||
'Content-Type': 'text/plain',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const result = await response.json();
|
||||
const resultText = await response.text();
|
||||
let result;
|
||||
try {
|
||||
result = JSON.parse(resultText);
|
||||
} catch (parseError) {
|
||||
console.error(`Error parsing response: ${parseError.message}, Response Text: ${resultText}`);
|
||||
return new Response(`Upload failed: Error parsing response - ${parseError.message}`, {
|
||||
status: 500,
|
||||
headers: {
|
||||
'Content-Type': 'text/plain',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (result.url) {
|
||||
// 如果成功,返回图片 URL
|
||||
console.log(`Upload successful: ${result.url}`);
|
||||
return new Response(result.url, {
|
||||
status: 200,
|
||||
headers: { 'Content-Type': 'text/plain' }
|
||||
headers: {
|
||||
'Content-Type': 'text/plain',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
},
|
||||
});
|
||||
} else {
|
||||
// 如果没有返回 URL,则可能上传失败
|
||||
return new Response('Upload failed: No URL returned', { status: 500 });
|
||||
console.error(`Upload failed: No URL returned, Response: ${resultText}`);
|
||||
return new Response('Upload failed: No URL returned', {
|
||||
status: 500,
|
||||
headers: {
|
||||
'Content-Type': 'text/plain',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
},
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error in handleAliExpressRequest:', error);
|
||||
return new Response(`Upload failed: ${error.message}`, { status: 500 });
|
||||
console.error(`Error in handleAliExpressRequest: ${error.stack}`);
|
||||
return new Response(`Upload failed: ${error.message}`, {
|
||||
status: 500,
|
||||
headers: {
|
||||
'Content-Type': 'text/plain',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user