Feat: 随机图API支持指定目录

This commit is contained in:
MarSeventh
2025-03-07 18:53:36 +08:00
parent 8fb8f238aa
commit 6e5b8d6be6
4 changed files with 70 additions and 52 deletions

View File

@@ -70,62 +70,68 @@ export async function onRequest(context) {
try {
// 读取图片信息
const img = await env.img_url.getWithMetadata(fileId);
// 读取图片信息
const img = await env.img_url.getWithMetadata(fileId);
// 如果是R2渠道的图片删除R2中对应的图片
if (img.metadata?.Channel === 'CloudflareR2') {
await env.img_r2.delete(fileId);
}
// 如果是R2渠道的图片删除R2中对应的图片
if (img.metadata?.Channel === 'CloudflareR2') {
await env.img_r2.delete(fileId);
}
// S3 渠道的图片删除S3中对应的图片
if (img.metadata?.Channel === "S3") {
const s3Client = new S3Client({
region: img.metadata?.S3Region || "auto", // 默认使用 auto 区域
endpoint: img.metadata?.S3Endpoint,
credentials: {
accessKeyId: img.metadata?.S3AccessKeyId,
secretAccessKey: img.metadata?.S3SecretAccessKey
},
});
// S3 渠道的图片删除S3中对应的图片
if (img.metadata?.Channel === "S3") {
const s3Client = new S3Client({
region: img.metadata?.S3Region || "auto", // 默认使用 auto 区域
endpoint: img.metadata?.S3Endpoint,
credentials: {
accessKeyId: img.metadata?.S3AccessKeyId,
secretAccessKey: img.metadata?.S3SecretAccessKey
},
});
const bucketName = img.metadata?.S3BucketName;
const key = img.metadata?.S3FileKey;
const bucketName = img.metadata?.S3BucketName;
const key = img.metadata?.S3FileKey;
try {
const command = new DeleteObjectCommand({
Bucket: bucketName,
Key: key,
});
try {
const command = new DeleteObjectCommand({
Bucket: bucketName,
Key: key,
});
await s3Client.send(command);
} catch (error) {
return new Response(`Error: S3 Delete Failed - ${error.message}`, { status: 500 });
}
}
await s3Client.send(command);
} catch (error) {
return new Response(`Error: S3 Delete Failed - ${error.message}`, { status: 500 });
}
}
// 删除KV中的图片信息
await env.img_url.delete(fileId);
const info = JSON.stringify(fileId);
// 删除KV中的图片信息
await env.img_url.delete(fileId);
const info = JSON.stringify(fileId);
// 清除CDN缓存
await purgeCFCache(env, cdnUrl);
// 清除CDN缓存
await purgeCFCache(env, cdnUrl);
// 清除api/randomFileList API缓存
try {
const cache = caches.default;
// 通过写入一个max-age=0的response来清除缓存
const nullResponse = new Response(null, {
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);
}
}
} catch (error) {
console.error('Failed to clear cache:', error);
}
// 清除api/randomFileList API缓存
try {
const cache = caches.default;
// 通过写入一个max-age=0的response来清除缓存
const nullResponse = new Response(null, {
headers: { 'Cache-Control': 'max-age=0' },
});
await cache.put(`${url.origin}/api/randomFileList`, nullResponse);
} catch (error) {
console.error('Failed to clear cache:', error);
}
return new Response(info);
return new Response(info);
} catch (e) {
return new Response('Error: Delete Image Failed', { status: 400 });
return new Response('Error: Delete Image Failed', { status: 400 });
}
}