-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
149 lines (122 loc) · 3.69 KB
/
main.ts
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
142
143
144
145
146
147
148
149
import { Hono } from "https://deno.land/x/[email protected]/mod.ts";
import { qrcode } from "https://deno.land/x/[email protected]/mod.ts";
import {
StatusCodes,
} from "https://deno.land/x/[email protected]/mod.ts";
import {
S3Client,
PutObjectCommand,
GetObjectCommand,
} from "npm:@aws-sdk/client-s3";
import { getSignedUrl } from "npm:@aws-sdk/s3-request-presigner";
import {
PORT,
AWS_REGION,
AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY,
AWS_S3_BUCKET_NAME,
APP_URL,
} from "./config.ts";
import { expireMap } from "./utils.ts";
import { STORE_KEY } from "./db.ts";
import { kv } from "./db.ts";
import type { Item } from "./types.ts";
const app = new Hono();
app.get("/", (c) => c.html(Deno.readTextFile("index.html")));
app.get("/download/:id", async (c) => {
const id = c.req.param("id");
const item = await kv.get<Item>([STORE_KEY, id]);
if (!item.value) {
return c.status(404);
}
if (item.value.expire < new Date().getTime()) {
c.status(410);
return c.text("Gone");
}
// const base64Image = await qrcode(String(item.value.url));
const base64Image = await qrcode(`${APP_URL}/f/${id}`);
const downloadPageTmpl = await Deno.readTextFile("download.html");
// match {{ key }} in the template
const templateRE = /\{\{\s([a-zA-Z_$][0-9a-zA-Z_$]*)\s\}\}/g;
const tmplVars: Record<string, string> = {
qrcode: String(base64Image),
filename: item.value.name,
url: item.value.url,
expire: item.value.expire.toString(),
};
const downloadPageStr = downloadPageTmpl.replace(
templateRE,
(_match, key) => {
return tmplVars[key];
}
);
return c.html(downloadPageStr);
});
app.get("/f/:id", async (c) => {
const id = c.req.param("id");
const item = await kv.get<Item>([STORE_KEY, id]);
if (!item.value) {
return c.status(404);
}
if (item.value.expire < new Date().getTime()) {
c.status(410);
return c.text("Gone");
}
return c.redirect(item.value.url);
});
app.post("/upload", async (c) => {
const form = await c.req.formData();
for (const [_key, value] of form.entries()) {
if (!value) {
c.status(422);
return c.text("UnprocessableEntity");
}
}
const expireKey = form.get("expire") as string;
const file = form.get("file") as File;
if (file.size > 10 * 1024 * 1024) {
c.status(StatusCodes.BAD_REQUEST);
return c.text("File size too large! (max 10MB)");
}
const min = 100_000; // Minimum 6-digit number
const max = 999_999; // Maximum 6-digit number
const id = Math.floor(Math.random() * (max - min + 1)) + min;
const created = new Date().getTime();
const expire = created + expireMap[expireKey];
// upload to bucket
const s3Client = new S3Client({
region: AWS_REGION,
credentials: {
accessKeyId: AWS_ACCESS_KEY_ID,
secretAccessKey: AWS_SECRET_ACCESS_KEY,
},
});
const putObjectCommand = new PutObjectCommand({
Bucket: AWS_S3_BUCKET_NAME,
Key: id.toString(),
Body: await file.arrayBuffer(),
ContentType: file.type,
});
const getObjectCommand = new GetObjectCommand({
Bucket: AWS_S3_BUCKET_NAME,
Key: id.toString(),
});
const s3PutResult = await s3Client.send(putObjectCommand);
console.log("uploaded: ", id, s3PutResult.$metadata.requestId);
// generate s3 presigned url
const presignedUrl = await getSignedUrl(s3Client, getObjectCommand, {
expiresIn: expireMap[expireKey] / 1_000,
});
console.log("presignedUrl: ", presignedUrl);
await kv.set([STORE_KEY, id.toString()], {
id,
name: file.name,
size: file.size,
url: presignedUrl,
created,
expire,
});
console.log("saved to kv: ", STORE_KEY, id);
return c.redirect(`/download/${id.toString()}`);
});
Deno.serve({ port: PORT }, app.fetch);