Skip to content

Commit

Permalink
修复旧版本兼容性问题;优化读取文件操作逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
MarSeventh committed Nov 6, 2024
1 parent c219dd1 commit 3569bfe
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions functions/file/[id].js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ export async function onRequest(context) { // Contents of context object
const encodedFileName = encodeURIComponent(fileName);
const fileType = imgRecord.metadata?.FileType || null;

// 检查文件可访问状态
let accessRes = await returnWithCheck(request, env, url, imgRecord);
if (accessRes.status !== 200) {
return accessRes; // 如果不可访问,直接返回
}

// Cloudflare R2渠道
if (imgRecord.metadata?.Channel === 'CloudflareR2') {
// 检查是否配置了R2
Expand All @@ -73,7 +79,7 @@ export async function onRequest(context) { // Contents of context object
headers,
});

return await returnWithCheck(newRes, request, env, params, url);
return newRes;
}

// Telegram及Telegraph渠道
Expand Down Expand Up @@ -123,15 +129,17 @@ export async function onRequest(context) { // Contents of context object
});

if (response.ok) {
return await returnWithCheck(newRes, request, env, params, url);
return newRes;
}
return newRes;
} catch (error) {
return new Response('Error: ' + error, { status: 500 });
}
}

async function returnWithCheck(response, request, env, params, url) {
async function returnWithCheck(request, env, url, imgRecord) {
const response = new Response('good', { status: 200 });

// Referer header equal to the dashboard page
if (request.headers.get('Referer') == url.origin + "/dashboard") {
//show the image
Expand All @@ -140,7 +148,7 @@ async function returnWithCheck(response, request, env, params, url) {

if (typeof env.img_url == "undefined" || env.img_url == null || env.img_url == "") { } else {
//check the record from kv
const record = await env.img_url.getWithMetadata(params.id);
const record = imgRecord;
if (record.metadata === null) {
} else {
//if the record is not null, redirect to the image
Expand Down Expand Up @@ -170,7 +178,10 @@ async function returnWithCheck(response, request, env, params, url) {
}
}
}
// other cases
return response;
}

async function getFileContent(request, max_retries = 2) {
let retries = 0;
while (retries <= max_retries) {
Expand Down

0 comments on commit 3569bfe

Please sign in to comment.