mirror of
https://github.com/MarSeventh/CloudFlare-ImgBed.git
synced 2026-01-31 09:03:19 +08:00
修复随机图API缓存不更新的问题
This commit is contained in:
@@ -39,7 +39,7 @@ export async function onRequest(context) {
|
||||
const fileId = file.name;
|
||||
const cdnUrl = `https://${url.hostname}/file/${fileId}`;
|
||||
|
||||
const success = await deleteFile(env, fileId, cdnUrl);
|
||||
const success = await deleteFile(env, fileId, cdnUrl, url);
|
||||
if (success) {
|
||||
deletedFiles.push(fileId);
|
||||
} else {
|
||||
@@ -79,7 +79,7 @@ export async function onRequest(context) {
|
||||
const fileId = params.path.split(',').join('/');
|
||||
const cdnUrl = `https://${url.hostname}/file/${fileId}`;
|
||||
|
||||
const success = await deleteFile(env, fileId, cdnUrl);
|
||||
const success = await deleteFile(env, fileId, cdnUrl, url);
|
||||
if (!success) {
|
||||
throw new Error('Delete file failed');
|
||||
}
|
||||
@@ -97,7 +97,7 @@ export async function onRequest(context) {
|
||||
}
|
||||
|
||||
// 删除单个文件的核心函数
|
||||
async function deleteFile(env, fileId, cdnUrl) {
|
||||
async function deleteFile(env, fileId, cdnUrl, url) {
|
||||
try {
|
||||
// 读取图片信息
|
||||
const img = await env.img_url.getWithMetadata(fileId);
|
||||
@@ -129,12 +129,8 @@ async function deleteFile(env, fileId, cdnUrl) {
|
||||
headers: { 'Cache-Control': 'max-age=0' },
|
||||
});
|
||||
|
||||
const keys = await cache.keys();
|
||||
for (let key of keys) {
|
||||
if (key.url.includes('/api/randomFileList')) {
|
||||
await cache.put(`${url.origin}/api/randomFileList`, nullResponse);
|
||||
}
|
||||
}
|
||||
const normalizedFolder = fileId.split('/').slice(0, -1).join('/');
|
||||
await cache.put(`${url.origin}/api/randomFileList?dir=${normalizedFolder}`, nullResponse);
|
||||
} catch (error) {
|
||||
console.error('Failed to clear cache:', error);
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ export async function onRequest(context) {
|
||||
const newFileId = `${folderDist}/${fileName}`;
|
||||
const cdnUrl = `https://${url.hostname}/file/${fileId}`;
|
||||
|
||||
const success = await moveFile(env, fileId, newFileId, cdnUrl);
|
||||
const success = await moveFile(env, fileId, newFileId, cdnUrl, url);
|
||||
if (success) {
|
||||
processedFiles.push(fileId);
|
||||
} else {
|
||||
@@ -93,7 +93,7 @@ export async function onRequest(context) {
|
||||
const newFileId = dist === '' ? fileKey : `${dist}/${fileKey}`;
|
||||
const cdnUrl = `https://${url.hostname}/file/${fileId}`;
|
||||
|
||||
const success = await moveFile(env, fileId, newFileId, cdnUrl);
|
||||
const success = await moveFile(env, fileId, newFileId, cdnUrl, url);
|
||||
if (!success) {
|
||||
throw new Error('Move file failed');
|
||||
}
|
||||
@@ -112,7 +112,7 @@ export async function onRequest(context) {
|
||||
}
|
||||
|
||||
// 移动单个文件的核心函数
|
||||
async function moveFile(env, fileId, newFileId, cdnUrl) {
|
||||
async function moveFile(env, fileId, newFileId, cdnUrl, url) {
|
||||
try {
|
||||
// 读取图片信息
|
||||
const img = await env.img_url.getWithMetadata(fileId);
|
||||
@@ -171,12 +171,10 @@ async function moveFile(env, fileId, newFileId, cdnUrl) {
|
||||
headers: { 'Cache-Control': 'max-age=0' },
|
||||
});
|
||||
|
||||
const keys = await cache.keys();
|
||||
for (let key of keys) {
|
||||
if (key.url.includes('/api/randomFileList')) {
|
||||
await cache.put(`${url.origin}/api/randomFileList`, nullResponse);
|
||||
}
|
||||
}
|
||||
const normalizedFolder = fileId.split('/').slice(0, -1).join('/');
|
||||
const normalizedDist = newFileId.split('/').slice(0, -1).join('/');
|
||||
await cache.put(`${url.origin}/api/randomFileList?dir=${normalizedFolder}`, nullResponse);
|
||||
await cache.put(`${url.origin}/api/randomFileList?dir=${normalizedDist}`, nullResponse);
|
||||
} catch (error) {
|
||||
console.error('Failed to clear cache:', error);
|
||||
}
|
||||
|
||||
@@ -117,8 +117,9 @@ async function getRandomFileList(env, url, dir) {
|
||||
let cursor = null;
|
||||
|
||||
do {
|
||||
const prefix = dir === ''? '' : dir + '/';
|
||||
const records = await env.img_url.list({
|
||||
prefix: dir,
|
||||
prefix: prefix,
|
||||
limit: 1000,
|
||||
cursor,
|
||||
});
|
||||
|
||||
@@ -205,7 +205,7 @@ export async function onRequestPost(context) { // Contents of context object
|
||||
|
||||
// 清除CDN缓存
|
||||
const cdnUrl = `https://${url.hostname}/file/${fullId}`;
|
||||
await purgeCDNCache(env, cdnUrl, url);
|
||||
await purgeCDNCache(env, cdnUrl, url, normalizedFolder);
|
||||
|
||||
|
||||
// ====================================不同渠道上传=======================================
|
||||
@@ -657,7 +657,7 @@ async function getFilePath(bot_token, file_id) {
|
||||
}
|
||||
}
|
||||
|
||||
async function purgeCDNCache(env, cdnUrl, url) {
|
||||
async function purgeCDNCache(env, cdnUrl, url, normalizedFolder) {
|
||||
if (env.dev_mode === 'true') {
|
||||
return;
|
||||
}
|
||||
@@ -677,12 +677,7 @@ async function purgeCDNCache(env, cdnUrl, url) {
|
||||
headers: { 'Cache-Control': 'max-age=0' },
|
||||
});
|
||||
|
||||
const keys = await cache.keys();
|
||||
for (let key of keys) {
|
||||
if (key.url.includes('/api/randomFileList')) {
|
||||
await cache.put(`${url.origin}/api/randomFileList`, nullResponse);
|
||||
}
|
||||
}
|
||||
await cache.put(`${url.origin}/api/randomFileList?dir=${normalizedFolder}`, nullResponse);
|
||||
} catch (error) {
|
||||
console.error('Failed to clear cache:', error);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user