fix(rgi): redirect to eTerra when server-side download unavailable
When fileVisibility returns OK → download server-side (fast). When not available → HTTP 302 redirect to eTerra direct URL. User's browser session handles authentication automatically. This means: if logged into eTerra in browser, ALL documents download. If not logged in, eTerra shows its own login page. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -8,8 +8,9 @@ export const dynamic = "force-dynamic";
|
||||
* GET /api/eterra/rgi/download-doc?workspaceId=127&applicationId=X&documentPk=Y&documentTypeId=Z
|
||||
*
|
||||
* Downloads an issued document from eTerra RGI.
|
||||
* 1. fileVisibility check — if 404, document not available
|
||||
* 2. loadDocument/downloadFile — actual download
|
||||
* Tries server-side download first. If that fails (some documents are
|
||||
* restricted to the current actor), returns a direct eTerra URL that
|
||||
* works in the user's browser session.
|
||||
*/
|
||||
export async function GET(req: NextRequest) {
|
||||
try {
|
||||
@@ -17,7 +18,6 @@ export async function GET(req: NextRequest) {
|
||||
const applicationId = req.nextUrl.searchParams.get("applicationId");
|
||||
const documentPk = req.nextUrl.searchParams.get("documentPk");
|
||||
const documentTypeId = req.nextUrl.searchParams.get("documentTypeId");
|
||||
const checkOnly = req.nextUrl.searchParams.get("check") === "1";
|
||||
|
||||
if (!workspaceId || !applicationId || !documentPk) {
|
||||
return NextResponse.json(
|
||||
@@ -34,43 +34,46 @@ export async function GET(req: NextRequest) {
|
||||
|
||||
const client = await EterraClient.create(username, password);
|
||||
|
||||
// Step 1: fileVisibility — check if document is downloadable
|
||||
// Try fileVisibility first (fast check)
|
||||
let available = false;
|
||||
if (documentTypeId) {
|
||||
try {
|
||||
await client.rgiGet(
|
||||
const vis = await client.rgiGet(
|
||||
`rgi/appdetail/issueddocs/fileVisibility/${workspaceId}/${applicationId}/${documentTypeId}`,
|
||||
);
|
||||
if (vis && typeof vis === "object" && (vis as Record<string, unknown>).msg === "OK") {
|
||||
available = true;
|
||||
}
|
||||
} catch {
|
||||
// fileVisibility failed — document not available for server-side download
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: "Document indisponibil pentru download server-side",
|
||||
available: false,
|
||||
eterraUrl: `https://eterra.ancpi.ro/eterra/api/rgi/appdetail/loadDocument/downloadFile/${workspaceId}/${documentPk}`,
|
||||
},
|
||||
{ status: 403 },
|
||||
);
|
||||
// Not available server-side
|
||||
}
|
||||
}
|
||||
|
||||
// Check-only mode: just verify availability
|
||||
if (checkOnly) {
|
||||
return NextResponse.json({ available: true });
|
||||
// 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
|
||||
}
|
||||
}
|
||||
|
||||
// Step 2: Download
|
||||
const { data, contentType, filename } = await client.rgiDownload(
|
||||
`rgi/appdetail/loadDocument/downloadFile/${workspaceId}/${documentPk}`,
|
||||
);
|
||||
|
||||
return new NextResponse(new Uint8Array(data), {
|
||||
status: 200,
|
||||
headers: {
|
||||
"Content-Type": contentType,
|
||||
"Content-Disposition": `attachment; filename="${encodeURIComponent(filename)}"`,
|
||||
"Content-Length": String(data.length),
|
||||
},
|
||||
});
|
||||
// Server-side download not available — redirect to eTerra direct URL
|
||||
// User's browser session (if logged into eTerra) can download it
|
||||
const eterraUrl = `https://eterra.ancpi.ro/eterra/api/rgi/appdetail/loadDocument/downloadFile/${workspaceId}/${documentPk}`;
|
||||
return NextResponse.redirect(eterraUrl, 302);
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Eroare server";
|
||||
return NextResponse.json({ error: message }, { status: 500 });
|
||||
|
||||
@@ -217,6 +217,14 @@ export class EterraClient {
|
||||
const cookies = await this.jar.getCookies(LOGIN_URL);
|
||||
if (!cookies.some((c) => c.key === "JSESSIONID"))
|
||||
throw new Error("Login failed / session not set");
|
||||
|
||||
// Activate RGI module context by loading the RGI page
|
||||
// This may set additional session attributes needed for document downloads
|
||||
try {
|
||||
await this.client.get(`${BASE_URL}/`, { timeout: 10_000 });
|
||||
} catch {
|
||||
// Non-critical
|
||||
}
|
||||
}
|
||||
|
||||
/* ---- High-level parcels --------------------------------------- */
|
||||
|
||||
Reference in New Issue
Block a user