fix(rgi): fast download with fileVisibility gate + clear error message
Download route simplified: 1. fileVisibility check — if 404, returns "indisponibil" + eTerra URL 2. Single download pattern (the one that works) When document not available server-side, response includes direct eTerra URL as fallback. No more 7 pattern attempts = much faster. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -295,30 +295,21 @@ function IssuedDocsPanel({
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex items-center gap-1 shrink-0">
|
<div className="flex items-center gap-1 shrink-0">
|
||||||
|
{/* Server-side download (works when fileVisibility returns OK) */}
|
||||||
<Button
|
<Button
|
||||||
size="sm"
|
size="sm"
|
||||||
variant="outline"
|
variant="outline"
|
||||||
className="gap-1"
|
className="gap-1"
|
||||||
onClick={() => {
|
asChild
|
||||||
// Open eTerra directly — user's browser session has download permission
|
>
|
||||||
// First open confirmOnView, then download
|
<a
|
||||||
const base = "https://eterra.ancpi.ro/eterra/api";
|
href={`/api/eterra/rgi/download-doc?workspaceId=${doc.workspaceId || workspaceId}&applicationId=${doc.applicationId || applicationPk}&documentPk=${doc.documentPk}&documentTypeId=${doc.documentTypeId}`}
|
||||||
const wid = doc.workspaceId || workspaceId;
|
target="_blank"
|
||||||
const appId = doc.applicationId || applicationPk;
|
rel="noopener noreferrer"
|
||||||
const dpk = doc.documentPk;
|
|
||||||
// Open confirm in hidden iframe, then download
|
|
||||||
const iframe = document.createElement("iframe");
|
|
||||||
iframe.style.display = "none";
|
|
||||||
iframe.src = `${base}/rgi/appdetail/issueddocs/confirmOnView/${wid}/${appId}/${dpk}`;
|
|
||||||
document.body.appendChild(iframe);
|
|
||||||
setTimeout(() => {
|
|
||||||
window.open(`${base}/rgi/appdetail/loadDocument/downloadFile/${wid}/${dpk}`, "_blank");
|
|
||||||
iframe.remove();
|
|
||||||
}, 500);
|
|
||||||
}}
|
|
||||||
>
|
>
|
||||||
<Download className="h-3.5 w-3.5" />
|
<Download className="h-3.5 w-3.5" />
|
||||||
Descarca
|
Descarca
|
||||||
|
</a>
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -4,17 +4,24 @@ import { EterraClient } from "@/modules/parcel-sync/services/eterra-client";
|
|||||||
export const runtime = "nodejs";
|
export const runtime = "nodejs";
|
||||||
export const dynamic = "force-dynamic";
|
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
|
||||||
|
*/
|
||||||
export async function GET(req: NextRequest) {
|
export async function GET(req: NextRequest) {
|
||||||
try {
|
try {
|
||||||
const workspaceId = req.nextUrl.searchParams.get("workspaceId");
|
const workspaceId = req.nextUrl.searchParams.get("workspaceId");
|
||||||
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 debug = req.nextUrl.searchParams.get("debug") === "1";
|
const checkOnly = req.nextUrl.searchParams.get("check") === "1";
|
||||||
|
|
||||||
if (!workspaceId || !applicationId || !documentPk) {
|
if (!workspaceId || !applicationId || !documentPk) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: "workspaceId, applicationId and documentPk are required" },
|
{ error: "workspaceId, applicationId and documentPk required" },
|
||||||
{ status: 400 },
|
{ status: 400 },
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -26,93 +33,36 @@ export async function GET(req: NextRequest) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const client = await EterraClient.create(username, password);
|
const client = await EterraClient.create(username, password);
|
||||||
const log: { step: string; result: unknown }[] = [];
|
|
||||||
|
|
||||||
// Step 1: fileVisibility (GET)
|
// Step 1: fileVisibility — check if document is downloadable
|
||||||
if (documentTypeId) {
|
if (documentTypeId) {
|
||||||
try {
|
try {
|
||||||
const r = await client.rgiGet(
|
await client.rgiGet(
|
||||||
`rgi/appdetail/issueddocs/fileVisibility/${workspaceId}/${applicationId}/${documentTypeId}`,
|
`rgi/appdetail/issueddocs/fileVisibility/${workspaceId}/${applicationId}/${documentTypeId}`,
|
||||||
);
|
);
|
||||||
log.push({ step: "fileVisibility(GET,documentTypeId)", result: r });
|
} catch {
|
||||||
} catch (e) {
|
// fileVisibility failed — document not available for server-side download
|
||||||
log.push({ step: "fileVisibility(GET,documentTypeId)", result: { error: String(e instanceof Error ? e.message : e) } });
|
return NextResponse.json(
|
||||||
}
|
{
|
||||||
}
|
error: "Document indisponibil pentru download server-side",
|
||||||
|
available: false,
|
||||||
// Step 1b: fileVisibility with documentPk
|
eterraUrl: `https://eterra.ancpi.ro/eterra/api/rgi/appdetail/loadDocument/downloadFile/${workspaceId}/${documentPk}`,
|
||||||
try {
|
},
|
||||||
const r = await client.rgiGet(
|
{ status: 403 },
|
||||||
`rgi/appdetail/issueddocs/fileVisibility/${workspaceId}/${applicationId}/${documentPk}`,
|
|
||||||
);
|
);
|
||||||
log.push({ step: "fileVisibility(GET,documentPk)", result: r });
|
}
|
||||||
} catch (e) {
|
|
||||||
log.push({ step: "fileVisibility(GET,documentPk)", result: { error: String(e instanceof Error ? e.message : e) } });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 2: confirmOnView (GET)
|
// Check-only mode: just verify availability
|
||||||
try {
|
if (checkOnly) {
|
||||||
const r = await client.rgiGet(
|
return NextResponse.json({ available: true });
|
||||||
`rgi/appdetail/issueddocs/confirmOnView/${workspaceId}/${applicationId}/${documentPk}`,
|
|
||||||
);
|
|
||||||
log.push({ step: "confirmOnView(GET)", result: r });
|
|
||||||
} catch (e) {
|
|
||||||
log.push({ step: "confirmOnView(GET)", result: { error: String(e instanceof Error ? e.message : e) } });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 2b: confirmOnView (POST)
|
// Step 2: Download
|
||||||
try {
|
const { data, contentType, filename } = await client.rgiDownload(
|
||||||
const r = await client.rgiPost(
|
|
||||||
`rgi/appdetail/issueddocs/confirmOnView/${workspaceId}/${applicationId}/${documentPk}`,
|
|
||||||
);
|
|
||||||
log.push({ step: "confirmOnView(POST)", result: r });
|
|
||||||
} catch (e) {
|
|
||||||
log.push({ step: "confirmOnView(POST)", result: { error: String(e instanceof Error ? e.message : e) } });
|
|
||||||
}
|
|
||||||
|
|
||||||
if (debug) {
|
|
||||||
// Try ALL download patterns
|
|
||||||
const patterns = [
|
|
||||||
`rgi/appdetail/loadDocument/downloadFile/${workspaceId}/${documentPk}`,
|
`rgi/appdetail/loadDocument/downloadFile/${workspaceId}/${documentPk}`,
|
||||||
`rgi/appdetail/loadDocument/downloadFile/${workspaceId}/${applicationId}/${documentPk}`,
|
);
|
||||||
`rgi/appdetail/issueddocs/downloadFile/${workspaceId}/${documentPk}`,
|
|
||||||
`rgi/appdetail/issueddocs/downloadFile/${workspaceId}/${applicationId}/${documentPk}`,
|
|
||||||
`rgi/appdetail/issueddocs/download/${workspaceId}/${documentPk}`,
|
|
||||||
`rgi/appdetail/issueddocs/download/${workspaceId}/${applicationId}/${documentPk}`,
|
|
||||||
`appDetail/loadDocument/downloadFile/${workspaceId}/${documentPk}`,
|
|
||||||
];
|
|
||||||
|
|
||||||
for (let i = 0; i < patterns.length; i++) {
|
|
||||||
try {
|
|
||||||
const { data, contentType, filename } = await client.rgiDownload(patterns[i]!);
|
|
||||||
log.push({
|
|
||||||
step: `download[${i}]`,
|
|
||||||
result: { success: true, contentType, filename, size: data.length, url: patterns[i] },
|
|
||||||
});
|
|
||||||
// If we found a working one, stop trying
|
|
||||||
break;
|
|
||||||
} catch (e) {
|
|
||||||
log.push({
|
|
||||||
step: `download[${i}]`,
|
|
||||||
result: { success: false, error: String(e instanceof Error ? e.message : e), url: patterns[i] },
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return NextResponse.json({ log });
|
|
||||||
}
|
|
||||||
|
|
||||||
// Non-debug: try download patterns
|
|
||||||
const patterns = [
|
|
||||||
`rgi/appdetail/loadDocument/downloadFile/${workspaceId}/${documentPk}`,
|
|
||||||
`rgi/appdetail/loadDocument/downloadFile/${workspaceId}/${applicationId}/${documentPk}`,
|
|
||||||
`appDetail/loadDocument/downloadFile/${workspaceId}/${documentPk}`,
|
|
||||||
];
|
|
||||||
|
|
||||||
for (const pattern of patterns) {
|
|
||||||
try {
|
|
||||||
const { data, contentType, filename } = await client.rgiDownload(pattern);
|
|
||||||
if (data.length > 0) {
|
|
||||||
return new NextResponse(new Uint8Array(data), {
|
return new NextResponse(new Uint8Array(data), {
|
||||||
status: 200,
|
status: 200,
|
||||||
headers: {
|
headers: {
|
||||||
@@ -121,13 +71,6 @@ export async function GET(req: NextRequest) {
|
|||||||
"Content-Length": String(data.length),
|
"Content-Length": String(data.length),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// next pattern
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return NextResponse.json({ error: "Download failed", log }, { status: 500 });
|
|
||||||
} 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 });
|
||||||
|
|||||||
Reference in New Issue
Block a user