WorkerJS_CloudFlare_ImageBed/cloudflare-worker-js-api/API_IMG_tencent.js
2024-09-07 20:17:49 +08:00

53 lines
1.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 参考了 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
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 });
}
}