เพิ่มทางเข้าเอกสารสำหรับ e-Court ที่ไม่มี URL ให้ดึง — POST /api/note/files/stage (x-api-key DOCVER_API_KEY) ส่งเนื้อไฟล์ PDF/รูปเป็น base64 ระบบเก็บใน RAM staging (TTL sliding 8 ชม. + LRU + cap 100MB/ไฟล์ 256MB รวม — ห้ามเขียน disk/DB/MinIO แม้แต่ไบต์เดียวตามโจทย์) ส่วน annotation/bookmark เก็บถาวรตามระบบเดิม: - lib/staged-docs.ts: Map บน globalThis + sentinel "staged:<uuid>.<ext>" ใน pdf_url (ext จาก magic bytes — ไม่ต้อง migration) + hasStagedDoc แยกจาก getStagedDoc - lib/ecourt-fid.ts: external_id เก็บ HMAC-SHA256(pepper, fid) แทน fid ดิบ (env FID_HASH_PEPPER fail-closed) + แปลง row เก่าด้วย npm run migrate:ecourt-fid - route stage: อ่าน body แบบ stream นับไบต์ + idle 60 วิ/total 10 นาที = 408 + concurrency default 1 = 429 + base64 strict + magic bytes กำหนด ext เอง - registerStagedFile: ลำดับกัน race 23505 — stage ใต้ id ก่อน insert โดนชนแล้ว re-stage ใต้ id ผู้ชนะ + try/finally คืน RAM ผู้แพ้ทุกทางออก - ทางอ่าน 4 จุด: /api/note/pdf + export route (410 gate ไทย), resolvePublicPdfPath guard PdfPathError, PDFViewer โหลดผ่าน route + objectURL (gate ครบทุกชนิดไฟล์) - session route (D5): ลิงก์ fid-only ตัดสิน staging ตั้งแต่แลก token — มี bytes ได้ fileId / หมดอายุ fileError "พ้นช่วงจัดเตรียม" / token ออกทุกกรณี - Swagger UI: endpoint ใหม่ + คู่มือ retry (408/429/5xx ยิงซ้ำได้ stage ซ้ำปลอดภัย) - MockStageForm บน /note/dev ทดสอบ 3 ขั้นในหน้าเดียว (ต้องถือ API key จริง) Verify ครบตาม docs/PLAN-STAGED.md: tsc สะอาด + vitest 59 เคสใหม่ผ่าน + E2E browser จริง (textLayer ไทยเรียงถูก / export ฝัง annotation / restart ได้ fileError ตั้งแต่ bootstrap / RAM 100MB: peak WS +580MB commit จริง +92MB) — ผ่าน code review รอบ 7 แก้ครบทุก finding Co-Authored-By: Claude Code <noreply@anthropic.com>
84 lines
4.4 KiB
TypeScript
84 lines
4.4 KiB
TypeScript
import { eq } from "drizzle-orm";
|
|
|
|
import { db } from "@/db";
|
|
import { files } from "@/db/schema";
|
|
import { fixPdfActualText } from "@/lib/pdf-actual-text";
|
|
import { fetchSourceBytes, isRemoteSource, RemoteFileError } from "@/lib/remote-file";
|
|
import { getStagedDoc, isStagedSource } from "@/lib/staged-docs";
|
|
import { PdfPathError, readPublicPdf } from "@/lib/server-pdf";
|
|
import { fileKindFromUrl, imageContentTypeFromUrl } from "@/utils/file-kind";
|
|
|
|
// ให้ viewer โหลด PDF ผ่านทางนี้แทนไฟล์ดิบใน public/ — ซ่อม ToUnicode จาก ActualText
|
|
// ของ PDF ไทยที่ cmap ของฟอนต์เพี้ยน (สกัดข้อความได้ control char ขยะแทนสระ/วรรณยุกต์)
|
|
// **แก้บนสำเนาในหน่วยความจำเท่านั้น** ไฟล์ต้นฉบับไม่ถูกเขียน (invariant #1)
|
|
// ไฟล์ปกติไม่มี ActualText = ได้ bytes เดิมกลับมาไม่มี cost การแปลง
|
|
|
|
const UUID_RE =
|
|
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
|
|
export async function GET(req: Request) {
|
|
const url = new URL(req.url);
|
|
const fileId = url.searchParams.get("fileId") ?? "";
|
|
if (!UUID_RE.test(fileId)) {
|
|
return new Response("fileId ไม่ถูกต้อง", { status: 400 });
|
|
}
|
|
|
|
const [file] = await db.select().from(files).where(eq(files.id, fileId));
|
|
if (!file) return new Response("ไม่พบไฟล์นี้", { status: 404 });
|
|
|
|
// Guard path traversal: pdfUrl จาก DB ต้อง resolve แล้วอยู่ใน public/ เท่านั้น (§5)
|
|
// N11: pdf_url เป็น sentinel staged: → bytes จาก RAM (ไม่มีไฟล์บนดิสก์/URL ให้ดึง) —
|
|
// หมดอายุ/ถูก evict/ext ไม่ตรง = 410 gate ให้ผู้ใช้กลับ e-Court กดลิงก์ใหม่
|
|
// (annotation/bookmark ใน DB ยังอยู่ครบ) · Buffer ที่คืนเป็น reference ของ entry —
|
|
// ปลอดภัยต่อการอ่านพร้อมกันหลาย request เพราะ fixPdfActualText/pdf-lib สร้างชุด
|
|
// bytes ใหม่เอง ไม่ mutate ต้นทาง (ตรวจแล้ว lib/pdf-actual-text.ts)
|
|
let srcBytes: Buffer;
|
|
if (isStagedSource(file.pdfUrl)) {
|
|
const staged = getStagedDoc(file.pdfUrl);
|
|
if (staged === null) {
|
|
return new Response(
|
|
JSON.stringify({ error: "เอกสารพ้นช่วงจัดเตรียม — กลับ e-Court กดลิงก์ใหม่" }),
|
|
{
|
|
status: 410,
|
|
headers: { "content-type": "application/json; charset=utf-8" },
|
|
},
|
|
);
|
|
}
|
|
srcBytes = staged;
|
|
} else if (isRemoteSource(file.pdfUrl)) {
|
|
try {
|
|
srcBytes = await fetchSourceBytes(file.pdfUrl);
|
|
} catch (err) {
|
|
if (err instanceof RemoteFileError) return new Response(err.message, { status: 502 });
|
|
throw err;
|
|
}
|
|
} else {
|
|
try {
|
|
srcBytes = readPublicPdf(file.pdfUrl);
|
|
} catch (err) {
|
|
if (err instanceof PdfPathError) return new Response(err.message, { status: 400 });
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
// N3: ไฟล์รูปภาพไม่มี ToUnicode ให้ซ่อม — คืน bytes ดิบ + content-type ถูกต้อง
|
|
// (viewer โหลดรูปจาก static pdfUrl ตรง ๆ อยู่แล้ว — branch นี้กัน stray call
|
|
// หลุดเข้า fixPdfActualText ที่จะ throw กับ bytes ที่ไม่ใช่ PDF)
|
|
if (fileKindFromUrl(file.pdfUrl) === "image") {
|
|
return new Response(new Uint8Array(srcBytes), {
|
|
headers: {
|
|
"Content-Type": imageContentTypeFromUrl(file.pdfUrl),
|
|
"Cache-Control": "no-store",
|
|
},
|
|
});
|
|
}
|
|
|
|
const fixed = await fixPdfActualText(srcBytes);
|
|
return new Response(new Uint8Array(fixed), {
|
|
headers: {
|
|
"Content-Type": "application/pdf",
|
|
// ขนาดไฟล์ตัวอย่างเล็ก ซ่อมใหม่ต่อ request (สักหลักสิบ ms) — ไม่ต้องแคช
|
|
"Cache-Control": "no-store",
|
|
},
|
|
});
|
|
}
|