Skip to content

Commit

Permalink
调整图片存取策略
Browse files Browse the repository at this point in the history
  • Loading branch information
MarSeventh committed Sep 16, 2024
1 parent 4b73b7c commit d462dc3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 28 deletions.
46 changes: 21 additions & 25 deletions functions/file/[id].js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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;
Expand All @@ -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 });
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down
5 changes: 2 additions & 3 deletions functions/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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 {
Expand All @@ -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);
Expand Down

0 comments on commit d462dc3

Please sign in to comment.