From 74eb2a6bed5bf2d6ce98bd040391aec49322d3b7 Mon Sep 17 00:00:00 2001 From: MarSeventh <1193267292@qq.com> Date: Fri, 13 Dec 2024 14:26:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96block=E3=80=81404=E7=AD=89?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E7=9A=84=E7=BC=93=E5=AD=98=E7=AD=96=E7=95=A5?= =?UTF-8?q?=EF=BC=8C=E5=B0=BD=E5=8F=AF=E8=83=BD=E5=87=8F=E5=B0=91=E5=9B=9E?= =?UTF-8?q?=E6=BA=90=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- functions/file/[id].js | 100 +++++++++++++++++++++++++++++------------ 1 file changed, 72 insertions(+), 28 deletions(-) diff --git a/functions/file/[id].js b/functions/file/[id].js index 0dbb1482..b92b30e9 100644 --- a/functions/file/[id].js +++ b/functions/file/[id].js @@ -126,13 +126,7 @@ export async function onRequest(context) { // Contents of context object if (response === null) { return new Response('Error: Failed to fetch image', { status: 500 }); } else if (response.status === 404) { - return new Response(null, { - status: 302, - headers: { - "Location": url.origin + "/static/404.png", - "Cache-Control": "public, max-age=31536000" - } - }) + return await return404(url); } try { @@ -182,32 +176,14 @@ async function returnWithCheck(request, env, url, imgRecord) { if (record.metadata.ListType == "White") { return response; } else if (record.metadata.ListType == "Block") { - return new Response(null, { - status: 302, - headers: { - "Location": url.origin + "/static/BlockImg.png", - "Cache-Control": "public, max-age=31536000" - } - }) + return await returnBlockImg(url); } else if (record.metadata.Label == "adult") { - return new Response(null, { - status: 302, - headers: { - "Location": url.origin + "/static/BlockImg.png", - "Cache-Control": "public, max-age=31536000" - } - }) + return await returnBlockImg(url); } //check if the env variables WhiteList_Mode are set if (env.WhiteList_Mode == "true") { //if the env variables WhiteList_Mode are set, redirect to the image - return new Response(null, { - status: 302, - headers: { - "Location": url.origin + "/static/WhiteListOn.png", - "Cache-Control": "public, max-age=31536000" - } - }) + return await returnWhiteListImg(url); } else { //if the env variables WhiteList_Mode are not set, redirect to the image return response; @@ -265,4 +241,72 @@ async function getFilePath(env, file_id) { function isTgChannel(imgRecord) { return imgRecord.metadata?.Channel === 'Telegram' || imgRecord.metadata?.Channel === 'TelegramNew'; +} + +async function return404(url) { + const Img404 = await fetch(url.origin + "/static/404.png"); + if (!Img404.ok) { + return new Response(null, + { + status: 302, + headers: { + "Location": url.origin + "/static/404.png", + "Cache-Control": "public, max-age=86400" + } + } + ); + } else { + return new Response(Img404.body, { + status: 404, + headers: { + "Content-Type": "image/png", + "Content-Disposition": "inline", + "Cache-Control": "public, max-age=86400", + }, + }); + } +} + +async function returnBlockImg(url) { + const blockImg = await fetch(url.origin + "/static/BlockImg.png"); + if (!blockImg.ok) { + return new Response(null, { + status: 302, + headers: { + "Location": url.origin + "/static/BlockImg.png", + "Cache-Control": "public, max-age=86400" + } + }) + } else { + return new Response(blockImg.body, { + status: 403, + headers: { + "Content-Type": "image/png", + "Content-Disposition": "inline", + "Cache-Control": "public, max-age=86400", + }, + }); + } +} + +async function returnWhiteListImg(url) { + const WhiteListImg = await fetch(url.origin + "/static/WhiteListOn.png"); + if (!WhiteListImg.ok) { + return new Response(null, { + status: 302, + headers: { + "Location": url.origin + "/static/WhiteListOn.png", + "Cache-Control": "public, max-age=86400" + } + }) + } else { + return new Response(WhiteListImg.body, { + status: 200, + headers: { + "Content-Type": "image/png", + "Content-Disposition": "inline", + "Cache-Control": "public, max-age=86400", + }, + }); + } } \ No newline at end of file