From 671a114060ceaa7922affa00f90857bf99317d0a Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Fri, 4 Sep 2026 19:03:03 -0700 Subject: flash: create printer dir and return errors instead of unhandled 500 The web flasher config endpoint wrote ./printer/config.txt without creating the directory, so on a fresh deploy it threw ENOENT and the JSON error body was flashed to devices as the LittleFS partition. Create the directory, validate inputs, and route errors through bail. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_014gVYGi29FjTeLdNTiutkZx --- routes/users.ts | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/routes/users.ts b/routes/users.ts index 438452b2..a6b75342 100644 --- a/routes/users.ts +++ b/routes/users.ts @@ -1,5 +1,5 @@ import { createReadStream } from "node:fs"; -import { unlink, writeFile } from "node:fs/promises"; +import { mkdir, unlink, writeFile } from "node:fs/promises"; import config from "$config"; import { requirePin } from "$lib/auth"; import { db, g, ga, gf, s } from "$lib/db"; @@ -1077,12 +1077,18 @@ export default { }, 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")); + try { + const { ssid, key, token } = req.body; + if (!ssid || !key || !token) fail("ssid, key and token required"); + const cfg = `${ssid.trim()}\n${key.trim()}\n${token.trim()}\n`; + await mkdir("./printer", { recursive: true }); + 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")); + } catch (e) { + bail(res, e.message); + } }, async app(req, res) { -- cgit v1.2.3