-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
46 lines (44 loc) · 1.37 KB
/
index.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
export default {
async fetch(request, env) {
if (request.method == 'GET') {
try {
const code = request.url.split('/').pop();
if (code){
const value = await env.file.get(code)
if (value == null) {
return new Response("Value not found", {status: 404})
}
return new Response(value)
}else{
return new Response('Usage: curl -k -F "file=@文件" ' + request.url.slice(0, -1))
}
}catch (e){
return new Response(e.message, {status: 500})
}
}
if (request.method == 'POST') {
try {
const formData = await request.formData()
const file = formData.get('file')
if (file){
const fileName = getName()
await env.file.put(fileName, file.stream(), {expirationTtl: 300})
return new Response('"link":"' + request.url + fileName + '"' )
}else{
return new Response('Usage: curl -k -F "file=@文件" ' + request.url.slice(0, -1))
}
}catch (e){
return new Response(e.message, {status: 500})
}
}
}
}
// 生成一个唯一的文件名
function getName() {
let randomString = '';
const alphabet = 'abcdefghijklmnopqrstuvwxyz'
for (let i = 0; i < 5; i++) {
randomString += alphabet.charAt(Math.floor(Math.random() * alphabet.length))
}
return randomString + Date.now()
}