feat(results): save results as PDF automatically on the server and ask to print when the finals are done (1.89.28)

This commit is contained in:
larssand committed 2026-11-14 15:58:23 +01:00
1 parent ae829123cc
commit e98abd92d9
16 files changed
+253 -6

No files matched your search

+40
View File
@@ -660,6 +660,46 @@ app.post("/api/passings/purge", (req, res) => {
res.json({ ok: true, deleted: result.changes });
});
// Results saved as PDF on their own (autopilot print): data/results/<date>/<file>.pdf. The race
// browser sends the same payload as Export PDF; a newer version of a heat overwrites the old one.
const RESULTS_PDF_DIR = path.join(DB_DIR, "results");
const safeResultName = (value, fallback) => String(value || "").normalize("NFKD").replace(/[̀-ͯ]/g, "").replace(/[^A-Za-z0-9._-]+/g, "_").replace(/_{2,}/g, "_").replace(/^[._]+/, "").slice(0, 120) || fallback;
app.post("/api/results/autosave", (req, res) => {
try {
const payload = normalizePdfExportPayload(req.body || {});
const date = /^\d{4}-\d{2}-\d{2}$/.test(String(req.body?.date || "")) ? req.body.date : new Date().toISOString().slice(0, 10);
const name = safeResultName(payload.filename.replace(/\.pdf$/i, ""), "results") + ".pdf";
const dir = path.join(RESULTS_PDF_DIR, date);
fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(path.join(dir, name), buildStyledPdf(payload));
res.json({ ok: true, date, name });
} catch (error) {
res.status(400).json({ error: error instanceof Error ? error.message : String(error) });
}
});
app.get("/api/results/saved", (_req, res) => {
const list = [];
try {
fs.readdirSync(RESULTS_PDF_DIR).filter((date) => /^\d{4}-\d{2}-\d{2}$/.test(date)).forEach((date) => {
fs.readdirSync(path.join(RESULTS_PDF_DIR, date)).filter((name) => name.toLowerCase().endsWith(".pdf")).forEach((name) => {
const stat = fs.statSync(path.join(RESULTS_PDF_DIR, date, name));
list.push({ date, name, size: stat.size, savedAt: stat.mtimeMs });
});
});
} catch { /* no results saved yet */ }
res.json({ files: list.sort((a, b) => b.savedAt - a.savedAt).slice(0, 200) });
});
app.get("/api/results/saved/:date/:name", (req, res) => {
const { date, name } = req.params;
if (!/^\d{4}-\d{2}-\d{2}$/.test(date) || safeResultName(name, "") !== name || !name.toLowerCase().endsWith(".pdf")) {
res.status(404).end();
return;
}
const file = path.join(RESULTS_PDF_DIR, date, name);
if (!fs.existsSync(file)) { res.status(404).end(); return; }
res.type("application/pdf").sendFile(file);
});
app.post("/api/export/pdf", (req, res) => {
try {
const payload = normalizePdfExportPayload(req.body || {});