Add workspace editor tools

This commit is contained in:
Formsmith
2026-06-14 22:08:55 +03:00
parent 9a889fdcb7
commit 753a1c2833
29 changed files with 2849 additions and 1397 deletions
@@ -2,7 +2,13 @@ import { promises as fs } from "fs";
import path from "path";
import { NextResponse } from "next/server";
export const revalidate = false;
const THUMBNAIL_DIR = path.join(process.cwd(), ".codex", "project-thumbnails");
const LOCAL_HOSTS = new Set(["localhost", "127.0.0.1", "::1", "[::1]"]);
const PNG_DATA_URL_PREFIX = "data:image/png;base64,";
const MAX_THUMBNAIL_BYTES = 5 * 1024 * 1024;
const MAX_THUMBNAIL_REQUEST_BYTES = Math.ceil((MAX_THUMBNAIL_BYTES * 4) / 3) + PNG_DATA_URL_PREFIX.length + 2048;
function safeProjectId(projectId: string) {
const clean = projectId.replace(/[^a-zA-Z0-9_-]/g, "");
@@ -17,7 +23,41 @@ function thumbnailPath(projectId: string) {
return path.join(THUMBNAIL_DIR, `${safeId}.png`);
}
function isLocalSameOriginRequest(request: Request) {
const requestUrl = new URL(request.url);
if (!LOCAL_HOSTS.has(requestUrl.hostname)) {
return false;
}
const origin = request.headers.get("origin");
if (origin) {
try {
const originUrl = new URL(origin);
if (originUrl.origin !== requestUrl.origin || !LOCAL_HOSTS.has(originUrl.hostname)) {
return false;
}
} catch {
return false;
}
}
const fetchSite = request.headers.get("sec-fetch-site");
return !fetchSite || fetchSite === "same-origin" || fetchSite === "none";
}
function decodedBase64ByteLength(value: string) {
if (value.length % 4 === 1 || !/^[A-Za-z0-9+/]+={0,2}$/.test(value)) {
return null;
}
const padding = value.endsWith("==") ? 2 : value.endsWith("=") ? 1 : 0;
return Math.floor((value.length * 3) / 4) - padding;
}
export async function GET(request: Request) {
if (!isLocalSameOriginRequest(request)) {
return new NextResponse("Project thumbnails are only available from this localhost app", { status: 403 });
}
const projectId = new URL(request.url).searchParams.get("projectId") ?? "";
const filePath = thumbnailPath(projectId);
if (!filePath) {
@@ -38,6 +78,15 @@ export async function GET(request: Request) {
}
export async function POST(request: Request) {
if (!isLocalSameOriginRequest(request)) {
return NextResponse.json({ error: "Project thumbnails are only available from this localhost app" }, { status: 403 });
}
const contentLength = Number(request.headers.get("content-length"));
if (Number.isFinite(contentLength) && contentLength > MAX_THUMBNAIL_REQUEST_BYTES) {
return NextResponse.json({ error: "Thumbnail image is too large" }, { status: 413 });
}
let body: { dataUrl?: unknown; projectId?: unknown };
try {
body = (await request.json()) as { dataUrl?: unknown; projectId?: unknown };
@@ -51,14 +100,22 @@ export async function POST(request: Request) {
}
const filePath = thumbnailPath(body.projectId);
const match = body.dataUrl.match(/^data:image\/png;base64,(.+)$/);
if (!filePath || !match) {
if (!filePath || !body.dataUrl.startsWith(PNG_DATA_URL_PREFIX)) {
return NextResponse.json({ error: "Invalid thumbnail image" }, { status: 400 });
}
const encodedImage = body.dataUrl.slice(PNG_DATA_URL_PREFIX.length);
const decodedBytes = decodedBase64ByteLength(encodedImage);
if (decodedBytes === null) {
return NextResponse.json({ error: "Invalid thumbnail image" }, { status: 400 });
}
if (decodedBytes > MAX_THUMBNAIL_BYTES) {
return NextResponse.json({ error: "Thumbnail image is too large" }, { status: 413 });
}
await fs.mkdir(THUMBNAIL_DIR, { recursive: true });
await fs.rm(filePath, { force: true });
await fs.writeFile(filePath, Buffer.from(match[1], "base64"));
await fs.writeFile(filePath, Buffer.from(encodedImage, "base64"));
return NextResponse.json({ version: Date.now() });
} catch (error) {
@@ -67,6 +124,10 @@ export async function POST(request: Request) {
}
export async function DELETE(request: Request) {
if (!isLocalSameOriginRequest(request)) {
return NextResponse.json({ error: "Project thumbnails are only available from this localhost app" }, { status: 403 });
}
const projectId = new URL(request.url).searchParams.get("projectId") ?? "";
const filePath = thumbnailPath(projectId);
if (!filePath) {