Skip to content

Commit

Permalink
improve file/[id]
Browse files Browse the repository at this point in the history
  • Loading branch information
DJChanahCJD committed Dec 10, 2024
1 parent c01edad commit ed89cde
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 62 deletions.
1 change: 0 additions & 1 deletion admin-imgtc.css
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,6 @@ body {

.control-btn:hover {
background: rgba(255, 255, 255, 0.3);
transform: scale(1.05);
}

/* 移动端适配 */
Expand Down
121 changes: 60 additions & 61 deletions functions/file/[id].js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export async function onRequest(context) {
const filePath = await getFilePath(env, url.pathname.split(".")[0].split("/")[2]);
console.log(filePath)
fileUrl = `https://api.telegram.org/file/bot${env.TG_Bot_Token}/${filePath}`;

}

const response = await fetch(fileUrl, {
Expand All @@ -31,78 +30,78 @@ export async function onRequest(context) {
body: request.body,
});

// If the response is OK, proceed with further checks
if (!response.ok) return response;

// Log response details
console.log(response.ok, response.status);

// If the response is OK, proceed with further checks
if (response.ok) {
// Allow the admin page to directly view the image
const isAdmin = request.headers.get('Referer')?.includes(`${url.origin}/admin`);
if (isAdmin) {
return response;
}
// Allow the admin page to directly view the image
const isAdmin = request.headers.get('Referer')?.includes(`${url.origin}/admin`);
if (isAdmin) {
return response;
}

// check if kv storage is available
if (!env.img_url) {
console.error("KV storage not available");
return new Response("img_url KV storage not configured", { status: 500 });
}

const record = await env.img_url.getWithMetadata(params.id);
console.log("Record:", record);
// Fetch KV metadata if available
if (env.img_url) {
// Ensure metadata exists and add default values for missing properties
if (record && record.metadata) {
const metadata = {
ListType: record.metadata.ListType || "None",
Label: record.metadata.Label || "None",
TimeStamp: record.metadata.TimeStamp || Date.now(),
liked: record.metadata.liked !== undefined ? record.metadata.liked : false,
fileName: record.metadata.fileName || params.id, // 添加原始文件名
};

// Handle based on ListType and Label
if (metadata.ListType === "White") {
return response;
} else if (metadata.ListType === "Block" || metadata.Label === "adult") {
const referer = request.headers.get('Referer');
const redirectUrl = referer ? "https://static-res.pages.dev/teleimage/img-block-compressed.png" : `${url.origin}/block-img.html`;
return Response.redirect(redirectUrl, 302);
}

// Check if WhiteList_Mode is enabled
if (env.WhiteList_Mode === "true") {
return Response.redirect(`${url.origin}/whitelist-on.html`, 302);
}
} else {
// If metadata does not exist, initialize it in KV with default values
await env.img_url.put(params.id, "", {
metadata: { ListType: "None", Label: "None", TimeStamp: Date.now(), liked: false, fileName: params.id },
});
let record = await env.img_url.getWithMetadata(params.id);
if (!record || !record.metadata) {
// Initialize metadata if it doesn't exist
console.log("metadata not found, initializing...");
record = {
metadata: {
ListType: "None",
Label: "None",
TimeStamp: Date.now(),
liked: false,
fileName: params.id
}
}
};
await env.img_url.put(params.id, "", { metadata: record.metadata });
}

// If no metadata or further actions required, moderate content and add to KV if needed
if (env.ModerateContentApiKey) {
const moderateResponse = await fetch(`https://api.moderatecontent.com/moderate/?key=${env.ModerateContentApiKey}&url=https://telegra.ph${url.pathname}${url.search}`);
const moderateData = await moderateResponse.json();
console.log("Moderate Data:", moderateData);
const metadata = {
ListType: record.metadata.ListType || "None",
Label: record.metadata.Label || "None",
TimeStamp: record.metadata.TimeStamp || Date.now(),
liked: record.metadata.liked !== undefined ? record.metadata.liked : false,
fileName: record.metadata.fileName || params.id,
};

if (env.img_url) {
await env.img_url.put(params.id, "", {
metadata: { ListType: "None", Label: moderateData.rating_label, TimeStamp: record.metadata.TimeStamp || Date.now(), liked: false, fileName: record.metadata.fileName || params.id },
});
}
// Handle based on ListType and Label
if (metadata.ListType === "White") {
return response;
} else if (metadata.ListType === "Block" || metadata.Label === "adult") {
const referer = request.headers.get('Referer');
const redirectUrl = referer ? "https://static-res.pages.dev/teleimage/img-block-compressed.png" : `${url.origin}/block-img.html`;
return Response.redirect(redirectUrl, 302);
}

if (moderateData.rating_label === "adult") {
return Response.redirect(`${url.origin}/block-img.html`, 302);
}
} else if (env.img_url) {
// Add image to KV with default metadata if ModerateContentApiKey is not available
console.log("KV not enabled for moderation, adding default metadata.");
await env.img_url.put(params.id, "", {
metadata: { ListType: "None", Label: "None", TimeStamp: record.metadata.TimeStamp || Date.now(), liked: false, fileName: record.metadata.fileName || params.id },
});
// Check if WhiteList_Mode is enabled
if (env.WhiteList_Mode === "true") {
return Response.redirect(`${url.origin}/whitelist-on.html`, 302);
}

// If no metadata or further actions required, moderate content and add to KV if needed
if (env.ModerateContentApiKey) {
const moderateResponse = await fetch(`https://api.moderatecontent.com/moderate/?key=${env.ModerateContentApiKey}&url=https://telegra.ph${url.pathname}${url.search}`);
const moderateData = await moderateResponse.json();
console.log("Moderate Data:", moderateData);
metadata.Label = moderateData.rating_label;

if (moderateData.rating_label === "adult") {
await env.img_url.put(params.id, "", { metadata });
return Response.redirect(`${url.origin}/block-img.html`, 302);
}
}

// save metadata directly, no additional conditions are needed
await env.img_url.put(params.id, "", { metadata });
return response;

}

async function getFilePath(env, file_id) {
Expand Down

0 comments on commit ed89cde

Please sign in to comment.