2026-09-11 13:28:35 +07:00
|
|
|
import { eq } from "drizzle-orm";
|
|
|
|
|
|
|
|
|
|
import { db } from "@/db";
|
|
|
|
|
import { files } from "@/db/schema";
|
|
|
|
|
import { fixPdfActualText } from "@/lib/pdf-actual-text";
|
2026-09-23 10:44:14 +07:00
|
|
|
import { fetchSourceBytes, isRemoteSource, RemoteFileError } from "@/lib/remote-file";
|
2026-09-11 13:28:35 +07:00
|
|
|
import { PdfPathError, readPublicPdf } from "@/lib/server-pdf";
|
2026-09-15 14:12:51 +07:00
|
|
|
import { fileKindFromUrl, imageContentTypeFromUrl } from "@/utils/file-kind";
|
2026-09-11 13:28:35 +07:00
|
|
|
|
|
|
|
|
// ให้ 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)
|
2026-09-23 10:44:14 +07:00
|
|
|
// N8: pdf_url เป็นรีโมต (ลิงก์ e-Court) → ดึงสดจาก allowlist แทนการอ่านดิสก์
|
2026-09-11 13:28:35 +07:00
|
|
|
let srcBytes: Buffer;
|
2026-09-23 10:44:14 +07:00
|
|
|
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;
|
|
|
|
|
}
|
2026-09-11 13:28:35 +07:00
|
|
|
}
|
|
|
|
|
|
2026-09-15 14:12:51 +07:00
|
|
|
// 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",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-11 13:28:35 +07:00
|
|
|
const fixed = await fixPdfActualText(srcBytes);
|
|
|
|
|
return new Response(new Uint8Array(fixed), {
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/pdf",
|
|
|
|
|
// ขนาดไฟล์ตัวอย่างเล็ก ซ่อมใหม่ต่อ request (สักหลักสิบ ms) — ไม่ต้องแคช
|
|
|
|
|
"Cache-Control": "no-store",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|