File size: 1,394 Bytes
33d698c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { NextRequest, NextResponse } from 'next/server'
import fs from 'fs'
import path from 'path'

const DATA_DIR = process.env.NODE_ENV === 'production' && fs.existsSync('/data')
  ? '/data'
  : path.join(process.cwd(), 'data')
const FLUTTER_APPS_DIR = path.join(DATA_DIR, 'documents', 'flutter_apps')

export async function GET(request: NextRequest) {
  try {
    const searchParams = request.nextUrl.searchParams
    const name = searchParams.get('name')

    if (!name) {
      return NextResponse.json(
        { success: false, error: 'App name is required' },
        { status: 400 }
      )
    }

    // Sanitize name
    const safeName = name.replace(/[^a-zA-Z0-9_-]/g, '_').toLowerCase()
    const fileName = `${safeName}.flutter.json`
    const filePath = path.join(FLUTTER_APPS_DIR, fileName)

    if (!fs.existsSync(filePath)) {
      return NextResponse.json(
        { success: false, error: `Flutter app '${safeName}' not found` },
        { status: 404 }
      )
    }

    const fileContent = fs.readFileSync(filePath, 'utf-8')
    const appData = JSON.parse(fileContent)

    return NextResponse.json({
      success: true,
      appName: safeName,
      appData
    })
  } catch (error) {
    console.error('Error getting Flutter app:', error)
    return NextResponse.json(
      { success: false, error: 'Failed to get Flutter app' },
      { status: 500 }
    )
  }
}