feat: update Directus API routes, add health endpoint

This commit is contained in:
makearmy 2025-09-22 11:28:07 -04:00
parent 78f8d225ee
commit 79f0af51eb
7 changed files with 771 additions and 807 deletions

View file

@ -1,21 +1,16 @@
// app/api/submit/settings/route.ts
import { NextRequest, NextResponse } from "next/server";
import {
bytesFromMB,
createSettingsItem,
directusFetch,
uploadFile,
} from "@/lib/directus";
import { NextResponse } from "next/server";
import { uploadFile, createSettingsItem, bytesFromMB } from "@/lib/directus";
/**
* Accepts EITHER:
* - application/json (photo/screen can be data URLs: photo_data, screen_data)
* - application/json (photo/screen as data URLs via preview_image{ name,data })
* - multipart/form-data with:
* - "payload" = JSON string (same shape as JSON body)
* - "photo" = result image (REQUIRED)
* - "screen" = screenshot image (optional)
* - field "payload" = JSON string (same shape as JSON body)
* - optional field "photo" = single file to upload
* - optional field "screen" = single file to upload
*
* Targets (collections):
* Collections (targets):
* - settings_fiber (+ laser_soft, repeat_all)
* - settings_co2gan
* - settings_co2gal
@ -24,10 +19,12 @@ import {
export const runtime = "nodejs";
type Target = "settings_fiber" | "settings_co2gan" | "settings_co2gal" | "settings_uv";
const MAX_MB = Number(process.env.FILE_MAX_MB || 25);
const MAX_BYTES = bytesFromMB(MAX_MB);
// light in-memory rate limiter
// simple in-memory rate limiter
const BUCKET = new Map<string, { c: number; resetAt: number }>();
const WINDOW_MS = Number(process.env.RATE_LIMIT_WINDOW || 60) * 1000;
const MAX_REQ = Number(process.env.RATE_LIMIT_MAX || 15);
@ -43,127 +40,22 @@ function rateLimitOk(ip: string) {
return true;
}
type Target = "settings_fiber" | "settings_co2gan" | "settings_co2gal" | "settings_uv";
/** Map target + kind → Directus folder name */
function folderName(target: Target, kind: "photo" | "screen") {
const base =
target === "settings_fiber"
? "le_fiber_settings"
: target === "settings_uv"
? "le_uv_settings"
: target === "settings_co2gal"
? "le_co2gal_settings"
: "le_co2gan_settings";
return kind === "photo" ? `${base}_photos` : `${base}_screenshots`;
}
/** Lookup a folder id by name, returns null if not found */
async function findFolderIdByName(name: string): Promise<string | null> {
try {
const res = await directusFetch<{ data: Array<{ id: string }> }>(
`/folders?limit=1&fields=id&filter[name][_eq]=${encodeURIComponent(name)}`
);
const id = res?.data?.[0]?.id ?? null;
return id || null;
} catch {
return null;
}
}
/** Patch a file to move it into a folder (no-op if folderId is null) */
async function moveFileToFolder(fileId: string, folderId: string | null) {
if (!fileId || !folderId) return;
await directusFetch(`/files/${fileId}`, {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ folder: folderId }),
});
}
// Whitelists for repeaters (matches your Directus repeater schemas)
const FILL_KEYS = new Set([
"name",
"power",
"speed",
"interval",
"pass",
"type",
"flood",
"air",
"frequency",
"pulse",
"angle",
"auto",
"increment",
"cross",
]);
const LINE_KEYS = new Set([
"name",
"power",
"speed",
"perf",
"cut",
"skip",
"pass",
"air",
"frequency",
"pulse",
"wobble",
"step",
"size",
]);
const RASTER_KEYS = new Set([
"name",
"power",
"speed",
"type",
"dither",
"halftone_cell",
"halftone_angle",
"inversion",
"interval",
"dot",
"pass",
"air",
"frequency",
"pulse",
"cross",
]);
function sanitizeNumber(n: any, fallback: number | null = null) {
if (n === null || n === undefined || n === "") return fallback;
const v = Number(n);
return Number.isFinite(v) ? v : fallback;
}
function sanitizeRepeaterRow(
row: Record<string, any>,
allowed: Set<string>
): Record<string, any> {
function bool(v: any) {
return !!v;
}
function sanitizeRepeaterRow(row: Record<string, any>, allowed: Set<string>) {
const out: Record<string, any> = {};
for (const k of Object.keys(row || {})) {
if (!allowed.has(k)) continue;
if (
[
"power",
"speed",
"interval",
"pass",
"halftone_cell",
"halftone_angle",
"dot",
"frequency",
"pulse",
"angle",
"increment",
"step",
"size",
].includes(k)
) {
if (["power","speed","interval","pass","halftone_cell","halftone_angle","dot","angle","increment","frequency","pulse","step","size"].includes(k)) {
out[k] = sanitizeNumber(row[k]);
} else if (["auto", "cross", "wobble", "perf", "air", "flood", "inversion"].includes(k)) {
out[k] = !!row[k];
} else {
out[k] = row[k];
}
@ -171,9 +63,29 @@ function sanitizeRepeaterRow(
return out;
}
async function readJsonOrMultipart(req: NextRequest) {
const ct = req.headers.get("content-type") || "";
// Based on your note: folders are "<parent>/<child>"
function folderPath(target: Target, kind: "photo" | "screen"): string {
const parent =
target === "settings_fiber" ? "le_fiber_settings" :
target === "settings_co2gan" ? "le_co2gan_settings" :
target === "settings_co2gal" ? "le_co2gal_settings" :
"le_uv_settings";
const child = kind === "photo"
? `${parent}_photos`
: `${parent}_screenshots`;
return `${parent}/${child}`;
}
async function readJsonOrMultipart(req: Request): Promise<{
mode: "json" | "multipart";
body: any;
files: { photo?: File; screen?: File };
}> {
const ct = req.headers.get("content-type") || "";
if (ct.includes("application/json")) {
const body = await req.json();
return { mode: "json", body, files: {} };
}
if (ct.includes("multipart/form-data")) {
const form = await req.formData();
const payloadRaw = String(form.get("payload") || "{}");
@ -183,27 +95,22 @@ async function readJsonOrMultipart(req: NextRequest) {
} catch {
throw new Error("Invalid JSON in 'payload' field");
}
const files = {
photo: (form.get("photo") as File) || null,
screen: (form.get("screen") as File) || null,
};
return { mode: "multipart" as const, body, files };
const photo = (form.get("photo") as File) || undefined;
const screen = (form.get("screen") as File) || undefined;
return { mode: "multipart", body, files: { photo, screen } };
}
if (ct.includes("application/json")) {
const body = await req.json();
return { mode: "json" as const, body, files: { photo: null as File | null, screen: null as File | null } };
}
throw new Error("Unsupported content-type. Use JSON or multipart/form-data.");
}
export async function POST(req: NextRequest) {
// Whitelists for repeaters (align with read-side UI)
const FILL_KEYS = new Set(["name","power","speed","interval","pass","type","flood","air","frequency","pulse","angle","auto","increment","cross"]);
const LINE_KEYS = new Set(["name","power","speed","perf","cut","skip","pass","air","frequency","pulse","wobble","step","size"]);
const RASTER_KEYS = new Set(["name","power","speed","type","dither","halftone_cell","halftone_angle","inversion","interval","dot","pass","air","frequency","pulse","cross"]);
export async function POST(req: Request) {
const started = Date.now();
try {
const ip =
req.headers.get("x-forwarded-for")?.split(",")[0]?.trim() ||
"0.0.0.0";
const ip = req.headers.get("x-forwarded-for")?.split(",")[0]?.trim() || "0.0.0.0";
if (!rateLimitOk(ip)) {
return NextResponse.json({ error: "Rate limited" }, { status: 429 });
}
@ -211,181 +118,134 @@ export async function POST(req: NextRequest) {
const { mode, body, files } = await readJsonOrMultipart(req);
const target: Target = body?.target;
if (!["settings_fiber", "settings_co2gan", "settings_co2gal", "settings_uv"].includes(target as any)) {
if (!target || !["settings_fiber","settings_co2gan","settings_co2gal","settings_uv"].includes(target)) {
return NextResponse.json({ error: "Invalid target" }, { status: 400 });
}
// Required base fields
const setting_title = String(body?.setting_title || body?.title || "").trim();
const uploader = String(body?.uploader || "").trim();
if (!setting_title) return NextResponse.json({ error: "Missing field: setting_title" }, { status: 400 });
if (!uploader) return NextResponse.json({ error: "Missing field: uploader" }, { status: 400 });
// Relations (required)
// Relations & numerics
const mat = body?.mat ?? null;
const mat_coat = body?.mat_coat ?? null;
const mat_color = body?.mat_color ?? null;
const mat_opacity = body?.mat_opacity ?? null;
const source = body?.source ?? null;
const lens = body?.lens ?? null;
const focus = sanitizeNumber(body?.focus, null);
const mat_thickness = sanitizeNumber(body?.mat_thickness, null);
const setting_notes = String(body?.setting_notes || body?.notes || "").trim() || "";
// Numbers
const mat_thickness = sanitizeNumber(body?.mat_thickness, null);
const focus = sanitizeNumber(body?.focus, null);
// Fiber-only requireds per your spec
const laser_soft = target === "settings_fiber" ? (body?.laser_soft ?? null) : undefined;
const repeat_all = target === "settings_fiber" ? sanitizeNumber(body?.repeat_all, null) : undefined;
// Notes (optional)
const setting_notes = String(body?.setting_notes || body?.notes || "").trim() || "";
// Fiber-only (required)
const laser_soft =
target === "settings_fiber" ? (body?.laser_soft ?? null) : null;
const repeat_all =
target === "settings_fiber" ? sanitizeNumber(body?.repeat_all, null) : null;
// Validate requireds
// Validate requireds per your list (server-side guardrails)
const missing: string[] = [];
if (!setting_title) missing.push("setting_title");
if (!uploader) missing.push("uploader");
if (!source) missing.push("source");
if (!lens) missing.push("lens");
if (focus === null || !Number.isFinite(focus)) missing.push("focus");
if (!mat) missing.push("mat");
if (!mat_coat) missing.push("mat_coat");
if (!mat_color) missing.push("mat_color");
if (!mat_opacity) missing.push("mat_opacity");
if (target === "settings_fiber") {
if (!laser_soft) missing.push("laser_soft");
if (repeat_all === null || !Number.isFinite(repeat_all)) missing.push("repeat_all");
for (const [k, v] of Object.entries({
source, lens, focus,
mat, mat_coat, mat_color, mat_opacity,
...(target === "settings_fiber" ? { laser_soft, repeat_all } : {}),
})) { if (v === null || v === "" || v === undefined) missing.push(k); }
if (missing.length) {
return NextResponse.json({ error: `Missing required field(s): ${missing.join(", ")}` }, { status: 400 });
}
// Handle files (photo required)
// Handle files (multipart) OR data URLs (json)
let photo_id: string | null = null;
let screen_id: string | null = null;
// If multipart, use file objects. Else allow base64 in JSON: photo_data/screen_data
if (mode === "multipart") {
if (!files.photo) missing.push("photo");
if (files.photo) {
if (files.photo.size > MAX_BYTES) {
return NextResponse.json(
{ error: `Photo exceeds ${MAX_MB} MB` },
{ status: 400 }
);
}
const up = await uploadFile(files.photo, (files.photo as File).name || "photo");
photo_id = (up as any)?.id ?? null;
async function guardSize(name: string, file: File) {
if (file.size > MAX_BYTES) {
throw new Error(`${name} exceeds ${MAX_MB} MB`);
}
}
// after upload, move into appropriate folder
const folder = await findFolderIdByName(folderName(target, "photo"));
await moveFileToFolder(String(photo_id), folder);
if (mode === "multipart") {
if (files.photo) {
await guardSize("photo", files.photo);
const up = await uploadFile(files.photo, (files.photo as File).name || "photo", {
folderNamePath: folderPath(target, "photo"),
title: setting_title,
});
photo_id = up.id;
}
if (files.screen) {
if (files.screen.size > MAX_BYTES) {
return NextResponse.json(
{ error: `Screenshot exceeds ${MAX_MB} MB` },
{ status: 400 }
);
}
const up = await uploadFile(files.screen, (files.screen as File).name || "screen");
screen_id = (up as any)?.id ?? null;
const folder = await findFolderIdByName(folderName(target, "screen"));
await moveFileToFolder(String(screen_id), folder);
await guardSize("screen", files.screen);
const up = await uploadFile(files.screen, (files.screen as File).name || "screen", {
folderNamePath: folderPath(target, "screen"),
title: setting_title,
});
screen_id = up.id;
}
} else {
// JSON mode with optional base64 strings
const pushBase64 = async (dataUrl: string, name: string) => {
const base64 = (dataUrl || "").split(",")[1] || "";
if (!base64) return null;
// JSON mode supports preview_image: { name, data } for both "photo" and "screen"
const upFromDataUrl = async (obj: any, kind: "photo" | "screen") => {
const dataUrl: string = obj?.data;
const name: string = obj?.name || kind;
if (!dataUrl) return null;
const base64 = dataUrl.split(",")[1] || "";
const raw = Buffer.from(base64, "base64");
if (raw.byteLength > MAX_BYTES) throw new Error(`${name} exceeds ${MAX_MB} MB`);
if (raw.byteLength > MAX_BYTES) throw new Error(`${kind} exceeds ${MAX_MB} MB`);
const blob = new Blob([raw]);
const up = await uploadFile(blob as any, name);
return (up as any)?.id ?? null;
const up = await uploadFile(blob as any, name, {
folderNamePath: folderPath(target, kind),
title: setting_title,
});
return up.id;
};
if (body?.photo_data) {
photo_id = await pushBase64(body.photo_data, "photo");
const folder = await findFolderIdByName(folderName(target, "photo"));
await moveFileToFolder(String(photo_id), folder);
} else {
missing.push("photo");
}
if (body?.screen_data) {
screen_id = await pushBase64(body.screen_data, "screen");
const folder = await findFolderIdByName(folderName(target, "screen"));
await moveFileToFolder(String(screen_id), folder);
}
if (body?.preview_image) photo_id = await upFromDataUrl(body.preview_image, "photo");
if (body?.preview_screen) screen_id = await upFromDataUrl(body.preview_screen, "screen");
}
if (missing.length) {
return NextResponse.json(
{ error: `Missing required: ${missing.join(", ")}` },
{ status: 400 }
);
}
// Repeaters (sanitize)
const fills = Array.isArray(body?.fill_settings) ? body.fill_settings : body?.fills || [];
const lines = Array.isArray(body?.line_settings) ? body.line_settings : body?.lines || [];
const rasters = Array.isArray(body?.raster_settings) ? body.raster_settings : body?.rasters || [];
// Repeaters
const fillsRaw = Array.isArray(body?.fill_settings) ? body.fill_settings : body?.fills || [];
const linesRaw = Array.isArray(body?.line_settings) ? body.line_settings : body?.lines || [];
const rastersRaw = Array.isArray(body?.raster_settings) ? body.raster_settings : body?.rasters || [];
const fill_settings = (fills as any[]).map((r) => sanitizeRepeaterRow(r, FILL_KEYS));
const line_settings = (lines as any[]).map((r) => sanitizeRepeaterRow(r, LINE_KEYS));
const raster_settings = (rasters as any[]).map((r) => sanitizeRepeaterRow(r, RASTER_KEYS));
const fill_settings = (fillsRaw as any[]).map((r) => sanitizeRepeaterRow(r, FILL_KEYS));
const line_settings = (linesRaw as any[]).map((r) => sanitizeRepeaterRow(r, LINE_KEYS));
const raster_settings = (rastersRaw as any[]).map((r) => sanitizeRepeaterRow(r, RASTER_KEYS));
// Build record
// Build payload (use your read-side keys)
const nowIso = new Date().toISOString();
const payload: Record<string, any> = {
setting_title,
uploader,
setting_notes,
// relations
mat,
mat_coat,
mat_color,
mat_opacity,
source,
lens,
// numbers
focus,
photo: photo_id, // null if none
screen: screen_id, // null if none
mat, mat_coat, mat_color, mat_opacity,
mat_thickness,
// files
photo: photo_id,
screen: screen_id,
// repeaters
fill_settings,
line_settings,
raster_settings,
// meta
submission_date: nowIso,
last_modified_date: nowIso,
source, lens, focus,
fill_settings, line_settings, raster_settings,
status: "pending",
submitted_via: "makearmy-app",
submitted_at: nowIso,
submission_date: nowIso,
last_modified_date: nowIso, // harmless even if you later make it optional
};
if (target === "settings_fiber") {
payload.laser_soft = laser_soft;
payload.repeat_all = repeat_all;
payload.laser_soft = laser_soft ?? null;
payload.repeat_all = repeat_all ?? null;
}
const created = await createSettingsItem(target, payload);
const { data } = await createSettingsItem(target, payload);
const newId = data?.submission_id ?? data?.id ?? null;
// normalize PK to always provide "id" (your tables use submission_id)
const newId =
(created as any)?.submission_id ??
(created as any)?.data?.submission_id ??
(created as any)?.id ??
(created as any)?.data?.id ??
null;
return NextResponse.json({
ok: true,
id: newId,
submission_id: newId,
took_ms: Date.now() - started,
});
return NextResponse.json({ ok: true, id: newId });
} catch (err: any) {
const msg = err?.message || "Unknown error";
return NextResponse.json({ error: msg }, { status: 500 });
console.error("[submit] error", err?.message || err);
return NextResponse.json(
{ error: err?.message || "Unknown error" },
{ status: 500 }
);
} finally {
// cheap request duration log
const ms = Date.now() - (globalThis as any).__start_ts ?? 0;
if (ms) console.log(`[submit/settings] handled in ~${ms}ms`);
}
}