From d462dc3d300ae949270fdda741448f651254084c Mon Sep 17 00:00:00 2001 From: MarSeventh <1193267292@qq.com> Date: Mon, 16 Sep 2024 16:33:27 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E5=9B=BE=E7=89=87=E5=AD=98?= =?UTF-8?q?=E5=8F=96=E7=AD=96=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- functions/file/[id].js | 46 +++++++++++++++++++----------------------- functions/upload.js | 5 ++--- 2 files changed, 23 insertions(+), 28 deletions(-) diff --git a/functions/file/[id].js b/functions/file/[id].js index 87146f95..29ebdbb6 100644 --- a/functions/file/[id].js +++ b/functions/file/[id].js @@ -9,8 +9,13 @@ export async function onRequest(context) { // Contents of context object next, // used for middleware or to fetch assets data, // arbitrary space for passing data between middlewares } = context; - // 解码params.id - params.id = decodeURIComponent(params.id); + + try { + // 解码params.id + params.id = decodeURIComponent(params.id); + } catch (e) { + return new Response('Error: Decode Image ID Failed', { status: 400 }); + } const url = new URL(request.url); let Referer = request.headers.get('Referer') @@ -41,11 +46,6 @@ export async function onRequest(context) { // Contents of context object return new Response('Error: Image not found', { status: 404 }); } - if (isTgChannel(imgRecord)) { - targetUrl = `https://api.telegram.org/file/bot${env.TG_BOT_TOKEN}/${imgRecord.metadata.TgFilePath}`; - } else { - targetUrl = 'https://telegra.ph/' + url.pathname + url.search; - } const fileName = imgRecord.metadata?.FileName || params.id; const encodedFileName = encodeURIComponent(fileName); const fileType = imgRecord.metadata?.FileType || null; @@ -65,7 +65,19 @@ export async function onRequest(context) { // Contents of context object // 旧版telegraph } - const response = await getFileContent(request, imgRecord, TgFileID, params.id, env, url); + // 构建目标 URL + if (isTgChannel(imgRecord)) { + // 获取TG图片真实地址 + const filePath = await getFilePath(env, TgFileID); + if (filePath === null) { + return new Response('Error: Failed to fetch image path', { status: 500 }); + } + targetUrl = `https://api.telegram.org/file/bot${env.TG_BOT_TOKEN}/${filePath}`; + } else { + targetUrl = 'https://telegra.ph/' + url.pathname + url.search; + } + + const response = await getFileContent(request); if (response === null) { return new Response('Error: Failed to fetch image', { status: 500 }); } @@ -130,7 +142,7 @@ export async function onRequest(context) { // Contents of context object } } -async function getFileContent(request, imgRecord, file_id, store_id, env, url, max_retries = 2) { +async function getFileContent(request, max_retries = 2) { let retries = 0; while (retries <= max_retries) { try { @@ -142,22 +154,6 @@ async function getFileContent(request, imgRecord, file_id, store_id, env, url, m if (response.ok || response.status === 304) { return response; } else { - // 若为TG渠道,更新TgFilePath - if (isTgChannel(imgRecord)) { - const filePath = await getFilePath(env, file_id); - if (filePath) { - imgRecord.metadata.TgFilePath = filePath; - await env.img_url.put(store_id, "", { - metadata: imgRecord.metadata, - }); - // 更新targetUrl - if (isTgChannel(imgRecord)) { - targetUrl = `https://api.telegram.org/file/bot${env.TG_BOT_TOKEN}/${imgRecord.metadata.TgFilePath}`; - } else { - targetUrl = 'https://telegra.ph/' + url.pathname + url.search; - } - } - } retries++; } } catch (error) { diff --git a/functions/upload.js b/functions/upload.js index 31132880..7a647374 100644 --- a/functions/upload.js +++ b/functions/upload.js @@ -149,7 +149,6 @@ export async function onRequestPost(context) { // Contents of context object // 构建独一无二的 ID const unique_index = time + Math.floor(Math.random() * 10000); const fullId = fileName? unique_index + '_' + fileName : unique_index + '.' + fileExt; - const encodedFullId = encodeURIComponent(fullId); // 若上传成功,将响应返回给客户端 if (response.ok) { res = new Response( @@ -164,7 +163,7 @@ export async function onRequestPost(context) { // Contents of context object if (apikey == undefined || apikey == null || apikey == "") { await env.img_url.put(fullId, "", { - metadata: { FileName: fileName, FileType: fileType, ListType: "None", Label: "None", TimeStamp: time, Channel: "TelegramNew", TgFilePath: filePath, TgFileId: id }, + metadata: { FileName: fileName, FileType: fileType, ListType: "None", Label: "None", TimeStamp: time, Channel: "TelegramNew", TgFileId: id }, }); } else { try { @@ -174,7 +173,7 @@ export async function onRequestPost(context) { // Contents of context object } const moderate_data = await fetchResponse.json(); await env.img_url.put(fullId, "", { - metadata: { FileName: fileName, FileType: fileType, ListType: "None", Label: moderate_data.rating_label, TimeStamp: time, Channel: "TelegramNew", TgFilePath: filePath, TgFileId: id }, + metadata: { FileName: fileName, FileType: fileType, ListType: "None", Label: moderate_data.rating_label, TimeStamp: time, Channel: "TelegramNew", TgFileId: id }, }); } catch (error) { console.error('Moderate Error:', error);