Add result and job management APIs
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import { join } from "node:path";
|
||||
import { dataPath } from "../../deployPaths";
|
||||
import { jsonError, normalizeStem, securityHeaders, withAuth } from "../../security";
|
||||
import { isSafeStem, jsonError, normalizeStem, securityHeaders, withAuth } from "../../security";
|
||||
|
||||
const UPLOAD_PAGE_PATH = join(import.meta.dir, "upload.html");
|
||||
import { mkdir } from "node:fs/promises";
|
||||
import { mkdir, readdir } from "node:fs/promises";
|
||||
import { flattenTemplate, extractFields, buildOutput, getNested } from "./extractor";
|
||||
|
||||
const TEMPLATE_PATH =
|
||||
@@ -59,6 +59,60 @@ function extractScalarText(value: unknown): string | undefined {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function extractorDownloadUrl(stem: string, file: string): string {
|
||||
return `/downloads/extractor/${encodeURIComponent(stem)}/${encodeURIComponent(file)}`;
|
||||
}
|
||||
|
||||
async function listExtractorResults() {
|
||||
let entries: string[];
|
||||
try {
|
||||
entries = await readdir(OUTPUTS_DIR);
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
|
||||
const results = [];
|
||||
for (const stem of entries.toSorted()) {
|
||||
if (!isSafeStem(stem)) continue;
|
||||
const jsonFile = `${stem}.json`;
|
||||
const pdfFile = `${stem}.pdf`;
|
||||
const jsonPath = join(OUTPUTS_DIR, stem, jsonFile);
|
||||
const pdfPath = join(OUTPUTS_DIR, stem, pdfFile);
|
||||
if (!(await Bun.file(jsonPath).exists())) continue;
|
||||
results.push({
|
||||
stem,
|
||||
files: {
|
||||
json: (await Bun.file(jsonPath).exists()) ? jsonFile : undefined,
|
||||
pdf: (await Bun.file(pdfPath).exists()) ? pdfFile : undefined,
|
||||
},
|
||||
downloads: [
|
||||
{ label: "JSON herunterladen", url: extractorDownloadUrl(stem, jsonFile) },
|
||||
...((await Bun.file(pdfPath).exists()) ? [{ label: "PDF herunterladen", url: extractorDownloadUrl(stem, pdfFile) }] : []),
|
||||
],
|
||||
});
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
async function serveExtractorDownload(stem: string, file: string): Promise<Response> {
|
||||
if (!isSafeStem(stem) || (file !== `${stem}.json` && file !== `${stem}.pdf`)) {
|
||||
return new Response("Not found", { status: 404, headers: securityHeaders });
|
||||
}
|
||||
const path = join(OUTPUTS_DIR, stem, file);
|
||||
const output = Bun.file(path);
|
||||
if (!(await output.exists())) {
|
||||
return new Response("Not found", { status: 404, headers: securityHeaders });
|
||||
}
|
||||
return new Response(output, {
|
||||
headers: {
|
||||
...securityHeaders,
|
||||
"Content-Type": file.endsWith(".json") ? "application/json; charset=utf-8" : "application/pdf",
|
||||
"Cache-Control": "private, no-store",
|
||||
"Content-Disposition": `attachment; filename="${file.replaceAll('"', "")}"`,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export const routes = {
|
||||
"/extractor": {
|
||||
GET: withAuth(async () => new Response(Bun.file(UPLOAD_PAGE_PATH), {
|
||||
@@ -67,6 +121,41 @@ export const routes = {
|
||||
),
|
||||
},
|
||||
|
||||
"/api/extractor/results": {
|
||||
GET: withAuth(async () => Response.json({ results: await listExtractorResults() })),
|
||||
},
|
||||
|
||||
"/api/extractor/results/:stem": {
|
||||
GET: withAuth(async (req: Request) => {
|
||||
const stem = (req as Request & { params: Record<string, string | undefined> }).params.stem ?? "";
|
||||
if (!isSafeStem(stem)) {
|
||||
return jsonError("Invalid result id", 400);
|
||||
}
|
||||
const jsonPath = join(OUTPUTS_DIR, stem, `${stem}.json`);
|
||||
const pdfPath = join(OUTPUTS_DIR, stem, `${stem}.pdf`);
|
||||
const jsonFile = Bun.file(jsonPath);
|
||||
if (!(await jsonFile.exists())) {
|
||||
return Response.json({ error: "Extractor result not found" }, { status: 404 });
|
||||
}
|
||||
return Response.json({
|
||||
ok: true,
|
||||
stem,
|
||||
result: await jsonFile.json(),
|
||||
downloads: [
|
||||
{ label: "JSON herunterladen", url: extractorDownloadUrl(stem, `${stem}.json`) },
|
||||
...((await Bun.file(pdfPath).exists()) ? [{ label: "PDF herunterladen", url: extractorDownloadUrl(stem, `${stem}.pdf`) }] : []),
|
||||
],
|
||||
});
|
||||
}),
|
||||
},
|
||||
|
||||
"/downloads/extractor/:stem/:file": {
|
||||
GET: withAuth(async (req: Request) => {
|
||||
const params = (req as Request & { params: Record<string, string | undefined> }).params;
|
||||
return serveExtractorDownload(params.stem ?? "", params.file ?? "");
|
||||
}),
|
||||
},
|
||||
|
||||
"/extract": {
|
||||
POST: withAuth(async (req: Request) => {
|
||||
const contentLength = Number(req.headers.get("content-length") ?? 0);
|
||||
|
||||
Reference in New Issue
Block a user