fix(rgi): set application context before download attempt

Before downloading, now calls:
1. verifyCurrentActorAuthenticated — sets actor context in session
2. appdetail/details — loads application context

Then tries download regardless of fileVisibility result.
The session context might be what enables downloads that previously
returned 404.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
AI Assistant
2026-03-24 22:50:27 +02:00
parent 4beac959c8
commit 3614c2fc4a
+37 -20
View File
@@ -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