All files / web/src/app/api/vision-training/boundary-samples/image route.ts

0% Statements 0/71
0% Branches 0/1
0% Functions 0/1
0% Lines 0/71

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 68 69 70 71 72                                                                                                                                               
import fs from 'fs'
import path from 'path'
import { withAuth } from '@/lib/auth/withAuth'

// Force dynamic rendering
export const dynamic = 'force-dynamic'

const BOUNDARY_DETECTOR_DIR = path.join(process.cwd(), 'data/vision-training/boundary-frames')

/**
 * GET /api/vision-training/boundary-samples/image
 *
 * Serve a boundary sample image.
 *
 * Query params:
 * - deviceId: Device directory (default: "default")
 * - baseName: Base filename (without extension)
 */
export const GET = withAuth(
  async (request) => {
    try {
      const searchParams = request.nextUrl.searchParams
      const deviceId = searchParams.get('deviceId') || 'default'
      const baseName = searchParams.get('baseName')

      if (!baseName) {
        return new Response('Missing baseName', { status: 400 })
      }

      // Validate baseName to prevent path traversal
      if (baseName.includes('/') || baseName.includes('\\') || baseName.includes('..')) {
        return new Response('Invalid baseName', { status: 400 })
      }

      // Validate deviceId to prevent path traversal
      if (deviceId.includes('/') || deviceId.includes('\\') || deviceId.includes('..')) {
        return new Response('Invalid deviceId', { status: 400 })
      }

      // Try PNG first, then JPG (passive captures from phone are JPEG)
      const pngPath = path.join(BOUNDARY_DETECTOR_DIR, deviceId, `${baseName}.png`)
      const jpgPath = path.join(BOUNDARY_DETECTOR_DIR, deviceId, `${baseName}.jpg`)

      let imagePath: string
      let contentType: string

      if (fs.existsSync(pngPath)) {
        imagePath = pngPath
        contentType = 'image/png'
      } else if (fs.existsSync(jpgPath)) {
        imagePath = jpgPath
        contentType = 'image/jpeg'
      } else {
        return new Response('Image not found', { status: 404 })
      }

      const imageBuffer = fs.readFileSync(imagePath)

      return new Response(imageBuffer, {
        headers: {
          'Content-Type': contentType,
          'Cache-Control': 'public, max-age=3600',
        },
      })
    } catch (error) {
      console.error('[boundary-samples/image] Error:', error)
      return new Response('Failed to serve image', { status: 500 })
    }
  },
  { role: 'admin' }
)