|
| 1 | +import { Resvg, initWasm } from "@resvg/resvg-wasm"; |
1 | 2 | import { Hono } from "hono"; |
2 | 3 | import { QR } from "qr-svg"; |
3 | 4 |
|
4 | 5 | import { createLogger } from "~/logging"; |
5 | 6 | import { isValidUUID } from "~/schema/shared/helpers"; |
6 | 7 |
|
| 8 | +// @ts-expect-error This import actually exists |
| 9 | +import resvgwasm from "./index_bg.wasm"; |
| 10 | + |
7 | 11 | const app = new Hono(); |
8 | 12 |
|
9 | 13 | app.get("/qr/raw/:id", (c) => { |
@@ -36,6 +40,48 @@ app.get("/qr/svg/:id", (c) => { |
36 | 40 | return c.text(svg); |
37 | 41 | }); |
38 | 42 |
|
| 43 | +app.get("/qr/png/:id", async (c) => { |
| 44 | + const logger = createLogger("qr-render-png"); |
| 45 | + const uuid = c.req.param("id").trim().toLowerCase(); |
| 46 | + |
| 47 | + if (!isValidUUID(uuid)) { |
| 48 | + logger.error("Invalid id"); |
| 49 | + throw new Error("Invalid id"); |
| 50 | + } |
| 51 | + |
| 52 | + try { |
| 53 | + await initWasm(resvgwasm as WebAssembly.Module); |
| 54 | + } catch (error) { |
| 55 | + logger.error("Resvg wasm not initialized"); |
| 56 | + } |
| 57 | + |
| 58 | + const svg = QR(uuid); |
| 59 | + |
| 60 | + const resvg = new Resvg(svg, { |
| 61 | + background: "white", |
| 62 | + fitTo: { |
| 63 | + mode: "width", |
| 64 | + value: 1200, |
| 65 | + }, |
| 66 | + font: { |
| 67 | + loadSystemFonts: false, |
| 68 | + }, |
| 69 | + }); |
| 70 | + |
| 71 | + const pngData = resvg.render(); |
| 72 | + const pngBuffer = pngData.asPng(); |
| 73 | + |
| 74 | + c.res.headers.set("Content-Type", "image/png"); |
| 75 | + |
| 76 | + return new Response(pngBuffer, { |
| 77 | + headers: { |
| 78 | + "Content-Type": "image/png", |
| 79 | + "Cache-Control": "public, immutable, no-transform, max-age=31536000", |
| 80 | + }, |
| 81 | + status: 200, |
| 82 | + }); |
| 83 | +}); |
| 84 | + |
39 | 85 | app.get("/", (c) => { |
40 | 86 | return c.json({ |
41 | 87 | message: "Greetings and salutations from the CommunityOS team", |
|
0 commit comments