Skip to content

Commit 9db513c

Browse files
author
YieldRay
committed
hide node.js
1 parent 1ea0613 commit 9db513c

10 files changed

Lines changed: 84 additions & 93 deletions

File tree

.prettierrc.yaml

Lines changed: 0 additions & 9 deletions
This file was deleted.

README.md

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
# Random-Picture
22

33
通过随机发送`url.csv`文件中给出的图床链接来实现一个随机图片 API
4-
本仓库含 php(vercel), node.js(vercel), deno(deno.dev) 版本,API 一致
4+
本仓库含 php(vercel), deno(deno.dev) 版本,API 一致
5+
另外给出了 node.js 实现,仅供测试
56

67
## 演示
78

8-
- <https://random-picture.vercel.app/> _(vercel 演示)_
9-
- <https://random-picture.vercel.app/api/> _(vercel, php 版本)_
9+
- <https://random-picture.vercel.app/> _(vercel, php 版本)_
1010
- <https://random-picture.vercel.app/random.jpg>
11+
- <https://random-picture.vercel.app/api/>
1112
- <https://random-picture.vercel.app/api/?json>
12-
- <https://random-picture.vercel.app/api/node/> _(vercel, nodejs 版本)_
13-
- <https://random-picture.vercel.app/api/node/?json>
1413

1514
- <https://rand.deno.dev/> (deno 版本)
1615
- <https://rand.deno.dev/?json>
@@ -28,17 +27,6 @@ fork 后,修改自己仓库的 `url.csv`,然后在 Vercel 平台上导入自
2827
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/import/git?s=https%3A%2F%2Fgithub.com%2FYieldRay%2FRandom-Picture)
2928
php 版本也可以直接上传到虚拟主机
3029

31-
## node.js 部署到 Vercel
32-
33-
![env.png](https://s2.loli.net/2022/03/25/ocECsXr2v1aKShG.png)
34-
部署到 vercel 后设置环境变量 `RECORD_URL` 为图片地址,或直接修改源码给定位置
35-
如果你已经 frok 了本项目,环境变量填入`https://raw.githubusercontents.com/YieldRay/Random-Picture/master/url.csv`即可
36-
**注意将用户名换为你自己的 Github 用户名**
37-
当然你也可以自行托管此文本,但是每次修改需要重新部署才能获取到更新后的地址
38-
如果出错请切换到 node.js >= 14
39-
40-
nodejs 及 deno 版本每次部署时将图床地址读入内存,因此 API 不宜托管过多图床链接
41-
4230
## deno 部署到 deno.dev
4331

4432
Deno 版本需要你能够托管一个文本文件,并获取这个文本文件的网址。下面托管在 Github

api/index.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
const ALLOW_RAW_OUTPUT = true;
3+
const ALLOW_RAW_OUTPUT = false;
44
// 是否开启 ?raw 选项,可能会消耗服务器较多流量
55

66
function has_query($query)
@@ -16,12 +16,9 @@ function has_query($query)
1616
$imgs_array = ['https://http.cat/503'];
1717
}
1818

