Skip to content

Commit 818cb0f

Browse files
author
Crazy-White
committed
add error handler
1 parent efd8b02 commit 818cb0f

7 files changed

Lines changed: 381 additions & 329 deletions

File tree

.prettierrc.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
trailingComma: 'all'
2+
tabWidth: 4
3+
semi: true
4+
singleQuote: true
5+
arrowParens: 'avoid'
6+
printWidth: 140
7+
bracketSpacing: true
8+
endOfLine: 'crlf'
9+
htmlWhitespaceSensitivity: 'ignore'

README.md

Lines changed: 72 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,72 @@
1-
# Random-Picture
2-
3-
随机图片 api
4-
5-
## 实验地址:
6-
7-
[http://miku.ueuo.com/](http://miku.ueuo.com/)_(无稳定性保证)_
8-
[https://random-picture.vercel.app/](https://random-picture.vercel.app/)_(部署到 vercel)_
9-
10-
### 注意:
11-
12-
| 路径 | 说明 |
13-
| --------------- | -------------------------------------- |
14-
| ./url.csv | 务必一行一个 url,不要输入不完整的 url |
15-
| ./api/index.php | 修改 ALLOW_OUTPUT 以开启服务器输出 |
16-
17-
## 部署到 Vercel
18-
19-
建议 fork 后,自行修改配置,然后在 Vercel 平台上导入自己的项目
20-
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/import/git?s=https%3A%2F%2Fgithub.com%2FCrazy-White%2FRandom-Picture)
21-
22-
## 关于 Rewrite
23-
24-
伪静态是可选的。
25-
开启伪静态后,将支持以<https://example.net/:id.png>形式访问
26-
**:id** 即图片 id,如果非数字,则随机跳转。请求会被缓存。
27-
Apache 和 Vercel 部署都直接支持伪静态。
28-
Nginx 参照以下配置:
29-
30-
```nginx
31-
location / {
32-
if (!-e $request_filename) {
33-
rewrite ^(\w)*\.(?:jpg|jpeg|png|gif|bmp|webp)$ /api/index.php?id=$1 last;
34-
}
35-
}
36-
```
37-
38-
## Doc
39-
40-
<section>
41-
<h3><strong>直接调用</strong></h3>
42-
<p>访问./api即可,跳转至图片地址</p>
43-
<p>访问./api/?id=数字,跳转至指定图片</p>
44-
<p>注:api附加参数 api=数字 亦可跳转至指定图片</p>
45-
<p>如:<a href="./api/?type=json&id=3">./api/?type=json&id=3</a></p>
46-
</section>
47-
<section>
48-
<h3><strong>访问<a href="./api/?type=length">./api/?type=length</a></strong></h3>
49-
<p>返回图片总量,即id的最大值</p>
50-
</section>
51-
<section>
52-
<h3><strong>访问<a href="./api/?type=output">./api/?type=output</a></strong></h3>
53-
<p>服务器读取后输出,一般不建议使用,默认禁用</p>
54-
</section>
55-
<section>
56-
<h3><strong>访问<a href="./api/?type=json">./api/?type=json</a></strong></h3>
57-
<p>服务器输出json</p>
58-
<pre class="language-json">
59-
<code class="language-json">
60-
{&quot;code&quot;:&quot;200&quot;,&quot;url&quot;:&quot;https:\/\/fp1.fghrsh.net\/2019\/07\/15\/c2549aaa63db078834ead6a92fe63b61.jpg&quot;}
61-
</code>
62-
</pre>
63-
</section>
64-
<section>
65-
<h3><strong>访问<a href="./api/?type=JSON">./api/?type=JSON</a></strong></h3>
66-
<p>服务器读取图片信息后输出json,如非需要图片信息不建议使用</p>
67-
<pre class="language-json">
68-
<code class="language-json">
69-
{&quot;code&quot;:&quot;200&quot;,&quot;url&quot;:&quot;https:\/\/fp1.fghrsh.net\/2019\/07\/15\/c2549aaa63db078834ead6a92fe63b61.jpg&quot;,&quot;width&quot;:&quot;1920&quot;,&quot;height&quot;:&quot;1080&quot;,&quot;mime&quot;:&quot;image\/jpeg&quot;,&quot;size&quot;:&quot;821735&quot;}
70-
</code>
71-
</pre>
72-
</section>
1+
# Random-Picture
2+
3+
随机图片 api
4+
5+
## 实验地址:
6+
7+
<https://illyasviel.feedia.co/> _(无稳定性保证)_
8+
<https://random-picture.vercel.app/> _(部署到 vercel)_
9+
10+
### 注意:
11+
12+
| 路径 | 说明 |
13+
| --------------- | ------------------------------------------------------- |
14+
| ./url.csv | 务必一行一个 url,不要输入不完整的 url |
15+
| ./api/index.php | ALLOW_OUTPUT 是否允许服务器输出;ERROR_IMG 出错时的图片 |
16+
17+
## 部署到 Vercel
18+
19+
建议 fork 后,自行修改配置,然后在 Vercel 平台上导入自己的项目
20+
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/import/git?s=https%3A%2F%2Fgithub.com%2FCrazy-White%2FRandom-Picture)
21+
22+
## 关于 Rewrite
23+
24+
伪静态是可选的。
25+
开启伪静态后,将支持以<https://example.net/:id.png>形式访问
26+
**:id** 即图片 id,如果非数字,则随机跳转。请求会被缓存。
27+
Apache 和 Vercel 部署都直接支持伪静态。
28+
Nginx 参照以下配置:
29+
30+
```nginx
31+
location / {
32+
if (!-e $request_filename) {
33+
rewrite ^(\w)*\.(?:jpg|jpeg|png|gif|bmp|webp)$ /api/index.php?id=$1 last;
34+
}
35+
}
36+
```
37+
38+
## Doc
39+
40+
<section>
41+
<h3><strong>直接调用</strong></h3>
42+
<p>访问./api即可,跳转至图片地址</p>
43+
<p>访问./api/?id=数字,跳转至指定图片</p>
44+
<p>注:api附加参数 api=数字 亦可跳转至指定图片</p>
45+
<p>如:<a href="./api/?type=json&id=3">./api/?type=json&id=3</a></p>
46+
</section>
47+
<section>
48+
<h3><strong>访问<a href="./api/?type=length">./api/?type=length</a></strong></h3>
49+
<p>返回图片总量,即id的最大值</p>
50+
</section>
51+
<section>
52+
<h3><strong>访问<a href="./api/?type=output">./api/?type=output</a></strong></h3>
53+
<p>服务器读取后输出,一般不建议使用,默认禁用</p>
54+
</section>
55+
<section>
56+
<h3><strong>访问<a href="./api/?type=json">./api/?type=json</a></strong></h3>
57+
<p>服务器输出json</p>
58+
<pre class="language-json">
59+
<code class="language-json">
60+
{&quot;code&quot;:&quot;200&quot;,&quot;url&quot;:&quot;https:\/\/fp1.fghrsh.net\/2019\/07\/15\/c2549aaa63db078834ead6a92fe63b61.jpg&quot;}
61+
</code>
62+
</pre>
63+
</section>
64+
<section>
65+
<h3><strong>访问<a href="./api/?type=JSON">./api/?type=JSON</a></strong></h3>
66+
<p>服务器读取图片信息后输出json,如非需要图片信息不建议使用</p>
67+
<pre class="language-json">
68+
<code class="language-json">
69+
{&quot;code&quot;:&quot;200&quot;,&quot;url&quot;:&quot;https:\/\/fp1.fghrsh.net\/2019\/07\/15\/c2549aaa63db078834ead6a92fe63b61.jpg&quot;,&quot;width&quot;:&quot;1920&quot;,&quot;height&quot;:&quot;1080&quot;,&quot;mime&quot;:&quot;image\/jpeg&quot;,&quot;size&quot;:&quot;821735&quot;}
70+
</code>
71+
</pre>
72+
</section>

