import { NextRequest, NextResponse } from 'next/server'; import fs from 'fs'; import path from 'path'; export async function GET( request: NextRequest, context: { params: Promise<{ filename: string }> } ) { const { filename } = await context.params; // Define potential paths to the backend uploads directory // We need to handle different CWD contexts (app root vs workspace root) const potentialPaths = [ path.join(process.cwd(), '..', 'api', 'uploads', filename), // If CWD is apps/web path.join(process.cwd(), 'apps', 'api', 'uploads', filename), // If CWD is workspace root path.resolve(process.cwd(), '../../apps/api/uploads', filename), // fallback 'c:/ignosidev/Igcrm/apps/api/uploads/' + filename // Absolute path fallback ]; let filePath = ''; for (const p of potentialPaths) { // console.log('Checking path:', p); if (fs.existsSync(p)) { filePath = p; console.log('Found PDF at:', filePath); break; } } if (!filePath) { console.error(`PDF not found. Filename: ${filename}. Searched in:`, potentialPaths); return new NextResponse('File not found', { status: 404 }); } try { const fileBuffer = fs.readFileSync(filePath); return new NextResponse(fileBuffer, { headers: { 'Content-Type': 'application/pdf', 'Content-Disposition': `inline; filename="${filename}"`, }, }); } catch (error) { console.error('Error reading PDF:', error); return new NextResponse('Internal Server Error', { status: 500 }); } }