Skip to content

Commit

Permalink
完善kv中无记录时的处理逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
MarSeventh committed Sep 17, 2024
1 parent 62c9fda commit d2ce008
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions functions/file/[id].js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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++;
}
Expand Down

0 comments on commit d2ce008

Please sign in to comment.