适配文件夹的分页读取

This commit is contained in:
MarSeventh
2025-03-06 18:48:31 +08:00
parent f00c8f1d1e
commit 030020e12a
19 changed files with 66 additions and 53 deletions

File diff suppressed because one or more lines are too long

BIN
css/107.9e7de4c9.css.gz Normal file

Binary file not shown.

Binary file not shown.

View File

@@ -1,52 +1,68 @@
export async function onRequest(context) {
// Contents of context object
const {
request, // same as existing Worker API
env, // same as existing Worker API
params, // if filename includes [id] or [[path]]
waitUntil, // same as ctx.waitUntil in existing Worker API
next, // used for middleware or to fetch assets
data, // arbitrary space for passing data between middlewares
} = context;
const { request, env } = context;
const url = new URL(request.url);
// 解析 URL 中的参数
let start = parseInt(url.searchParams.get('start'), 10) || 0;
let count = parseInt(url.searchParams.get('count'), 10) || 50;
let sum = url.searchParams.get('sum') || false;
let dir = url.searchParams.get('dir') || ''; // 目录名
let allRecords = await getAllRecords(env);
// 按照 metadata 中的时间戳倒序排序
allRecords.sort((a, b) => {
return b.metadata.TimeStamp - a.metadata.TimeStamp;
});
// count 为 -1 时,返回所有数据
if (count === -1) {
// sum 参数为 true 时,只返回数据总数
if (sum === 'true') {
return new Response(JSON.stringify(
{
sum: allRecords.length
}
), {
headers: { "Content-Type": "application/json" }
});
} else {
return new Response(JSON.stringify(allRecords), {
headers: { "Content-Type": "application/json" }
});
// 解析目录下的文件和子目录
let filteredRecords = [];
let subdirectories = new Set();
for (let record of allRecords) {
let key = record.name;
if (key.startsWith(dir)) {
let relativePath = key.substring(dir.length);
if (relativePath.startsWith('/')) {
relativePath = relativePath.substring(1);
}
let parts = relativePath.split('/');
if (parts.length === 1) {
// 直接位于该目录的文件
filteredRecords.push(record);
} else {
// 该目录下的子文件夹
subdirectories.add(parts[0]);
}
}
}
// 倒序返回指定数量的数据
start = Math.max(0, start); // start 不能小于 0
count = Math.max(1, count); // count 不能小于 1
const resultRecords = allRecords.slice(start, start + count);
// 按照 metadata 中的时间戳倒序排序
filteredRecords.sort((a, b) => {
return b.metadata.TimeStamp - a.metadata.TimeStamp;
});
// 只返回 `count` 条数据
return new Response(JSON.stringify(resultRecords), {
// sum 参数为 true 时,只返回数据总数
if (count === -1 && sum === 'true') {
return new Response(JSON.stringify({ sum: filteredRecords.length }), {
headers: { "Content-Type": "application/json" }
});
}
// count 为 -1 时返回所有数据
if (count === -1) {
return new Response(JSON.stringify({
files: filteredRecords,
directories: Array.from(subdirectories)
}), {
headers: { "Content-Type": "application/json" }
});
}
// 进行分页
start = Math.max(0, start);
count = Math.max(1, count);
const resultRecords = filteredRecords.slice(start, start + count);
return new Response(JSON.stringify({
files: resultRecords,
directories: Array.from(subdirectories)
}), {
headers: { "Content-Type": "application/json" }
});
}
@@ -56,20 +72,17 @@ async function getAllRecords(env) {
let cursor = null;
while (true) {
const limit = 1000; // 读取所需的最少数量
const limit = 1000;
const response = await env.img_url.list({ limit, cursor });
cursor = response.cursor;
// 过滤掉以 "manage@" 开头的 key
const filteredRecords = response.keys.filter(item => !item.name.startsWith("manage@"));
allRecords.push(...filteredRecords);
if (response.keys.length < limit) {
if (!cursor || filteredRecords.length < limit) {
break;
}
cursor = response.cursor;
}
return allRecords;
}
}

View File

@@ -1,4 +1,4 @@
<!doctype html><html lang=""><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="icon" href="/logo.png"><title>Sanyue ImgHub</title><script defer="defer" src="/js/app.04f11edd.js"></script><link href="/css/app.9a1a6b51.css" rel="stylesheet"></head><body><noscript><strong>We're sorry but sanyue_imghub doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id="app"></div></body></html><style>/* 下拉菜单样式 */
<!doctype html><html lang=""><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="icon" href="/logo.png"><title>Sanyue ImgHub</title><script defer="defer" src="/js/app.80a61b60.js"></script><link href="/css/app.9a1a6b51.css" rel="stylesheet"></head><body><noscript><strong>We're sorry but sanyue_imghub doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id="app"></div></body></html><style>/* 下拉菜单样式 */
.el-dropdown__popper.el-popper {
border-radius: 12px;
border: none;

Binary file not shown.

File diff suppressed because one or more lines are too long

BIN
js/107.f02efbed.js.gz Normal file

Binary file not shown.

1
js/107.f02efbed.js.map Normal file

File diff suppressed because one or more lines are too long

BIN
js/107.f02efbed.js.map.gz Normal file

Binary file not shown.

Binary file not shown.

File diff suppressed because one or more lines are too long

Binary file not shown.

Binary file not shown.

Binary file not shown.

File diff suppressed because one or more lines are too long

BIN
js/app.80a61b60.js.gz Normal file

Binary file not shown.

File diff suppressed because one or more lines are too long

BIN
js/app.80a61b60.js.map.gz Normal file

Binary file not shown.