From 233d20e8033686e84f1363bef45932b30e106bc2 Mon Sep 17 00:00:00 2001 From: BlueSkyXN <63384277+BlueSkyXN@users.noreply.github.com> Date: Thu, 12 Sep 2024 22:30:16 +0800 Subject: [PATCH] 0.8.4 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复tgph --- cloudflare-page/OneAPI-imgbed-MIX.html | 2 +- cloudflare-worker-js-api/API_IMG_tgph.js | 42 +++++++++++++++++++++ cloudflare-worker-js-api/API_IMG_tgphimg.js | 2 +- cloudflare-worker-js-api/worker.js | 12 ++++-- python-uploader/{test.py => test-aggmoe.py} | 0 python-uploader/test-tgph.py | 37 ++++++++++++++++++ 6 files changed, 89 insertions(+), 6 deletions(-) create mode 100644 cloudflare-worker-js-api/API_IMG_tgph.js rename python-uploader/{test.py => test-aggmoe.py} (100%) create mode 100644 python-uploader/test-tgph.py diff --git a/cloudflare-page/OneAPI-imgbed-MIX.html b/cloudflare-page/OneAPI-imgbed-MIX.html index a4076f6..de56a09 100644 --- a/cloudflare-page/OneAPI-imgbed-MIX.html +++ b/cloudflare-page/OneAPI-imgbed-MIX.html @@ -89,7 +89,7 @@ - +
diff --git a/cloudflare-worker-js-api/API_IMG_tgph.js b/cloudflare-worker-js-api/API_IMG_tgph.js new file mode 100644 index 0000000..6fa467e --- /dev/null +++ b/cloudflare-worker-js-api/API_IMG_tgph.js @@ -0,0 +1,42 @@ +addEventListener('fetch', event => { + event.respondWith(handleTgphimgRequest(event.request)); +}); + +async function handleTgphimgRequest(request) { + // 确认请求方法为 POST 并且内容类型正确 + if (request.method !== 'POST' || !request.headers.get('Content-Type').includes('multipart/form-data')) { + return new Response('Invalid request', { status: 400 }); + } + + // 解析表单数据 + const formData = await request.formData(); + const imageFile = formData.get('image'); // 假设字段名为 'image' + if (!imageFile) return new Response('Image file not found', { status: 400 }); + + // 修改字段名为 'file',以适应 Telegra.ph 的接口 + formData.append('file', imageFile); + formData.delete('image'); // 删除原来的 'image' 字段 + + // Telegra.ph 的上传接口 + const targetUrl = 'https://telegra.ph/upload?source=bugtracker'; + + // 发送修改后的表单数据 + const response = await fetch(targetUrl, { + method: 'POST', + body: formData + }); + + // 处理响应 + if (response.ok) { + const result = await response.json(); + if (result && result.src) { + // 提取 src 并拼接成完整的 URL + const imageUrl = `https://telegra.ph${result.src.replace(/\\/g, '')}`; // 处理反斜杠 + return new Response(imageUrl); + } else { + return new Response('Error: Unexpected response format', { status: 500 }); + } + } else { + return new Response('Error: ' + await response.text(), { status: response.status }); + } +} diff --git a/cloudflare-worker-js-api/API_IMG_tgphimg.js b/cloudflare-worker-js-api/API_IMG_tgphimg.js index 78e33f0..65881ef 100644 --- a/cloudflare-worker-js-api/API_IMG_tgphimg.js +++ b/cloudflare-worker-js-api/API_IMG_tgphimg.js @@ -14,7 +14,7 @@ async function handleTgphimgRequest(request) { if (!imageFile) return new Response('Image file not found', { status: 400 }); // Telegra.ph 的上传接口 - const targetUrl = 'https://telegra.ph/upload?source=bugtracker'; + const targetUrl = 'https://telegra.ph/upload'; // 为了与 Telegra.ph 接口兼容,我们保留表单数据的格式并直接转发 const response = await fetch(targetUrl, { diff --git a/cloudflare-worker-js-api/worker.js b/cloudflare-worker-js-api/worker.js index c8f5a6d..150d806 100644 --- a/cloudflare-worker-js-api/worker.js +++ b/cloudflare-worker-js-api/worker.js @@ -631,10 +631,14 @@ async function handleRequest(request) { const imageFile = formData.get('image'); // 假设字段名为 'image' if (!imageFile) return new Response('Image file not found', { status: 400 }); + // 修改字段名为 'file',以适应 Telegra.ph 的接口 + formData.append('file', imageFile); + formData.delete('image'); // 删除原来的 'image' 字段 + // Telegra.ph 的上传接口 const targetUrl = 'https://telegra.ph/upload?source=bugtracker'; - // 为了与 Telegra.ph 接口兼容,我们保留表单数据的格式并直接转发 + // 发送修改后的表单数据 const response = await fetch(targetUrl, { method: 'POST', body: formData @@ -643,9 +647,9 @@ async function handleRequest(request) { // 处理响应 if (response.ok) { const result = await response.json(); - if (result && result[0] && result[0].src) { - const imageUrl = `https://telegra.ph${result[0].src}`; - // 直接返回图片 URL 而不是 JSON 对象 + if (result && result.src) { + // 提取 src 并拼接成完整的 URL + const imageUrl = `https://telegra.ph${result.src.replace(/\\/g, '')}`; // 处理反斜杠 return new Response(imageUrl); } else { return new Response('Error: Unexpected response format', { status: 500 }); diff --git a/python-uploader/test.py b/python-uploader/test-aggmoe.py similarity index 100% rename from python-uploader/test.py rename to python-uploader/test-aggmoe.py diff --git a/python-uploader/test-tgph.py b/python-uploader/test-tgph.py new file mode 100644 index 0000000..2691f4d --- /dev/null +++ b/python-uploader/test-tgph.py @@ -0,0 +1,37 @@ +import requests + +# 图片上传接口的URL(与JS代码中一致) +upload_url = "https://telegra.ph/upload?source=bugtracker" + +# 要上传的图片路径 +image_path = r"F:\Download\20240707-204330.jpg" + +# 读取图片文件 +with open(image_path, 'rb') as image_file: + # 使用 multipart/form-data 发送文件 + files = { + 'file': image_file + } + + # 向接口发送POST请求 + response = requests.post(upload_url, files=files) + +# 打印响应状态码和内容 +print(f"状态码: {response.status_code}") +print(f"响应内容: {response.text}") + +# 检查响应 +if response.status_code == 200: + try: + result = response.json() + print(f"完整的JSON响应: {result}") + if isinstance(result, list) and result and 'src' in result[0]: + # 拼接图片的URL + image_url = f"https://telegra.ph{result[0]['src']}" + print(f"Image uploaded successfully: {image_url}") + else: + print(f"Unexpected response format: {response.text}") + except ValueError: + print(f"Failed to parse JSON response: {response.text}") +else: + print(f"Error uploading image: {response.status_code} - {response.text}")