diff --git a/README.md b/README.md
index 9ebe8da9..cdb23667 100644
--- a/README.md
+++ b/README.md
@@ -79,8 +79,9 @@ Add Features:
- **目录功能上线啦(感谢[fantasy-ke](https://github.com/fantasy-ke)协助)**,当前支持:
- 上传到指定目录
- 整目录删除
- - 文件位置移动
+ - 文件位置移动( Telegraph 和旧版 Telegram 渠道不支持移动)
- 按目录读取文件
+- 随机图API支持按目录读取
Fix Bugs:
@@ -101,6 +102,7 @@ Add Features:
- 整目录删除
- 文件位置移动( Telegraph 和旧版 Telegram 渠道不支持移动)
- 按目录读取文件
+- 随机图API支持按目录读取
Fix Bugs:
@@ -870,7 +872,7 @@ Web端在登录页面输入你的**认证码**即可登录使用;API端需要
| **接口功能** | 从图床中随机返回一张图片的链接(注意会消耗列出次数) |
| **前置条件** | 设置`AllowRandom`环境变量,值为`true` |
| **请求方法** | GET |
-| **请求参数** | **Query参数**:
`content`:返回的文件类型,可选值有`[image, video]`,多个使用`,`分隔,默认为`image`
`type`: 设为`img`时直接返回图片(此时form不生效);设为`url`时返回完整url链接;默认返回随机图的文件路径。
`form`: 设为`text`时直接返回文本,默认返回json格式内容。 |
+| **请求参数** | **Query参数**:
`content`:返回的文件类型,可选值有`[image, video]`,多个使用`,`分隔,默认为`image`
`type`: 设为`img`时直接返回图片(此时form不生效);设为`url`时返回完整url链接;默认返回随机图的文件路径。
`form`: 设为`text`时直接返回文本,默认返回json格式内容。
`dir`: 读取目录,使用相对路径,例如`img/test`会返回该目录以及所有子目录下的文件。 |
| **响应格式** | 1、当`type`为`img`时:
返回格式为`image/jpeg`
2、当`type`为其他值时:
当`form`不是`text`时,返回JSON格式内容,`data.url`为返回的链接/文件路径。
否则,直接返回链接/文件路径。 |
> **请求示例**:
diff --git a/functions/random.js b/functions/random.js
index 3bebd28b..dd00386b 100644
--- a/functions/random.js
+++ b/functions/random.js
@@ -37,8 +37,12 @@ export async function onRequest(context) {
fileType = fileType.split(',');
}
+ // 读取指定文件夹
+ const paramDir = requestUrl.searchParams.get('dir') || '';
+ const dir = paramDir.replace(/^\/+/, '').replace(/\/{2,}/g, '/').replace(/\/$/, '');
+
// 调用randomFileList接口,读取KV数据库中的所有记录
- let allRecords = await getRandomFileList(env, requestUrl);
+ let allRecords = await getRandomFileList(env, requestUrl, dir);
// 筛选出符合fileType要求的记录
allRecords = allRecords.filter(item => { return fileType.some(type => item.FileType.includes(type)) });
@@ -82,7 +86,7 @@ export async function onRequest(context) {
}
}
-async function getRandomFileList(env, url) {
+async function getRandomFileList(env, url, dir) {
// 检查缓存中是否有记录,有则直接返回
const cache = caches.default;
const cacheRes = await cache.match(`${url.origin}/api/randomFileList`);
@@ -95,6 +99,7 @@ async function getRandomFileList(env, url) {
do {
const records = await env.img_url.list({
+ prefix: dir,
limit: 1000,
cursor,
});