mirror of
https://github.com/BlueSkyXN/WorkerJS_CloudFlare_ImageBed.git
synced 2024-11-16 03:32:26 +08:00
53 lines
1.7 KiB
JavaScript
53 lines
1.7 KiB
JavaScript
// 参考了 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 });
|
||
}
|
||
}
|