WorkerJS_CloudFlare_ImageBed/cloudflare-worker-js-api/API_IMG_tencent.js

53 lines
1.7 KiB
JavaScript
Raw Normal View History

2024-09-07 20:17:49 +08:00
// 参考了 https://github.com/k08255-lxm/WX-MT_Image/blob/main/upload_wechat.php
// 以及 https://github.com/x-dr/telegraph-Image/blob/main/src/app/api/tencent/route.js
2024-09-07 20:07:59 +08:00
async function handleTencentRequest(request) {
try {
// 确保请求方法为 POST
if (request.method !== 'POST') {
return new Response('Method not allowed', { status: 405 });
}
// 解析multipart/form-data
const formData = await request.formData();
const imageFile = formData.get('image'); // 假设前端发送的字段名是 'image'
if (!imageFile) {
return new Response('No image file found in the request', { status: 400 });
}
// 准备发送到腾讯接口的FormData
const uploadFormData = new FormData();
uploadFormData.append('media', imageFile, imageFile.name);
// 腾讯的上传URL
const uploadUrl = "https://openai.weixin.qq.com/weixinh5/webapp/h774yvzC2xlB4bIgGfX2stc4kvC85J/cos/upload";
// 发送请求到腾讯接口
const response = await fetch(uploadUrl, {
method: 'POST',
body: uploadFormData
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const result = await response.json();
if (result.url) {
// 如果成功返回图片URL
return new Response(result.url, {
status: 200,
headers: { 'Content-Type': 'text/plain' }
});
} else {
// 如果没有返回URL则可能上传失败
return new Response('Upload failed: No URL returned', { status: 500 });
}
} catch (error) {
console.error('Error in handleTencentRequest:', error);
return new Response(`Upload failed: ${error.message}`, { status: 500 });
}
}