19-
20-
$id = 0;
21-
if (is_numeric($_GET['id'])) {
19+
$id = $_GET['id'] ?? "";
20+
if (strlen($id) > 0 && is_numeric($id)) {
2221
header('Cache-Control: public, max-age=86400');
23-
global $id;
24-
$id = $_GET['id'];
2522
settype($id, 'int');
2623
$len = count($imgs_array);
2724
if ($id >= $len || $id < 0) {
@@ -37,18 +34,15 @@ function has_query($query)
3734
header('Access-Control-Allow-Origin: *');
3835
header('Content-Type: application/json');
3936
echo json_encode(['id' => $id, 'url' => $imgs_array[$id]]);
40-
exit();
41-
}
42-
43-
44-
if (has_query('raw')) {
37+
} else if (has_query('raw')) {
4538
if (!ALLOW_RAW_OUTPUT) {
4639
header('HTTP/1.1 403 Forbidden');
4740
exit();
4841
}
4942
header('Content-Type: image/png');
5043
echo file_get_contents($imgs_array[$id]);
51-
exit();
5244
} else {
5345
header('Location: ' . $imgs_array[$id]);
5446
}
47+
48+
exit();

index.html

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
"Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", "sans-serif";
1515
font-size: 16px;
1616
}
17+
1718
* {
1819
box-sizing: border-box;
1920
}
21+
2022
header {
2123
padding: 40vh 0 8vh 0;
2224
text-align: center;
@@ -29,24 +31,29 @@
2931
overflow: hidden;
3032
background: #a0e5e4;
3133
}
34+
3235
h1,
3336
h2,
3437
h3 {
3538
font-weight: 400 !important;
3639
font-weight: bold;
3740
margin: 0;
3841
}
42+
3943
h1 {
4044
font-size: 3.5rem;
4145
}
46+
4247
h2 {
4348
font-size: 2rem;
4449
font-weight: 300;
4550
}
51+
4652
h3 {
4753
font-size: 1.5rem;
4854
font-weight: 300;
4955
}
56+
5057
#scroll-down {
5158
background-color: transparent;
5259
margin-top: 2rem;
@@ -57,6 +64,7 @@
5764
cursor: pointer;
5865
animation: bounce 2s infinite 2s;
5966
}
67+
6068
#scroll-down::before {
6169
display: block;
6270
content: "";
@@ -66,24 +74,34 @@
6674
border: 2px solid #424242;
6775
border-width: 0px 0 2px 2px;
6876
}
77+
6978
a {
7079
text-decoration: none;
7180
color: #79c37c;
7281
}
82+
7383
main {
7484
padding: 1vw;
7585
}
86+
7687
section {
7788
padding: 1rem;
7889
}
90+
7991
footer {
8092
padding: 1vh;
8193
text-align: center;
8294
background-color: #aaaaaa;
8395
}
96+
8497
footer a {
8598
color: #ddd;
8699
}
100+
101+
footer a:hover {
102+
text-decoration: underline;
103+
}
104+
87105
@keyframes bounce {
88106
0%,
89107
100%,
@@ -92,16 +110,19 @@
92110
80% {
93111
transform: translateY(0);
94112
}
113+
95114
40% {
96115
transform: translateY(-10px);
97116
}
117+
98118
60% {
99119
transform: translateY(-5px);
100120
}
101121
}
122+
102123
pre {
103124
background-color: #f5f5f5;
104-
padding: 1rem;
125+
padding: 2rem 1rem;
105126
border-radius: 4px;
106127
overflow: auto;
107128
}
@@ -119,7 +140,7 @@ <h3>说明文档</h3>
119140
<main>
120141
<section>
121142
<h2><strong>假定 API 部署到 https://example.net/</strong></h2>
122-
<small>将 /api/ 替换为 /api/node/ 使用 node.js 版本</small>
143+
<small>php</small>
123144
<p>
124145
访问
125146
<a href="https://example.net/api">https://example.net/api</a>
@@ -146,7 +167,7 @@ <h2>
146167
<a href="https://example.net/api/?raw">https://example.net/api/?raw</a>
147168
</strong>
148169
</h2>
149-
<p>服务器读取后输出</p>
170+
<p>服务器读取后输出,默认关闭</p>
150171
</section>
151172
<section>
152173
<h2>
@@ -156,18 +177,17 @@ <h2>
156177
</strong>
157178
</h2>
158179
<p>输出json</p>
159-
<pre class="language-json">
160-
<code class="language-json">
180+
<pre>
161181
{"id":3,url":"https://z3.ax1x.com/2021/08/19/fqDucQ.png"}
162-
</code>
163-
</pre>
182+
</pre
183+
>
164184
</section>
165185
</main>
166186
<script>
167187
document.querySelector("main").innerHTML = document
168188
.querySelector("main")
169189
.innerHTML.replace(/https:\/\/example.net/g, location.origin);
170-
// 替换example.net
190+
171191
document.getElementById("scroll-down").addEventListener("click", function () {
172192
document.querySelector("main").scrollIntoView({
173193
behavior: "smooth",

package.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

test/deno.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { serve } from 'https://deno.land/std@0.130.0/http/server.ts';
1+
import { serve } from "https://deno.land/std@0.133.0/http/server.ts";
22

33
// 修改下面的地址即可,这个地址应该返回一个文本文件,每行一个图片地址
4-
const recordURL = 'https://raw.githubusercontents.com/YieldRay/Random-Picture/master/url.csv';
4+
const recordURL = "https://raw.githubusercontents.com/YieldRay/Random-Picture/master/url.csv";
55
/**
66
* 有?json则返回json,否则如有?raw直接输出图像否则302跳转
77
* 优先获取123.jpg中的id,其次?id
@@ -14,14 +14,14 @@ const recordURL = 'https://raw.githubusercontents.com/YieldRay/Random-Picture/ma
1414
*/
1515
const randomNum = (min: number, max: number) => Math.floor(Math.random() * (max - min + 1)) + min;
1616
const imagesArray = await fetch(recordURL)
17-
.then(res => res.text())
18-
.then(text => text.split(/\r|\n|\r\n/).filter(url => url.length > 5));
17+
.then((res) => res.text())
18+
.then((text) => text.split(/\r|\n|\r\n/).filter((url) => url.length > 5));
1919

2020
async function handler(req: Request): Promise<Response> {
2121
const url = new URL(req.url);
22-
if (url.pathname === '/favicon.ico') {
22+
if (url.pathname === "/favicon.ico") {
2323
// return new Response(null, { status: 404 });
24-
return await fetch('https://deno.land/favicon.ico');
24+
return await fetch("https://deno.land/favicon.ico");
2525
}
2626
const searchParams = new URLSearchParams(url.search);
2727
try {
@@ -30,38 +30,40 @@ async function handler(req: Request): Promise<Response> {
3030
if (matched) {
3131
stringNumber = matched[1];
3232
} else {
33-
stringNumber = searchParams.get('id') ?? '';
33+
stringNumber = searchParams.get("id") ?? "";
3434
}
3535
let id = Number(stringNumber);
3636
if (stringNumber.length === 0 || Number.isNaN(id)) {
3737
id = randomNum(0, imagesArray.length - 1);
3838
} else {
39-
if (id < 0 || id >= imagesArray.length) id = randomNum(0, imagesArray.length - 1);
39+
if (id < 0 || id >= imagesArray.length) {
40+
id = randomNum(0, imagesArray.length - 1);
41+
}
4042
}
4143
const remoteURL = imagesArray[id];
4244
console.log(`send ${id} of ${imagesArray.length} with ${req.url}`);
4345
// 调整发送格式json/raw/302
44-
if (searchParams.has('json')) {
46+
if (searchParams.has("json")) {
4547
return new Response(JSON.stringify({ id, url: remoteURL }), {
4648
headers: {
47-
'access-control-allow-origin': '*',
48-
'content-type': 'application/json; charset=utf-8',
49-
'cache-control': 'no-cache',
49+
"access-control-allow-origin": "*",
50+
"content-type": "application/json; charset=utf-8",
51+
"cache-control": "no-cache",
5052
},
5153
});
52-
} else if (searchParams.has('raw')) {
54+
} else if (searchParams.has("raw")) {
5355
return await fetch(remoteURL, {
5456
headers: {
55-
Referer: 'https://www.pixiv.net/',
56-
'User-Agent': 'PixivIOSApp/6.7.1 (iOS 10.3.1; iPhone8,1)',
57+
Referer: "https://www.pixiv.net/",
58+
"User-Agent": "PixivIOSApp/6.7.1 (iOS 10.3.1; iPhone8,1)",
5759
}, // 这个Header允许调用pixiv上面的图片
5860
});
5961
} else {
6062
return new Response(null, {
6163
status: 302,
6264
headers: {
6365
location: remoteURL,
64-
'cache-control': 'no-cache',
66+
"cache-control": "no-cache",
6567
},
6668
});
6769
}

test/develop.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// 本地测试
2-
import http from 'http';
3-
import handler from './node.mjs';
2+
import http from "http";
3+
import handler from "./node.mjs";
44
const port = process.env.PORT || 8000;
55
console.log(`Listening on ${port}`);
66
http.createServer(handler).listen(port);

test/mini.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php $a=file('url.csv')[array_rand(file('url.csv'))];header("Location:$a");
1+
<?php $a=file('url.csv');$b=$a[array_rand($a)];header("Location:$b");

0 commit comments

Comments
 (0)