← back to Letsbegin

app/api/files/route.ts

29 lines

import { NextRequest, NextResponse } from 'next/server'
import * as fs from 'fs'

export async function GET(request: NextRequest) {
  try {
    const { searchParams } = new URL(request.url)
    const filePath = searchParams.get('path')

    if (!filePath) {
      return NextResponse.json({ error: 'Path required' }, { status: 400 })
    }

    // Security: only allow reading from Projects directory
    if (!filePath.startsWith('/root/Projects/') && !filePath.startsWith('/tmp/')) {
      return NextResponse.json({ error: 'Access denied' }, { status: 403 })
    }

    if (!fs.existsSync(filePath)) {
      return NextResponse.json({ error: 'File not found' }, { status: 404 })
    }

    const content = fs.readFileSync(filePath, 'utf-8')
    return NextResponse.json({ content, path: filePath })
  } catch (error: any) {
    console.error('File read error:', error)
    return NextResponse.json({ error: error.message }, { status: 500 })
  }
}