summaryrefslogtreecommitdiff
path: root/frontend/app/api/delete/route.ts
blob: 31af119fe6f6bf45ba619de2de3552f3b5c5f955 (plain)
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
60
61
62
63
64
65
66
67
import { NextRequest, NextResponse } from 'next/server'
import { Auth_get_user } from '@/lib/auth'
import { Drive_remove } from '@/lib/drive_server'

// DELETE /api/delete - Delete files/directories
export async function DELETE(request: NextRequest) {
  try {
    // Check user authentication
    const user = await Auth_get_user()
    if (!user.isLoggedIn) {
      return NextResponse.json({ error: 'User not authenticated' }, { status: 401 })
    }

    // Parse request body to get paths to delete
    const body = await request.json()
    const { paths } = body

    if (!paths || !Array.isArray(paths) || paths.length === 0) {
      return NextResponse.json({ error: 'No paths provided for deletion' }, { status: 400 })
    }

    // Validate paths (basic sanitation)
    for (const path of paths) {
      if (typeof path !== 'string' || path.trim() === '') {
        return NextResponse.json({ error: 'Invalid path provided' }, { status: 400 })
      }
    }

    const results = []
    const errors = []

    // Delete each path using Drive_remove
    for (const path of paths) {
      try {
        await Drive_remove(path, user.email)
        results.push({ path, success: true })
      } catch (error) {
        console.error(`Failed to delete ${path}:`, error)
        errors.push({ 
          path, 
          error: error instanceof Error ? error.message : 'Unknown error'
        })
      }
    }

    // Return results
    const response = {
      success: errors.length === 0,
      message: errors.length === 0 
        ? `Successfully deleted ${results.length} item(s)`
        : `Deleted ${results.length} item(s), failed to delete ${errors.length} item(s)`,
      results,
      errors
    }

    return NextResponse.json(response, { 
      status: errors.length === 0 ? 200 : 207 // 207 Multi-Status for partial success
    })

  } catch (error) {
    console.error('Delete API error:', error)
    return NextResponse.json(
      { error: error instanceof Error ? error.message : 'Internal server error' },
      { status: 500 }
    )
  }
}