VR-MEDIA / src /app /api /settings /route.ts
Zero124's picture
VR Media Server 2.0 — latest build (custom Blomp thumbnail auto-detection + full pipeline)
5c0175e
Raw History Blame Contribute Delete
6.28 kB
// GET /api/settings — list settings (secrets masked).
// POST /api/settings — upsert an allow-listed set of settings: Offcloud API key,
// TeraBox/Blomp credentials, scan config and passthrough defaults.
import { NextRequest, NextResponse } from "next/server";
import { db } from "@/lib/db";
import { clearTeraboxStatusCache, TERABOX_SECRET_KEYS } from "@/lib/terabox";
import { clearBlompCache, BLOMP_SECRET_KEYS } from "@/lib/blomp";
export const dynamic = "force-dynamic";
/** Keys the UI is allowed to write through the generic loop. */
const WRITABLE_KEYS = [
// Offcloud + scanning
"offcloud_api_key",
"scan_paths",
"auto_scan",
// Blomp auto thumbnail upload (generate posters, push to the drive)
"blomp_autothumb",
// Quest 3 passthrough tuning (override the env defaults at runtime)
"passthrough_mode",
"passthrough_method",
"passthrough_color",
"passthrough_fov",
"passthrough_range",
"passthrough_falloff",
] as const;
/** Values masked on GET so secrets never round-trip to the browser. */
const SECRET_KEYS = new Set<string>([
"offcloud_api_key",
...TERABOX_SECRET_KEYS,
...BLOMP_SECRET_KEYS,
]);
/** Allowed values for the enum-like settings. */
const ENUMS: Record<string, string[]> = {
passthrough_mode: ["all", "auto", "off"],
passthrough_method: ["alpha", "chroma", "auto"],
auto_scan: ["true", "false"],
blomp_autothumb: ["true", "false"],
};
export async function GET() {
const settings = await db.setting.findMany();
const out: Record<string, string> = {};
for (const s of settings) {
if (SECRET_KEYS.has(s.key)) {
// Only reveal whether it is set.
out[s.key] = s.value ? "***set***" : "";
} else {
out[s.key] = s.value;
}
}
// Effective passthrough defaults come from env when no override is stored.
out.env_passthrough_mode = process.env.PASSTHROUGH_MODE || "all";
out.env_passthrough_method = process.env.PASSTHROUGH_METHOD || "auto";
out.env_passthrough_color = process.env.PASSTHROUGH_COLOR || "green";
out.env_passthrough_fov = process.env.PASSTHROUGH_FOV || "180";
return NextResponse.json(out);
}
export async function POST(req: NextRequest) {
const body = (await req.json().catch(() => ({}))) as Record<string, unknown>;
const updates: { key: string; value: string }[] = [];
const rejected: string[] = [];
const pushSecret = (key: string, value: unknown) => {
if (typeof value !== "string" || !value || value.startsWith("***")) return;
updates.push({ key, value });
};
for (const key of WRITABLE_KEYS) {
const raw = body[key];
if (typeof raw !== "string") continue;
const value = raw.trim();
// Never persist the masked placeholder back over a real secret.
if (SECRET_KEYS.has(key)) {
if (!value || value.startsWith("***")) continue;
}
// Validate enum-like keys instead of storing arbitrary text.
const allowed = ENUMS[key];
if (allowed && !allowed.includes(value)) {
rejected.push(key);
continue;
}
updates.push({ key, value });
}
const {
terabox_mode,
terabox_cookie,
terabox_root,
terabox_client_id,
terabox_client_secret,
terabox_private_secret,
blomp_email,
blomp_password,
blomp_container,
blomp_root,
} = body as Record<string, string | undefined>;
// ---- TeraBox ----
if (terabox_mode === "cookie" || terabox_mode === "oauth") {
updates.push({ key: "terabox_mode", value: terabox_mode });
}
pushSecret("terabox_cookie", terabox_cookie);
pushSecret("terabox_client_secret", terabox_client_secret);
pushSecret("terabox_private_secret", terabox_private_secret);
if (typeof terabox_client_id === "string" && terabox_client_id.trim()) {
updates.push({ key: "terabox_client_id", value: terabox_client_id.trim() });
}
if (typeof terabox_root === "string") {
const root = terabox_root.trim() || "/";
updates.push({ key: "terabox_root", value: root.startsWith("/") ? root : `/${root}` });
}
// Saving a cookie without an explicit mode implies cookie mode.
if (terabox_cookie && !terabox_mode) {
updates.push({ key: "terabox_mode", value: "cookie" });
}
// ---- Blomp ----
if (typeof blomp_email === "string" && blomp_email.trim()) {
updates.push({ key: "blomp_email", value: blomp_email.trim() });
}
pushSecret("blomp_password", blomp_password);
if (typeof blomp_container === "string") {
// Empty = auto-detect (email-named container, or the only one).
updates.push({
key: "blomp_container",
value: blomp_container.trim().replace(/^\/+|\/+$/g, ""),
});
}
if (typeof blomp_root === "string") {
const raw = blomp_root.trim();
if (!raw) {
// Blank clears the override, so scans auto-detect folders (default /deovr).
updates.push({ key: "blomp_root", value: "" });
} else {
const trimmed = raw.replace(/^\/+|\/+$/g, "");
// "/" is meaningful: scan the whole container.
updates.push({ key: "blomp_root", value: trimmed ? `/${trimmed}` : "/" });
}
}
for (const u of updates) {
await db.setting.upsert({
where: { key: u.key },
update: { value: u.value },
create: { key: u.key, value: u.value },
});
}
if (updates.some((u) => u.key.startsWith("terabox_"))) {
clearTeraboxStatusCache();
}
if (updates.some((u) => u.key.startsWith("blomp_"))) {
clearBlompCache();
// Connecting or re-configuring Blomp should make the library usable right
// away: run a full reconcile in the background and arm the 60s loop. The
// detection queue it fills starts automatically.
try {
const { startBlompSync, syncBlompOnce } = await import("@/lib/blomp-sync");
void startBlompSync().catch(() => {});
void syncBlompOnce({ mode: "full", trigger: "manual" }).catch(() => {});
} catch (err) {
console.warn("[settings] could not start Blomp sync:", err);
}
}
return NextResponse.json({
ok: true,
updated: updates.length,
keys: updates.map((u) => u.key),
...(rejected.length ? { rejected } : {}),
});
}