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
|
* GET /api/eterra/rgi/download-doc?workspaceId=127&applicationId=X&documentPk=Y&documentTypeId=Z
|
||||||
*
|
*
|
||||||
* Downloads an issued document from eTerra RGI.
|
* Downloads an issued document from eTerra RGI.
|
||||||
* 1. fileVisibility check — if 404, document not available
|
* Tries server-side download first. If that fails (some documents are
|
||||||
* 2. loadDocument/downloadFile — actual download
|
* restricted to the current actor), returns a direct eTerra URL that
|
||||||
|
* works in the user's browser session.
|
||||||
*/
|
*/
|
||||||
export async function GET(req: NextRequest) {
|
export async function GET(req: NextRequest) {
|
||||||
try {
|
try {
|
||||||
@@ -17,7 +18,6 @@ export async function GET(req: NextRequest) {
|
|||||||
const applicationId = req.nextUrl.searchParams.get("applicationId");
|
const applicationId = req.nextUrl.searchParams.get("applicationId");
|
||||||
const documentPk = req.nextUrl.searchParams.get("documentPk");
|
const documentPk = req.nextUrl.searchParams.get("documentPk");
|
||||||
const documentTypeId = req.nextUrl.searchParams.get("documentTypeId");
|
const documentTypeId = req.nextUrl.searchParams.get("documentTypeId");
|
||||||
const checkOnly = req.nextUrl.searchParams.get("check") === "1";
|
|
||||||
|
|
||||||
if (!workspaceId || !applicationId || !documentPk) {
|
if (!workspaceId || !applicationId || !documentPk) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
@@ -34,35 +34,28 @@ export async function GET(req: NextRequest) {
|
|||||||
|
|
||||||
const client = await EterraClient.create(username, password);
|
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) {
|
if (documentTypeId) {
|
||||||
try {
|
try {
|
||||||
await client.rgiGet(
|
const vis = await client.rgiGet(
|
||||||
`rgi/appdetail/issueddocs/fileVisibility/${workspaceId}/${applicationId}/${documentTypeId}`,
|
`rgi/appdetail/issueddocs/fileVisibility/${workspaceId}/${applicationId}/${documentTypeId}`,
|
||||||
);
|
);
|
||||||
|
if (vis && typeof vis === "object" && (vis as Record<string, unknown>).msg === "OK") {
|
||||||
|
available = true;
|
||||||
|
}
|
||||||
} catch {
|
} catch {
|
||||||
// fileVisibility failed — document not available for server-side download
|
// Not available server-side
|
||||||
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 },
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check-only mode: just verify availability
|
// If fileVisibility passed, try download
|
||||||
if (checkOnly) {
|
if (available) {
|
||||||
return NextResponse.json({ available: true });
|
try {
|
||||||
}
|
|
||||||
|
|
||||||
// Step 2: Download
|
|
||||||
const { data, contentType, filename } = await client.rgiDownload(
|
const { data, contentType, filename } = await client.rgiDownload(
|
||||||
`rgi/appdetail/loadDocument/downloadFile/${workspaceId}/${documentPk}`,
|
`rgi/appdetail/loadDocument/downloadFile/${workspaceId}/${documentPk}`,
|
||||||
);
|
);
|
||||||
|
if (data.length > 0) {
|
||||||
return new NextResponse(new Uint8Array(data), {
|
return new NextResponse(new Uint8Array(data), {
|
||||||
status: 200,
|
status: 200,
|
||||||
headers: {
|
headers: {
|
||||||
@@ -71,6 +64,16 @@ export async function GET(req: NextRequest) {
|
|||||||
"Content-Length": String(data.length),
|
"Content-Length": String(data.length),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// Fall through to redirect
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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) {
|
} catch (error) {
|
||||||
const message = error instanceof Error ? error.message : "Eroare server";
|
const message = error instanceof Error ? error.message : "Eroare server";
|
||||||
return NextResponse.json({ error: message }, { status: 500 });
|
return NextResponse.json({ error: message }, { status: 500 });
|
||||||
|
|||||||
@@ -217,6 +217,14 @@ export class EterraClient {
|
|||||||
const cookies = await this.jar.getCookies(LOGIN_URL);
|
const cookies = await this.jar.getCookies(LOGIN_URL);
|
||||||
if (!cookies.some((c) => c.key === "JSESSIONID"))
|
if (!cookies.some((c) => c.key === "JSESSIONID"))
|
||||||
throw new Error("Login failed / session not set");
|
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 --------------------------------------- */
|
/* ---- High-level parcels --------------------------------------- */
|
||||||
|
|||||||
Reference in New Issue
Block a user