import { NextRequest, NextResponse } from 'next/server'; import { getServerSession } from 'next-auth'; import { authOptions } from '@/app/api/auth/options'; import { S3Client, GetObjectCommand } from '@aws-sdk/client-s3'; import { NoSuchKey } from '@aws-sdk/client-s3'; import { logger } from '@/lib/logger'; // Initialize S3 client const s3Client = new S3Client({ region: 'us-east-1', endpoint: 'https://dome-api.slm-lab.net', credentials: { accessKeyId: '4aBT4CMb7JIMMyUtp4Pl', secretAccessKey: 'HGn39XhCIlqOjmDVzRK9MED2Fci2rYvDDgbLFElg' }, forcePathStyle: true // Required for MinIO }); // This endpoint serves mission images from Minio using the server's credentials export async function GET( request: NextRequest, { params }: { params: Promise<{ path: string[] }> } ) { try { const { path: pathSegments } = await params; if (!pathSegments || pathSegments.length === 0) { logger.error('No path segments provided'); return new NextResponse('Path is required', { status: 400 }); } // Reconstruct the full path from path segments const filePath = pathSegments.join('/'); logger.debug('Fetching mission image', { originalPath: filePath, segments: pathSegments }); // Remove the missions/ prefix from the URL path since the file is already in the missions bucket const minioPath = filePath.replace(/^missions\//, ''); logger.debug('Full Minio path', { minioPath, bucket: 'missions' }); const command = new GetObjectCommand({ Bucket: 'missions', Key: minioPath, }); try { const response = await s3Client.send(command); if (!response.Body) { logger.error('File not found in Minio', { path: filePath, minioPath, bucket: 'missions' }); return new NextResponse('File not found', { status: 404 }); } // Set appropriate content type and cache control const contentType = response.ContentType || 'image/png'; // Default to image/png if not specified const headers = new Headers(); headers.set('Content-Type', contentType); headers.set('Cache-Control', 'public, max-age=31536000'); logger.debug('Serving image', { path: filePath, minioPath, contentType, contentLength: response.ContentLength }); return new NextResponse(response.Body as any, { headers }); } catch (error) { logger.error('Error fetching file from Minio', { error: error instanceof Error ? error.message : String(error), path: filePath, minioPath, bucket: 'missions', errorType: error instanceof NoSuchKey ? 'NoSuchKey' : 'Unknown' }); if (error instanceof NoSuchKey) { return new NextResponse('File not found', { status: 404 }); } return new NextResponse('Internal Server Error', { status: 500 }); } } catch (error) { logger.error('Error in image serving', { error: error instanceof Error ? error.message : String(error) }); return new NextResponse('Internal Server Error', { status: 500 }); } }