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
/** @jsxImportSource https://esm.sh/hono@4.0.8/jsx **/
import { blob } from "https://esm.town/v/std/blob?v=11";
import { Hono } from "npm:hono@4.0.8";
import { HTTPException } from "npm:hono@4.0.8/http-exception";
const route = new Hono();
route.get("/", (c) => {
return c.render(
<form method="POST" enctype="multipart/form-data">
<input name="file" type="file" />
<input type="submit" value="Upload" />
</form>,
);
});
route.post("/", async (c) => {
const { file } = await c.req.parseBody();
if (!(file instanceof File)) {
throw new HTTPException(400, { message: file });
}
// check if blob already exists
const key = file.name;
try {
await blob.get(key);
} catch (_) {
// if blob does not exists, create it
await blob.set(key, await file.arrayBuffer());
return c.redirect("/");
}
throw new HTTPException(400, { message: `key ${key} already exists` });
});
export default route;
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
April 24, 2024