Theia rip-out: - Delete app/api/theia-auth/route.ts (Traefik ForwardAuth shim) - Delete app/api/projects/[projectId]/workspace/route.ts and app/api/projects/prewarm/route.ts (Cloud Run Theia provisioning) - Delete lib/cloud-run-workspace.ts and lib/coolify-workspace.ts - Strip provisionTheiaWorkspace + theiaWorkspaceUrl/theiaAppUuid/ theiaError from app/api/projects/create/route.ts response - Remove Theia callbackUrl branch in app/auth/page.tsx - Drop "Open in Theia" button + xterm/Theia PTY copy in build/page.tsx - Drop theiaWorkspaceUrl from deployment/page.tsx Project type - Strip Theia IDE line + theia-code-os from advisor + agent-chat context strings - Scrub Theia mention from lib/auth/workspace-auth.ts comment P5.1 (custom apex domains + DNS): - lib/coolify.ts + lib/opensrs.ts: nameserver normalization, OpenSRS XML auth, Cloud DNS plumbing - scripts/smoke-attach-e2e.ts: full prod GCP + sandbox OpenSRS + prod Coolify smoke covering register/zone/A/NS/PATCH/cleanup In-progress (Justine onboarding/build, MVP setup, agent telemetry): - New (justine)/stories, project (home) layouts, mvp-setup, run, tasks routes + supporting components - Project shell + sidebar + nav refactor for the Stackless palette - Agent session API hardening (sessions, events, stream, approve, retry, stop) + atlas-chat, advisor, design-surfaces refresh - New scripts/sync-db-url-from-coolify.mjs + scripts/prisma-db-push.mjs + docker-compose.local-db.yml for local Prisma workflows - lib/dev-bypass.ts, lib/chat-context-refs.ts, lib/prd-sections.ts - Misc: stories CSS, debug/prisma route, modal-theme, BuildLivePlanPanel Made-with: Cursor
108 lines
3.6 KiB
TypeScript
108 lines
3.6 KiB
TypeScript
/**
|
|
* GET /api/projects/[projectId]/file?path=apps/admin
|
|
*
|
|
* Returns directory listing or file content from the project's Gitea repo.
|
|
* Response for directory: { type: "dir", items: [{ name, path, type }] }
|
|
* Response for file: { type: "file", content: string, encoding: "utf8" | "base64" }
|
|
*/
|
|
import { NextResponse } from 'next/server';
|
|
import { authSession } from "@/lib/auth/session-server";
|
|
import { query } from '@/lib/db-postgres';
|
|
|
|
const GITEA_API_URL = process.env.GITEA_API_URL ?? 'https://git.vibnai.com';
|
|
const GITEA_API_TOKEN = process.env.GITEA_API_TOKEN ?? '';
|
|
|
|
async function giteaGet(path: string) {
|
|
const res = await fetch(`${GITEA_API_URL}/api/v1${path}`, {
|
|
headers: { Authorization: `token ${GITEA_API_TOKEN}` },
|
|
next: { revalidate: 10 },
|
|
});
|
|
if (!res.ok) throw new Error(`Gitea ${res.status}: ${path}`);
|
|
return res.json();
|
|
}
|
|
|
|
const BINARY_EXTENSIONS = new Set([
|
|
'png', 'jpg', 'jpeg', 'gif', 'webp', 'svg', 'ico',
|
|
'woff', 'woff2', 'ttf', 'eot',
|
|
'zip', 'tar', 'gz', 'pdf',
|
|
]);
|
|
|
|
function isBinary(name: string): boolean {
|
|
const ext = name.split('.').pop()?.toLowerCase() ?? '';
|
|
return BINARY_EXTENSIONS.has(ext);
|
|
}
|
|
|
|
export async function GET(
|
|
req: Request,
|
|
{ params }: { params: Promise<{ projectId: string }> }
|
|
) {
|
|
try {
|
|
const { projectId } = await params;
|
|
const session = await authSession();
|
|
if (!session?.user?.email) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
|
}
|
|
|
|
const { searchParams } = new URL(req.url);
|
|
const filePath = searchParams.get('path') ?? '';
|
|
|
|
// Verify ownership + get giteaRepo
|
|
const rows = await query<{ data: Record<string, unknown> }>(
|
|
`SELECT p.data FROM fs_projects p
|
|
JOIN fs_users u ON u.id = p.user_id
|
|
WHERE p.id = $1 AND u.data->>'email' = $2 LIMIT 1`,
|
|
[projectId, session.user.email]
|
|
);
|
|
if (rows.length === 0) {
|
|
return NextResponse.json({ error: 'Project not found' }, { status: 404 });
|
|
}
|
|
|
|
const giteaRepo = rows[0].data?.giteaRepo as string | undefined;
|
|
if (!giteaRepo) {
|
|
return NextResponse.json({ error: 'No Gitea repo connected' }, { status: 404 });
|
|
}
|
|
|
|
const encodedPath = filePath ? encodeURIComponent(filePath).replace(/%2F/g, '/') : '';
|
|
const apiPath = `/repos/${giteaRepo}/contents/${encodedPath}`;
|
|
const data = await giteaGet(apiPath);
|
|
|
|
// Directory listing
|
|
if (Array.isArray(data)) {
|
|
const items = data
|
|
.map((item: { name: string; path: string; type: string; size?: number }) => ({
|
|
name: item.name,
|
|
path: item.path,
|
|
type: item.type, // "file" | "dir" | "symlink"
|
|
size: item.size,
|
|
}))
|
|
.sort((a, b) => {
|
|
// Dirs first
|
|
if (a.type === 'dir' && b.type !== 'dir') return -1;
|
|
if (a.type !== 'dir' && b.type === 'dir') return 1;
|
|
return a.name.localeCompare(b.name);
|
|
});
|
|
return NextResponse.json({ type: 'dir', items });
|
|
}
|
|
|
|
// Single file
|
|
const item = data as { name: string; content?: string; encoding?: string; size?: number };
|
|
if (isBinary(item.name)) {
|
|
return NextResponse.json({ type: 'file', content: '(binary file)', encoding: 'utf8' });
|
|
}
|
|
|
|
// Gitea returns base64-encoded content
|
|
const raw = item.content ?? '';
|
|
let content: string;
|
|
try {
|
|
content = Buffer.from(raw.replace(/\n/g, ''), 'base64').toString('utf8');
|
|
} catch {
|
|
content = raw;
|
|
}
|
|
|
|
return NextResponse.json({ type: 'file', content, encoding: 'utf8', name: item.name });
|
|
} catch (err) {
|
|
console.error('[file API]', err);
|
|
return NextResponse.json({ error: 'Failed to fetch file' }, { status: 500 });
|
|
}
|
|
}
|