forked from x-dr/short
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.js
144 lines (115 loc) · 4.44 KB
/
create.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* @api {post} /create Create
*/
// Path: functions/create.js
function generateRandomString(length) {
const characters = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
let result = '';
for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * characters.length);
result += characters.charAt(randomIndex);
}
return result;
}
export async function onRequest(context) {
if (context.request.method === 'OPTIONS') {
return new Response(null, {
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Max-Age': '86400', // 24小时
},
});
}
// export async function onRequestPost(context) {
const { request, env } = context;
const originurl = new URL(request.url);
const clientIP = request.headers.get("x-forwarded-for") || request.headers.get("clientIP");
const userAgent = request.headers.get("user-agent");
const origin = `${originurl.protocol}//${originurl.hostname}`
const options = {
timeZone: 'Asia/Shanghai',
year: 'numeric',
month: 'long',
day: 'numeric',
hour12: false,
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
};
const timedata = new Date();
const formattedDate = new Intl.DateTimeFormat('zh-CN', options).format(timedata);
const { url, slug } = await request.json();
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Max-Age': '86400', // 24 hours
};
if (!url) return Response.json({ message: 'Missing required parameter: url.' });
// url格式检查
if (!/^https?:\/\/.{3,}/.test(url)) {
return Response.json({ message: 'Illegal format: url.' },{
headers: corsHeaders,
status: 400
})
}
// 自定义slug长度检查 2<slug<10 是否不以文件后缀结尾
if (slug && (slug.length < 2 || slug.length > 10 || /.+\.[a-zA-Z]+$/.test(slug))) {
return Response.json({ message: 'Illegal length: slug, (>= 2 && <= 10), or not ending with a file extension.' },{
headers: corsHeaders,
status: 400
});
}
try {
// 如果自定义slug
if (slug) {
const existUrl = await env.DB.prepare(`SELECT url as existUrl FROM links where slug = '${slug}'`).first()
// url & slug 是一样的。
if (existUrl && existUrl.existUrl === url) {
return Response.json({ slug, link: `${origin}/${slug2}` },{
headers: corsHeaders,
status: 200
})
}
// slug 已存在
if (existUrl) {
return Response.json({ message: 'Slug already exists.' },{
headers: corsHeaders,
status: 200
})
}
}
// 目标 url 已存在
const existSlug = await env.DB.prepare(`SELECT slug as existSlug FROM links where url = '${url}'`).first()
// url 存在且没有自定义 slug
if (existSlug && !slug) {
return Response.json({ slug: existSlug.existSlug, link: `${origin}/${existSlug.existSlug}` },{
headers: corsHeaders,
status: 200
})
}
const bodyUrl = new URL(url);
if (bodyUrl.hostname === originurl.hostname) {
return Response.json({ message: 'You cannot shorten a link to the same domain.' }, {
headers: corsHeaders,
status: 400
})
}
// 生成随机slug
const slug2 = slug ? slug : generateRandomString(4);
// console.log('slug', slug2);
const info = await env.DB.prepare(`INSERT INTO links (url, slug, ip, status, ua, create_time)
VALUES ('${url}', '${slug2}', '${clientIP}',1, '${userAgent}', '${formattedDate}')`).run()
return Response.json({ slug: slug2, link: `${origin}/${slug2}` },{
headers: corsHeaders,
status: 200
})
} catch (e) {
// console.log(e);
return Response.json({ message: e.message },{
headers: corsHeaders,
status: 500
})
}
}