|
| 1 | +import { NextRequest, NextResponse } from 'next/server' |
| 2 | +import { createServerClient } from '@/lib/supabase-server' |
| 3 | + |
| 4 | +export const dynamic = 'force-dynamic' |
| 5 | + |
| 6 | +// Helper function to build file tree from flat list |
| 7 | +function buildFileTree(files: any[]): any[] { |
| 8 | + const tree: any[] = [] |
| 9 | + const pathMap = new Map<string, any>() |
| 10 | + |
| 11 | + // Sort files by path to ensure parents are processed before children |
| 12 | + const sortedFiles = files.sort((a, b) => a.path.localeCompare(b.path)) |
| 13 | + |
| 14 | + for (const file of sortedFiles) { |
| 15 | + const node = { |
| 16 | + name: file.name, |
| 17 | + path: file.path, |
| 18 | + isDirectory: file.is_directory, |
| 19 | + children: file.is_directory ? [] : undefined, |
| 20 | + } |
| 21 | + |
| 22 | + pathMap.set(file.path, node) |
| 23 | + |
| 24 | + if (file.parent_path) { |
| 25 | + const parent = pathMap.get(file.parent_path) |
| 26 | + if (parent && parent.children) { |
| 27 | + parent.children.push(node) |
| 28 | + } else { |
| 29 | + tree.push(node) |
| 30 | + } |
| 31 | + } else { |
| 32 | + tree.push(node) |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + return tree |
| 37 | +} |
| 38 | + |
| 39 | +export async function GET(request: NextRequest) { |
| 40 | + try { |
| 41 | + const supabase = createServerClient() |
| 42 | + |
| 43 | + // Get authenticated user from session |
| 44 | + const { data: { user }, error: authError } = await supabase.auth.getUser() |
| 45 | + |
| 46 | + if (authError || !user) { |
| 47 | + return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) |
| 48 | + } |
| 49 | + |
| 50 | + // Fetch all workspace files for the user |
| 51 | + const { data: files, error } = await supabase |
| 52 | + .from('workspace_files') |
| 53 | + .select('*') |
| 54 | + .eq('user_id', user.id) |
| 55 | + .order('path', { ascending: true }) |
| 56 | + |
| 57 | + if (error) { |
| 58 | + console.error('Error fetching workspace files:', error) |
| 59 | + return NextResponse.json({ error: 'Failed to fetch files' }, { status: 500 }) |
| 60 | + } |
| 61 | + |
| 62 | + // Build file tree structure |
| 63 | + const fileTree = buildFileTree(files || []) |
| 64 | + |
| 65 | + return NextResponse.json(fileTree) |
| 66 | + } catch (error) { |
| 67 | + console.error('Error in GET /api/files:', error) |
| 68 | + return NextResponse.json({ error: 'Internal server error' }, { status: 500 }) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +export async function POST(request: NextRequest) { |
| 73 | + try { |
| 74 | + const body = await request.json() |
| 75 | + const { path, isDirectory, content = '' } = body |
| 76 | + |
| 77 | + if (!path) { |
| 78 | + return NextResponse.json({ error: 'Path is required' }, { status: 400 }) |
| 79 | + } |
| 80 | + |
| 81 | + const supabase = createServerClient() |
| 82 | + |
| 83 | + // Get authenticated user from session |
| 84 | + const { data: { user }, error: authError } = await supabase.auth.getUser() |
| 85 | + |
| 86 | + if (authError || !user) { |
| 87 | + return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) |
| 88 | + } |
| 89 | + |
| 90 | + // Extract file name and parent path |
| 91 | + const pathParts = path.split('/') |
| 92 | + const name = pathParts[pathParts.length - 1] |
| 93 | + const parentPath = pathParts.length > 1 ? pathParts.slice(0, -1).join('/') : null |
| 94 | + |
| 95 | + // Calculate content size |
| 96 | + const sizeBytes = Buffer.byteLength(content, 'utf8') |
| 97 | + |
| 98 | + // Insert the new file |
| 99 | + const { data: file, error } = await supabase |
| 100 | + .from('workspace_files') |
| 101 | + .insert({ |
| 102 | + user_id: user.id, |
| 103 | + path, |
| 104 | + name, |
| 105 | + content, |
| 106 | + is_directory: isDirectory, |
| 107 | + parent_path: parentPath, |
| 108 | + size_bytes: sizeBytes, |
| 109 | + }) |
| 110 | + .select() |
| 111 | + .single() |
| 112 | + |
| 113 | + if (error) { |
| 114 | + console.error('Error creating workspace file:', error) |
| 115 | + return NextResponse.json({ error: 'Failed to create file' }, { status: 500 }) |
| 116 | + } |
| 117 | + |
| 118 | + return NextResponse.json({ success: true, file }) |
| 119 | + } catch (error) { |
| 120 | + console.error('Error in POST /api/files:', error) |
| 121 | + return NextResponse.json({ error: 'Internal server error' }, { status: 500 }) |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +export async function DELETE(request: NextRequest) { |
| 126 | + try { |
| 127 | + const body = await request.json() |
| 128 | + const { path } = body |
| 129 | + |
| 130 | + if (!path) { |
| 131 | + return NextResponse.json({ error: 'Path is required' }, { status: 400 }) |
| 132 | + } |
| 133 | + |
| 134 | + const supabase = createServerClient() |
| 135 | + |
| 136 | + // Get authenticated user from session |
| 137 | + const { data: { user }, error: authError } = await supabase.auth.getUser() |
| 138 | + |
| 139 | + if (authError || !user) { |
| 140 | + return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) |
| 141 | + } |
| 142 | + |
| 143 | + // Use LIKE to match all nested paths that start with this path |
| 144 | + const { error } = await supabase |
| 145 | + .from('workspace_files') |
| 146 | + .delete() |
| 147 | + .eq('user_id', user.id) |
| 148 | + .or(`path.eq.${path},path.like.${path}/%`) |
| 149 | + |
| 150 | + if (error) { |
| 151 | + console.error('Error deleting workspace file:', error) |
| 152 | + return NextResponse.json({ error: 'Failed to delete file' }, { status: 500 }) |
| 153 | + } |
| 154 | + |
| 155 | + return NextResponse.json({ success: true }) |
| 156 | + } catch (error) { |
| 157 | + console.error('Error in DELETE /api/files:', error) |
| 158 | + return NextResponse.json({ error: 'Internal server error' }, { status: 500 }) |
| 159 | + } |
| 160 | +} |
0 commit comments