From 66198815e79fbf38075e3b02dfdee31001e62fea Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Thu, 11 Sep 2025 16:00:46 -0700 Subject: flash route --- index.ts | 4 ++-- routes/users.ts | 39 ++++++++++++--------------------------- 2 files changed, 14 insertions(+), 29 deletions(-) diff --git a/index.ts b/index.ts index 02a6e0b5..0ca35ec5 100644 --- a/index.ts +++ b/index.ts @@ -141,7 +141,7 @@ app.post( }, users.login, ); -app.post("/printer", users.printer); +app.post("/flash", users.flash); app.get("/challenge", users.challenge); app.post("/nostrAuth", users.nostrAuth); app.get("/app/:pubkey", auth, users.app); @@ -194,7 +194,7 @@ app.post("/echo", (req, res) => { }); const host: string = process.env["HOST"] || "0.0.0.0"; -const port: number = parseInt(process.env["PORT"]) || 3119; +const port: number = Number.parseInt(process.env["PORT"]) || 3119; app.listen({ host, port }); diff --git a/routes/users.ts b/routes/users.ts index 1338c50c..93311aff 100644 --- a/routes/users.ts +++ b/routes/users.ts @@ -1,5 +1,5 @@ import { createReadStream } from "node:fs"; -import { appendFile, unlink } from "node:fs/promises"; +import { appendFile, unlink, writeFile } from "node:fs/promises"; import config from "$config"; import { requirePin } from "$lib/auth"; import { db, g, ga, s } from "$lib/db"; @@ -289,7 +289,7 @@ export default { } user.fresh = false; - user.tip = Math.max(0, Math.min(1000, parseInt(user.tip))); + user.tip = Math.max(0, Math.min(1000, Number.parseInt(user.tip))); if (password && password === confirm) { user.password = await Bun.password.hash(password, { @@ -866,28 +866,13 @@ export default { } }, - async printer(req, res) { - const { ssid, key, username, password } = req.body; - const dir = "./printer"; - const path = `${dir}/config.txt`; - - try { - await unlink(path); - await appendFile(path, `${ssid}\n`, "utf8"); - await appendFile(path, `${key}\n`, "utf8"); - await appendFile(path, `${username}\n`, "utf8"); - await appendFile(path, `${password}\n`, "utf8"); - - const output = "./littlefs.img"; - await $`./mklittlefs -c ${dir} -p 256 -b 4096 -s 0x20000 ${output}`; - - res.header("Content-Type", "application/octet-stream"); - res.header("Content-Disposition", "attachment; filename=littlefs.img"); - console.log("sending back"); - return res.send(createReadStream(output)); - } catch (error) { - res.code(500).send({ error: "Failed to generate LittleFS image" }); - } + async flash(req, res) { + const { ssid, key, token } = req.body; + const cfg = `${ssid.trim()}\n${key.trim()}\n${token.trim()}\n`; + await writeFile("./printer/config.txt", cfg, "utf8"); + await $`./mklittlefs -c ./printer -p 256 -b 4096 -s 0x20000 ./littlefs.img`; + res.header("Content-Type", "application/octet-stream"); + return res.send(createReadStream("./littlefs.img")); }, async app(req, res) { @@ -934,9 +919,9 @@ export default { a.spent = payments.reduce( (a, b) => a + - (Math.abs(parseInt(b.amount || 0)) + - parseInt(b.fee || 0) + - parseInt(b.ourfee || 0)), + (Math.abs(Number.parseInt(b.amount || 0)) + + Number.parseInt(b.fee || 0) + + Number.parseInt(b.ourfee || 0)), 0, ); }), -- cgit v1.2.3