File size: 2,070 Bytes
eeb9404
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { NextRequest, NextResponse } from 'next/server';
import { getSession } from '@/lib/auth/session';
import { taskManager } from '@/lib/server-generate/singleton';
import { VirtualFileSystem } from '@/lib/vfs';
import { getWorkspaceAdapter, getSQLiteAdapter } from '@/lib/vfs/adapters/server';

async function getVFSForTask(workspaceId?: string): Promise<VirtualFileSystem> {
  const adapter = workspaceId ? getWorkspaceAdapter(workspaceId) : getSQLiteAdapter();
  const vfs = new VirtualFileSystem(adapter);
  await vfs.init();
  return vfs;
}

export async function POST(request: NextRequest) {
  const session = await getSession();
  if (!session) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });

  let taskId: string, paths: string[];
  try { ({ taskId, paths } = await request.json()); } catch {
    return NextResponse.json({ error: 'Invalid JSON' }, { status: 400 });
  }
  if (!taskId || !Array.isArray(paths)) {
    return NextResponse.json({ error: 'Missing taskId or paths' }, { status: 400 });
  }

  const task = taskManager.getTask(taskId);
  if (!task) return NextResponse.json({ error: 'Task not found' }, { status: 404 });

  if (task.sessionId !== session.userId) {
    return NextResponse.json({ error: 'Forbidden' }, { status: 403 });
  }

  const vfs = await getVFSForTask(task.workspaceId);
  const files = [];
  const deleted = [];
  for (const filePath of paths) {
    try {
      const exists = await vfs.fileExists(task.projectId, filePath);
      if (!exists) {
        deleted.push(filePath);
        continue;
      }
      const file = await vfs.readFile(task.projectId, filePath);
      const binary = file.content instanceof ArrayBuffer;
      files.push({
        path: filePath,
        content: binary ? Buffer.from(file.content as ArrayBuffer).toString('base64') : file.content as string,
        binary,
      });
    } catch (err) {
      if (err instanceof Error && err.message.includes('not found')) {
        deleted.push(filePath);
      }
    }
  }

  return NextResponse.json({ files, deleted });
}