muse/book-reading-clip-lamp

A Wi‑Fi/USB‑C ESP32‑S3 reading lamp with OV5640 camera, microphone, speaker amplifier, rechargeable Li‑Po power management, six-LED boost light, USB protection/charging detection, and a clip/gooseneck enclosure with external FlexPIFA antenna.

Version
0.4.3
License
unset
Stars
0

serve-enclosure.mjs

import { resolve, sep } from "node:path"
const root = resolve(import.meta.dir)
const server = Bun.serve({ hostname: "127.0.0.1", port: 3021, async fetch(req) {
  const url = new URL(req.url)
  if (req.method === "POST" && url.pathname === "/save-preview") {
    const { image, view, mode } = await req.json()
    if (!["front", "rear", "side"].includes(view) || !["assembled", "inside", "exploded"].includes(mode) ||
        typeof image !== "string" || !image.startsWith("data:image/png;base64,")) return new Response("Invalid view", { status: 400 })
    const name = `mechanical/preview-${view}-${mode}.png`
    await Bun.write(resolve(root, name), Buffer.from(image.split(",")[1], "base64"))
    return Response.json({ file: name })
  }
  const relative = decodeURIComponent(url.pathname === "/" ? "/enclosure-viewer.html" : url.pathname)
  const path = resolve(root, "." + relative)
  if (!path.startsWith(root + sep)) return new Response("Not found", { status: 404 })
  const file = Bun.file(path)
  return await file.exists() ? new Response(file) : new Response("Not found", { status: 404 })
}})
console.log(`Enclosure review: ${server.url}`)