api/index.php

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,56 @@
11
<?php
22
const ALLOW_OUTPUT = false; // 修改以开启
3-
const NOT_FOUND_IMG = "https://i.loli.net/2020/08/17/J7ZU2VAHPQTbcy8.png";
3+
const ERROR_IMG = [
4+
404 => 'https://http.cat/404',
5+
503 => 'https://http.cat/503'
6+
];
47

58
if (file_exists('../url.csv')) {
69
$url = file('../url.csv', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
710
} else {
8-
$url = file('https://' . $_SERVER['HTTP_HOST'] . '/url.csv', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
11+
$url = file('http://' . $_SERVER['HTTP_HOST'] . '/url.csv', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
912
}
1013

14+
if (empty($url[0])) {
15+
$code = 503;
16+
$target_url = ERROR_IMG[503];
17+
}
18+
19+
1120
$id = $_REQUEST['id'];
1221
$type = $_REQUEST['type'];
1322
$length = count($url);
1423
$final_id = array_rand($url);
1524
$is_idValid = false;
1625

1726
if (isset($id)) {
18-
header("Cache-Control: public, max-age=86400");
27+
header('Cache-Control: public, max-age=86400');
1928
if (is_numeric($id)) {
20-
settype($id, "integer");
29+
settype($id, 'integer');
2130
if ($is_idValid = is_int($id)) {
2231
$final_id = $id; // id是整数
2332
}
2433
}
2534
} else {
26-
header("Cache-Control: no-cache");
35+
header('Cache-Control: no-cache');
2736
}
2837

2938

3039

31-
if ($is_idValid && $final_id > $length) {
40+
if (!$code && $is_idValid && $final_id > $length) {
3241
// exceed maximum length
3342
$code = 404;
34-
$target_url = NOT_FOUND_IMG;
43+
$target_url = ERROR_IMG[404];
3544
} else {
3645
$code = 200;
3746
$target_url = $url[$final_id];
3847
}
3948

4049
/**
41-
* 只使用以下变量
42-
* $code $target_url $length
50+
* Variables for OUTPUT
51+
* $code - mock http code
52+
* $target_url - the final url
53+
* $length - amount of the images
4354
*/
4455
header('Access-Control-Max-Age: 86400'); //1day
4556
header('Access-Control-Allow-Origin: *');
@@ -49,18 +60,18 @@
4960
echo $length;
5061
break;
5162
case 'json':
52-
$result = array(
53-
"code" => $code,
54-
"url" => $target_url
55-
);
63+
$result = [
64+
'code' => $code,
65+
'url' => $target_url
66+
];
5667
header('Content-Type: text/json');
5768
echo json_encode($result);
5869
break;
5970
case 'JSON':
60-
$result = array(
61-
"code" => $code,
62-
"url" => $target_url
63-
);
71+
$result = [
72+
'code' => $code,
73+
'url' => $target_url
74+
];
6475
$imageInfo = getimagesize($target_url);
6576
$imageSize = get_headers($target_url, 1)['Content-Length'];
6677
$result['width'] = $imageInfo[0];
@@ -80,5 +91,5 @@
8091
}
8192
break;
8293
default:
83-
header("Location: $target_url");
94+
header('Location: ' . $target_url);
8495
}

0 commit comments

Comments
 (0)