From d2ce008e5d192c0a6fc48e46843041a0d714532b Mon Sep 17 00:00:00 2001 From: MarSeventh <1193267292@qq.com> Date: Tue, 17 Sep 2024 14:05:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84kv=E4=B8=AD=E6=97=A0=E8=AE=B0?= =?UTF-8?q?=E5=BD=95=E6=97=B6=E7=9A=84=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- functions/file/[id].js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/functions/file/[id].js b/functions/file/[id].js index 29ebdbb6..0f93265d 100644 --- a/functions/file/[id].js +++ b/functions/file/[id].js @@ -41,11 +41,8 @@ export async function onRequest(context) { // Contents of context object return new Response('Error: Please configure KV database', { status: 500 }); } const imgRecord = await env.img_url.getWithMetadata(params.id); - // 图片是否存在 - if (imgRecord === null || imgRecord?.metadata === null) { - return new Response('Error: Image not found', { status: 404 }); - } - + // 如果meatdata不存在,只可能是之前未设置KV,且存储在Telegraph上的图片,那么在后面获取时会返回404错误,此处不用处理 + const fileName = imgRecord.metadata?.FileName || params.id; const encodedFileName = encodeURIComponent(fileName); const fileType = imgRecord.metadata?.FileType || null; @@ -80,6 +77,8 @@ export async function onRequest(context) { // Contents of context object const response = await getFileContent(request); if (response === null) { return new Response('Error: Failed to fetch image', { status: 500 }); + } else if (response.status === 404) { + return new Response('Error: Image Not Found', { status: 404 }); } try { @@ -153,6 +152,8 @@ async function getFileContent(request, max_retries = 2) { }); if (response.ok || response.status === 304) { return response; + } else if (response.status === 404) { + return new Response('Error: Image Not Found', { status: 404 }); } else { retries++; }