diff --git a/src/app/api/eterra/rgi/download-doc/route.ts b/src/app/api/eterra/rgi/download-doc/route.ts index 7179e98..11baff5 100644 --- a/src/app/api/eterra/rgi/download-doc/route.ts +++ b/src/app/api/eterra/rgi/download-doc/route.ts @@ -34,7 +34,26 @@ export async function GET(req: NextRequest) { const client = await EterraClient.create(username, password); - // Try fileVisibility first (fast check) + // Step 0: Set application context (like the web UI does) + // This call sets session-level attributes required for document access + try { + await client.rgiGet( + `appDetail/verifyCurrentActorAuthenticated/${applicationId}/${workspaceId}`, + ); + } catch { + // Non-critical + } + + // Also load application details (sets more session context) + try { + await client.rgiPost( + `rgi/appdetail/details?applicationid=${applicationId}`, + ); + } catch { + // Non-critical + } + + // Try fileVisibility let available = false; if (documentTypeId) { try { @@ -45,29 +64,27 @@ export async function GET(req: NextRequest) { available = true; } } catch { - // Not available server-side + // Not available — will try direct download anyway } } - // If fileVisibility passed, try download - if (available) { - try { - const { data, contentType, filename } = await client.rgiDownload( - `rgi/appdetail/loadDocument/downloadFile/${workspaceId}/${documentPk}`, - ); - if (data.length > 0) { - return new NextResponse(new Uint8Array(data), { - status: 200, - headers: { - "Content-Type": contentType, - "Content-Disposition": `attachment; filename="${encodeURIComponent(filename)}"`, - "Content-Length": String(data.length), - }, - }); - } - } catch { - // Fall through to redirect + // Try download (even if fileVisibility failed — context might be enough) + try { + const { data, contentType, filename } = await client.rgiDownload( + `rgi/appdetail/loadDocument/downloadFile/${workspaceId}/${documentPk}`, + ); + if (data.length > 0) { + return new NextResponse(new Uint8Array(data), { + status: 200, + headers: { + "Content-Type": contentType, + "Content-Disposition": `attachment; filename="${encodeURIComponent(filename)}"`, + "Content-Length": String(data.length), + }, + }); } + } catch { + // Fall through to redirect } // Server-side download not available — redirect to eTerra direct URL