Skip to content

Commit

Permalink
优化block、404等状态的缓存策略,尽可能减少回源请求
Browse files Browse the repository at this point in the history
  • Loading branch information
MarSeventh committed Dec 13, 2024
1 parent d2a34c9 commit 74eb2a6
Showing 1 changed file with 72 additions and 28 deletions.
100 changes: 72 additions & 28 deletions functions/file/[id].js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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",
},
});
}
}

0 comments on commit 74eb2a6

Please sign in to comment.