Skip to content

Commit 19c656f

Browse files
committed
处理一直存在的逻辑错误
maybe方案345还有小错误
1 parent 673cf98 commit 19c656f

File tree

5 files changed

+57
-56
lines changed

5 files changed

+57
-56
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414

1515
[https://demo2.randomimg.sfacg.ltd/api](https://demo2.randomimg.sfacg.ltd/api)
1616

17-
[https://demo2.randomimg.sfacg.ltd/api?cat=示例图](https://demo2.randomimg.sfacg.ltd/api?cat=示例图)
17+
[https://demo2.randomimg.sfacg.ltd/api?cat=demoimg1](https://demo2.randomimg.sfacg.ltd/api?cat=demoimg1)
1818

1919
[https://demo2.randomimg.sfacg.ltd/api?cat=demoimg&id=8](https://demo2.randomimg.sfacg.ltd/api?cat=demoimg&id=8)没有对应资源返回状态码404和`404.html`
2020

21-
[https://demo2.randomimg.sfacg.ltd/api?cat=demoimg&cat=示例图](https://demo2.randomimg.sfacg.ltd/api?cat=demoimg&cat=示例图)多分类抽取
21+
[https://demo2.randomimg.sfacg.ltd/api?cat=demoimg&cat=demoimg1](https://demo2.randomimg.sfacg.ltd/api?cat=demoimg&cat=demoimg1)多分类抽取
2222

2323
[https://demo2.randomimg.sfacg.ltd/api?cat=demoimg&id=4](https://demo2.randomimg.sfacg.ltd/api?cat=demoimg&id=4)查看`demoimg`下的`4.jpg`
2424

@@ -112,11 +112,11 @@ PS:
112112
- json返回包含:分类`cat`,图片id`id`,图片github原链接`githubUrl`,worker代理链接`workerUrl`,ghproxy代理链接`proxyUrl`
113113
```
114114
{
115-
"category": "示例图",
115+
"category": "demoimg1",
116116
"id": 2,
117-
"githubUrl": "https://raw.githubusercontent.com/Cheshire-Nya/easy-random-image-api/main/示例图/2.jpg",
118-
"workerUrl": "https://demo2.randomimg.sfacg.ltd/api?id=2&cat=示例图",
119-
"proxyUrl": "https://ghproxy.com/https://raw.githubusercontent.com/Cheshire-Nya/easy-random-image-api/main/示例图/2.jpg"
117+
"githubUrl": "https://raw.githubusercontent.com/Cheshire-Nya/easy-random-image-api/main/demoimg1/2.jpg",
118+
"workerUrl": "https://demo2.randomimg.sfacg.ltd/api?id=2&cat=demoimg1",
119+
"proxyUrl": "https://ghproxy.com/https://raw.githubusercontent.com/Cheshire-Nya/easy-random-image-api/main/demoimg1/2.jpg"
120120
}
121121
```
122122

worker.js

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ var imgHost = "https://raw.githubusercontent.com/Cheshire-Nya/easy-random-image-
1111
//用github作为图库应按照此格式"https://raw.githubusercontent.com/<github用户名>/<仓库名>/<分支名>"
1212
var defaultPath = '/'; //现在是仓库根目录(换其他文件夹格式`'/<文件夹名>'`)
1313
//访问的url路径为`/api`或`/api/`时抽图的文件夹
14-
var redirectProxy = 2;
14+
var redirectProxy = 1;
1515
//type=302时返回的链接是否是经过代理的,0 不代理(返回github原链接),1 worker代理,2 ghproxy代理
1616
var maxValues = {
1717
"/": 2, //(defaultPath和对应图片数)现在是仓库根目录,对应2张图,前部需要有`/`
18-
"示例图": 10, //示例图
19-
//中文路径
18+
"demoimg1": 10, //示例图
19+
//路径1
2020
"demoimg": 5, //demoimg
21-
//英文路径
21+
//路径2
22+
//文件夹名需为纯英文字符,中文在链接中会被转码然后出奇奇怪怪的问题
2223
//其他要抽图的文件夹和对应图片数,不用在名称前加`/`
2324
//其他路径下同理,只需要这样相同格式多写一条键值对即可`'<文件夹名>': <数值>,`
2425
}
@@ -79,7 +80,7 @@ function extractSearch(urlSearch, request) {
7980
else return error();
8081
}
8182
else if (cats) {
82-
let allCatsValid = true;
83+
/*多余的错误逻辑
8384
for (let i = 0; i < cats.length; i++) {
8485
if (!(cats[i] in maxValues) || maxValues[cats[i]] < 1) {
8586
allCatsValid = false;
@@ -88,38 +89,36 @@ function extractSearch(urlSearch, request) {
8889
maxValues[cats[i]]--;
8990
}
9091
}
91-
if (allCatsValid) {
92-
if (id) {
93-
let imgName = id;
94-
let imgPath = cats[Math.floor(Math.random() * cats.length)];
95-
if (type === 'json') {
96-
return typejson(imgPath, imgName, request);
97-
}
98-
else if (!searchParams.has('type')) {
99-
return prescriptive(imgPath, imgName);
100-
}
101-
else {
102-
return error();
103-
}
92+
*/
93+
if (id) {
94+
let imgName = id;
95+
let imgPath = cats;
96+
if (type === 'json') {
97+
return typejson(imgPath, imgName, request);
98+
}
99+
else if (!searchParams.has('type')) {
100+
return prescriptive(imgPath, imgName);
101+
}
102+
else {
103+
return error();
104+
}
105+
}
106+
else if (!searchParams.has('id')) {
107+
let imgPath = cats[Math.floor(Math.random() * cats.length)];
108+
if (type === '302') {
109+
return redirect(imgPath, request);
104110
}
105-
else if (!searchParams.has('id')) {
106-
let imgPath = cats[Math.floor(Math.random() * cats.length)];
107-
if (type === '302') {
108-
return redirect(imgPath, request);
109-
}
110-
else if (type === 'json') {
111-
let max = maxValues[imgPath];
112-
let imgName = Math.floor(Math.random()*(max-min+1)+min);
113-
return typejson(imgPath, imgName, request);
114-
}
115-
else if (!searchParams.has('type')) {
116-
return random(imgPath);
117-
}
118-
else return error();
119-
}
111+
else if (type === 'json') {
112+
let max = maxValues[imgPath];
113+
let imgName = Math.floor(Math.random()*(max-min+1)+min);
114+
return typejson(imgPath, imgName, request);
115+
}
116+
else if (!searchParams.has('type')) {
117+
return random(imgPath);
118+
}
120119
else return error();
121-
}
122-
else return error();//不支持的分类
120+
}
121+
else return error();
123122
}
124123
else return error();
125124
}

方案3/image.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
"demoimg/2.jpg",
55
"1.jpg",
66
"2.jpg",
7-
"示例图/1.jpg",
8-
"示例图/2.jpg"
7+
"demoimg1/1.jpg",
8+
"demoimg1/2.jpg"
99
],
1010
"category2": [
1111
"demoimg/3.jpg",
1212
"demoimg/4.jpg",
1313
"demoimg/5.jpg",
14-
"示例图/3.jpg",
15-
"示例图/4.jpg",
16-
"示例图/5.jpg",
17-
"示例图/6.jpg",
18-
"示例图/7.jpg"
14+
"demoimg1/3.jpg",
15+
"demoimg1/4.jpg",
16+
"demoimg1/5.jpg",
17+
"demoimg1/6.jpg",
18+
"demoimg1/7.jpg"
1919
]
2020
}

方案4/image.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,26 @@
33
"mobile": [
44
"demoimg/1.jpg",
55
"1.jpg",
6-
"示例图/1.jpg"
6+
"demoimg1/1.jpg"
77
],
88
"pc": [
99
"demoimg/2.jpg",
1010
"2.jpg",
11-
"示例图/2.jpg"
11+
"demoimg1/2.jpg"
1212
]
1313
},
1414
"category2": {
1515
"mobile": [
1616
"demoimg/3.jpg",
1717
"demoimg/4.jpg",
1818
"demoimg/5.jpg",
19-
"示例图/3.jpg",
20-
"示例图/4.jpg"
19+
"demoimg1/3.jpg",
20+
"demoimg1/4.jpg"
2121
],
2222
"pc": [
23-
"示例图/5.jpg",
24-
"示例图/6.jpg",
25-
"示例图/7.jpg"
23+
"demoimg1/5.jpg",
24+
"demoimg1/6.jpg",
25+
"demoimg1/7.jpg"
2626
]
2727
}
2828
}

方案5/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ Cloudflare Worker首页:https://workers.cloudflare.com
5858

5959
- `jsonUrl`:存储图片信息的json文件地址
6060

61-
- `availableDevices`:除jpg外新添加的其他格式
61+
- `availableExtraForms`:除jpg外新添加的其他格式
62+
63+
- `availableDevices`:图片分辨率适合的设备分类
6264

6365
#### 可选
6466

0 commit comments

Comments
 (0)