-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
45 lines (35 loc) · 1.25 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
import "std/dotenv/load.ts";
const s3Params = {
endPoint: Deno.env.get("B2_ENDPOINT") || "",
region: Deno.env.get("B2_REGION") || "",
accessKey: Deno.env.get("B2_KEY"),
secretKey: Deno.env.get("B2_SECRET"),
bucket: Deno.env.get("B2_BUCKET"),
pathStyle: false
};
async function handleRequest(req: Request): Promise<Response> {
const auth = req.headers.get("Authorization");
if (auth === null || !auth.startsWith("Bearer")) {
return new Response("Not authorized", { status: 403 });
}
const token = auth.slice(7);
if (token !== Deno.env.get("TOKEN")) {
return new Response("Not authorized", { status: 403 });
}
if (req.body === null) {
return new Response("Empty body", { status: 400 });
}
const url = new URL(req.url);
const regex = /\/([^\/]+)\/([^\/]+)\/([^\/]+)/
const found = url.pathname.match(regex);
if (found === null) {
return new Response("Invalid path", { status: 400 });
}
const worker = new Worker(new URL("./worker.ts", import.meta.url).href, { type: "module" });
const [, name, mcVersion, libVersion] = found;
const prefix = `${name}/${mcVersion}/${libVersion}`;
const body = new Uint8Array(await req.arrayBuffer());
worker.postMessage({ body, prefix, s3Params });
return new Response("Uploading");
}
Deno.serve(handleRequest);