Move web app into apps workspace

This commit is contained in:
Formsmith
2026-06-08 22:19:00 +03:00
parent 4b222f9884
commit fb54762ef0
73 changed files with 13 additions and 12 deletions
@@ -0,0 +1,38 @@
import { mkdir, writeFile } from "node:fs/promises";
import path from "node:path";
import { NextRequest, NextResponse } from "next/server";
const localHosts = new Set(["localhost", "127.0.0.1", "::1", "[::1]"]);
function sanitizeFileName(value: unknown) {
const fallback = `codex-screenshot-${Date.now()}.png`;
if (typeof value !== "string") {
return fallback;
}
const cleaned = value.replace(/[^a-z0-9_.-]+/gi, "_").replace(/^_+|_+$/g, "");
return cleaned.endsWith(".png") ? cleaned : `${cleaned || fallback}.png`;
}
export async function POST(request: NextRequest) {
if (process.env.NODE_ENV === "production") {
return NextResponse.json({ error: "Codex screenshot capture is only available in local development." }, { status: 404 });
}
const host = request.headers.get("host")?.split(":")[0] ?? "";
if (!localHosts.has(host)) {
return NextResponse.json({ error: "Codex screenshot capture only accepts local requests." }, { status: 403 });
}
const body = (await request.json()) as { dataUrl?: unknown; name?: unknown };
if (typeof body.dataUrl !== "string" || !body.dataUrl.startsWith("data:image/png;base64,")) {
return NextResponse.json({ error: "Expected a PNG data URL." }, { status: 400 });
}
const bytes = Buffer.from(body.dataUrl.slice("data:image/png;base64,".length), "base64");
const directory = path.join(process.cwd(), ".codex", "screenshots");
await mkdir(directory, { recursive: true });
const filePath = path.join(directory, sanitizeFileName(body.name));
await writeFile(filePath, bytes);
return NextResponse.json({ path: filePath, bytes: bytes.length });
}
@@ -0,0 +1,31 @@
import { promises as fs } from "fs";
import path from "path";
import { NextResponse } from "next/server";
function safeFileName(filename: string) {
const base = path.basename(filename).replace(/[<>:"/\\|?*\u0000-\u001f]/g, "_").trim();
return base || "download.txt";
}
export async function POST(request: Request) {
try {
const body = (await request.json()) as { content?: unknown; filename?: unknown; folder?: unknown };
if (typeof body.content !== "string" || typeof body.filename !== "string" || typeof body.folder !== "string") {
return NextResponse.json({ error: "Invalid download request" }, { status: 400 });
}
const trimmedFolder = body.folder.trim();
if (!trimmedFolder) {
return NextResponse.json({ error: "Choose a folder first" }, { status: 400 });
}
const targetDirectory = path.isAbsolute(trimmedFolder) ? trimmedFolder : path.join(process.cwd(), trimmedFolder);
await fs.mkdir(targetDirectory, { recursive: true });
const targetPath = path.join(targetDirectory, safeFileName(body.filename));
await fs.writeFile(targetPath, body.content, "utf8");
return NextResponse.json({ path: targetPath });
} catch (error) {
return NextResponse.json({ error: error instanceof Error ? error.message : "Could not save file" }, { status: 500 });
}
}
@@ -0,0 +1,78 @@
import { promises as fs } from "fs";
import path from "path";
import { NextResponse } from "next/server";
const THUMBNAIL_DIR = path.join(process.cwd(), ".codex", "project-thumbnails");
function safeProjectId(projectId: string) {
const clean = projectId.replace(/[^a-zA-Z0-9_-]/g, "");
return clean || null;
}
function thumbnailPath(projectId: string) {
const safeId = safeProjectId(projectId);
if (!safeId) {
return null;
}
return path.join(THUMBNAIL_DIR, `${safeId}.png`);
}
export async function GET(request: Request) {
const projectId = new URL(request.url).searchParams.get("projectId") ?? "";
const filePath = thumbnailPath(projectId);
if (!filePath) {
return new NextResponse("Invalid project id", { status: 400 });
}
try {
const image = await fs.readFile(filePath);
return new NextResponse(image, {
headers: {
"Cache-Control": "no-store",
"Content-Type": "image/png",
},
});
} catch {
return new NextResponse("Thumbnail not found", { status: 404 });
}
}
export async function POST(request: Request) {
let body: { dataUrl?: unknown; projectId?: unknown };
try {
body = (await request.json()) as { dataUrl?: unknown; projectId?: unknown };
} catch {
return NextResponse.json({ error: "Invalid thumbnail request" }, { status: 400 });
}
try {
if (typeof body.projectId !== "string" || typeof body.dataUrl !== "string") {
return NextResponse.json({ error: "Invalid thumbnail request" }, { status: 400 });
}
const filePath = thumbnailPath(body.projectId);
const match = body.dataUrl.match(/^data:image\/png;base64,(.+)$/);
if (!filePath || !match) {
return NextResponse.json({ error: "Invalid thumbnail image" }, { status: 400 });
}
await fs.mkdir(THUMBNAIL_DIR, { recursive: true });
await fs.rm(filePath, { force: true });
await fs.writeFile(filePath, Buffer.from(match[1], "base64"));
return NextResponse.json({ version: Date.now() });
} catch (error) {
return NextResponse.json({ error: error instanceof Error ? error.message : "Could not save thumbnail" }, { status: 500 });
}
}
export async function DELETE(request: Request) {
const projectId = new URL(request.url).searchParams.get("projectId") ?? "";
const filePath = thumbnailPath(projectId);
if (!filePath) {
return NextResponse.json({ error: "Invalid project id" }, { status: 400 });
}
await fs.rm(filePath, { force: true });
return NextResponse.json({ deleted: true });
}