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 });
}
const key = file.name;
try {
await blob.get(key);
} catch (_) {
await blob.set(key, await file.arrayBuffer());
return c.redirect("/");
}
throw new HTTPException(400, { message: `key ${key} already exists` });
});
export default route;