2026-09-23 10:44:14 +07:00
|
|
|
// N8 (PLAN-ECOURT.md D1) — ลงทะเบียนเอกสารจากลิงก์ e-Court ลง DB แบบ metadata-only
|
|
|
|
|
// (ไม่ดาวน์โหลด — เก็บแค่ title + external_id + URL ใน pdf_url; ตอนเปิดดู/export
|
|
|
|
|
// ค่อยดึงไฟล์สดผ่าน lib/remote-file.ts)
|
|
|
|
|
// server module ธรรมดา **ไม่ใช่ "use server"** — เรียกจาก server component ของหน้า "/"
|
|
|
|
|
// ไม่เปิดเป็น endpoint ให้ client ยิงตรง ๆ
|
|
|
|
|
import { randomUUID } from "node:crypto";
|
|
|
|
|
import { eq } from "drizzle-orm";
|
|
|
|
|
|
|
|
|
|
import { db } from "@/db";
|
|
|
|
|
import { files } from "@/db/schema";
|
|
|
|
|
import { PdfPathError, readPublicPdf } from "@/lib/server-pdf";
|
|
|
|
|
import {
|
|
|
|
|
externalIdFor,
|
|
|
|
|
isHttpUrl,
|
genesis+worker: design system Genesis ทั้งชุด + patch pdf.worker แก้ fake-space ไทย + ปิด review design/responsive/quality รอบ 2026-09-24 — (1) Genesis: ฟอนต์ General Sans self-host + DM Sans + Sarabun, AppNav 56px backdrop-blur แทน SessionBar, DotGrid หน้า login, palette/รัศมี/เงา/focus ตาม docs/DESIGN.md ครบทุกหน้า (2) postinstall scripts/patch-pdf-worker.mjs ถอด if (scaledDim) รอบ prevTransform ให้อัปเดตทุก glyph — กัน addFakeSpaces แทรก space กลางคำไทยใน PDF ที่ justify ด้วย letter-spacing + test กัน regression (tests/scripts/ + fixture thai_letterspacing.pdf, invariant #6) (3) ฟอร์มอัปโหลด + ช่องกลุ่ม typeId ทางเลือก — normDocumentGroup ที่ utils/external-file.ts เป็น single source ร่วมกับ N8 (4) token fixes: focus ring ปุ่ม pin + แถว bookmark ตามหน้า, ปุ่ม action ใน popup/composer/dialog เป็น rounded-md ทั้งคู่, selection preview เป็น tint ของ primary แทน blue-500 ดิบ, default สี shape/highlight รวมที่ utils/annotation-display, loading animate-pulse + จดข้อยกเว้น (เงา pin, font weights ของ viewer) ลง DESIGN.md (5) responsive: Export PDF max-lg:order-first ให้เห็นบน iPad, แถวบน header แถวเดียวเลื่อนได้บนจอแคบ, re-fit ความกว้างตอน toggle sidebar เฉพาะเมื่อยังไม่ซูมเอง, pin popover flip ขึ้นบนเมื่อใกล้ท้ายหน้า, toast hint ตอนสลับ tool กลางเอกสาร, ป้าย "ชนิด · ผู้ทำ" ของรายการผู้อื่นสำหรับจอสัมผัส (6) คุณภาพ: @layer ครอบ @media กันกฎ touch รั่ยไป desktop (Tailwind v3 ยก @layer ออกจาก media ทิ้ง wrapper — ยืนยันด้วย build จริง + วัดบนจอ), กฎ M5 ครอบ contenteditable กัน Safari zoom ตอนแก้ไข TEXTBOX บน iPad, COMMENT ใช้ box มาตราฐานเดียวตาม pin px จริง (render/scroll target/move-clamp/bookmark), ตัดหัวการ์ดซ้ำกับ h2 หน้ารายการ — พร้อมไฟล์ untracked ที่ postinstall/โค้ดอ้างถึงครบชุด (app/fonts/, AppNav, DotGrid, tests/) กัน fresh clone fail ที่ npm install (คำขอผู้ใช้ 2026-09-24)
Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-09-24 11:46:02 +07:00
|
|
|
normDocumentGroup,
|
2026-09-23 10:44:14 +07:00
|
|
|
titleFromExternalUrl,
|
|
|
|
|
validateExternalFileUrl,
|
|
|
|
|
type ExternalFileUrlError,
|
|
|
|
|
} from "@/utils/external-file";
|
|
|
|
|
import { SAMPLE_FILE_EXTENSIONS } from "@/utils/file-kind";
|
|
|
|
|
|
|
|
|
|
export type RegisterExternalFileResult =
|
|
|
|
|
| { ok: true; fileId: string }
|
|
|
|
|
| { ok: false; error: string };
|
|
|
|
|
|
|
|
|
|
const URL_ERROR_TEXT: Record<ExternalFileUrlError, string> = {
|
|
|
|
|
invalid_url: "ลิงก์ไฟล์ไม่ถูกต้อง",
|
|
|
|
|
not_https: "ลิงก์ไฟล์ต้องเป็น https เท่านั้น",
|
|
|
|
|
has_credentials: "ลิงก์ไฟล์มีชื่อผู้ใช้/รหัสผ่านฝังอยู่ — ไม่รองรับ",
|
|
|
|
|
host_not_allowed: "โฮสต์ของลิงก์ไฟล์ไม่อยู่ในรายการอนุญาต",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** path ในเครื่อง (ทดสอบใน dev เช่น /samples/…): decode %20, ห้าม .., นามสกุลผ่าน
|
|
|
|
|
* whitelist และต้องมีไฟล์จริงใน public/ ผ่าน path guard เดียวกับ viewer/export */
|
|
|
|
|
function validateLocalPath(
|
|
|
|
|
raw: string,
|
|
|
|
|
): { ok: true; path: string; ext: string } | { ok: false; error: string } {
|
|
|
|
|
if (raw.includes("..")) return { ok: false, error: "เส้นทางไฟล์ไม่ถูกต้อง" };
|
|
|
|
|
let p = raw;
|
|
|
|
|
try {
|
|
|
|
|
p = decodeURIComponent(raw);
|
|
|
|
|
} catch {
|
|
|
|
|
// ชื่อ encode เสีย — ใช้ค่าดิบ
|
|
|
|
|
}
|
|
|
|
|
const dot = p.lastIndexOf(".");
|
|
|
|
|
const ext = dot > 0 ? p.slice(dot).toLowerCase() : "";
|
|
|
|
|
if (!SAMPLE_FILE_EXTENSIONS.includes(ext)) {
|
|
|
|
|
return { ok: false, error: `รองรับเฉพาะไฟล์ ${SAMPLE_FILE_EXTENSIONS.join(" / ")}` };
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
readPublicPdf(p);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
if (err instanceof PdfPathError || (err as NodeJS.ErrnoException)?.code === "ENOENT") {
|
|
|
|
|
return { ok: false, error: "ไม่พบไฟล์นี้ในระบบ (path ต้องอยู่ใน public/ เช่น /samples/…)" };
|
|
|
|
|
}
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
|
|
|
|
return { ok: true, path: p, ext };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function registerExternalFile(
|
|
|
|
|
rawFileUrl: string,
|
|
|
|
|
rawFileId?: string,
|
|
|
|
|
rawTypeId?: string,
|
|
|
|
|
): Promise<RegisterExternalFileResult> {
|
|
|
|
|
let trimmed = rawFileUrl.trim();
|
|
|
|
|
if (!trimmed) return { ok: false, error: "ไม่ได้ระบุลิงก์ไฟล์" };
|
|
|
|
|
// URL ที่ไม่ได้ขึ้นต้น scheme (เช่น "www.rd.usda.gov/…") เติม https:// ให้ก่อน validate
|
|
|
|
|
// — path ในเครื่อง (ขึ้นต้น "/") ไม่แตะ
|
|
|
|
|
if (!trimmed.startsWith("/") && !/^https?:\/\//i.test(trimmed)) {
|
|
|
|
|
trimmed = `https://${trimmed}`;
|
|
|
|
|
}
|
|
|
|
|
|
genesis+worker: design system Genesis ทั้งชุด + patch pdf.worker แก้ fake-space ไทย + ปิด review design/responsive/quality รอบ 2026-09-24 — (1) Genesis: ฟอนต์ General Sans self-host + DM Sans + Sarabun, AppNav 56px backdrop-blur แทน SessionBar, DotGrid หน้า login, palette/รัศมี/เงา/focus ตาม docs/DESIGN.md ครบทุกหน้า (2) postinstall scripts/patch-pdf-worker.mjs ถอด if (scaledDim) รอบ prevTransform ให้อัปเดตทุก glyph — กัน addFakeSpaces แทรก space กลางคำไทยใน PDF ที่ justify ด้วย letter-spacing + test กัน regression (tests/scripts/ + fixture thai_letterspacing.pdf, invariant #6) (3) ฟอร์มอัปโหลด + ช่องกลุ่ม typeId ทางเลือก — normDocumentGroup ที่ utils/external-file.ts เป็น single source ร่วมกับ N8 (4) token fixes: focus ring ปุ่ม pin + แถว bookmark ตามหน้า, ปุ่ม action ใน popup/composer/dialog เป็น rounded-md ทั้งคู่, selection preview เป็น tint ของ primary แทน blue-500 ดิบ, default สี shape/highlight รวมที่ utils/annotation-display, loading animate-pulse + จดข้อยกเว้น (เงา pin, font weights ของ viewer) ลง DESIGN.md (5) responsive: Export PDF max-lg:order-first ให้เห็นบน iPad, แถวบน header แถวเดียวเลื่อนได้บนจอแคบ, re-fit ความกว้างตอน toggle sidebar เฉพาะเมื่อยังไม่ซูมเอง, pin popover flip ขึ้นบนเมื่อใกล้ท้ายหน้า, toast hint ตอนสลับ tool กลางเอกสาร, ป้าย "ชนิด · ผู้ทำ" ของรายการผู้อื่นสำหรับจอสัมผัส (6) คุณภาพ: @layer ครอบ @media กันกฎ touch รั่ยไป desktop (Tailwind v3 ยก @layer ออกจาก media ทิ้ง wrapper — ยืนยันด้วย build จริง + วัดบนจอ), กฎ M5 ครอบ contenteditable กัน Safari zoom ตอนแก้ไข TEXTBOX บน iPad, COMMENT ใช้ box มาตราฐานเดียวตาม pin px จริง (render/scroll target/move-clamp/bookmark), ตัดหัวการ์ดซ้ำกับ h2 หน้ารายการ — พร้อมไฟล์ untracked ที่ postinstall/โค้ดอ้างถึงครบชุด (app/fonts/, AppNav, DotGrid, tests/) กัน fresh clone fail ที่ npm install (คำขอผู้ใช้ 2026-09-24)
Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-09-24 11:46:02 +07:00
|
|
|
const typeId = normDocumentGroup(rawTypeId) ?? undefined;
|
2026-09-23 10:44:14 +07:00
|
|
|
|
|
|
|
|
// สองทาง: https ของ e-Court (ตามสัญญาลิงก์ — validate + allowlist) หรือ path ในเครื่อง
|
|
|
|
|
// (ทดสอบ dev เช่น /samples/…) — ทั้งคู่เก็บลง pdf_url เหมือนกัน ต่างกันแค่ตอนเปิดดู
|
|
|
|
|
// (รีโมตดึงสดผ่าน lib/remote-file.ts, ในเครื่องอ่านจาก public/)
|
|
|
|
|
let sourceUrl: string;
|
|
|
|
|
let title: string;
|
|
|
|
|
let externalId: string;
|
|
|
|
|
if (isHttpUrl(trimmed)) {
|
|
|
|
|
const checked = validateExternalFileUrl(trimmed);
|
|
|
|
|
if (!checked.ok) return { ok: false, error: URL_ERROR_TEXT[checked.error] };
|
|
|
|
|
sourceUrl = checked.url.href;
|
|
|
|
|
title = titleFromExternalUrl(checked.url, rawFileId);
|
|
|
|
|
externalId = externalIdFor(rawFileId, checked.url);
|
|
|
|
|
} else if (trimmed.startsWith("/")) {
|
|
|
|
|
const local = validateLocalPath(trimmed);
|
|
|
|
|
if (!local.ok) return local;
|
|
|
|
|
sourceUrl = local.path;
|
|
|
|
|
const base = local.path.split("/").pop() ?? "";
|
|
|
|
|
title =
|
|
|
|
|
base.slice(0, base.length - local.ext.length).trim().slice(0, 200) || "เอกสาร";
|
|
|
|
|
const id = rawFileId?.trim();
|
|
|
|
|
externalId = id ? `ecourt:${id}` : `url:${local.path}`;
|
|
|
|
|
} else {
|
|
|
|
|
return {
|
|
|
|
|
ok: false,
|
|
|
|
|
error: "ลิงก์ไฟล์ต้องเป็น https หรือ path ในระบบที่ขึ้นต้นด้วย / เช่น /samples/…",
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const [existing] = await db
|
|
|
|
|
.select()
|
|
|
|
|
.from(files)
|
|
|
|
|
.where(eq(files.externalId, externalId))
|
|
|
|
|
.limit(1);
|
|
|
|
|
if (existing) {
|
|
|
|
|
// fileId เดิมเจออีกครั้ง: refresh Temp link ใหม่ + ย้ายกลุ่มตาม typeId ล่าสุด
|
|
|
|
|
// (ลิงก์ไม่ส่ง typeId มา = คงกลุ่มเดิม ห้ามเคลียร์ — PLAN-ECOURT.md การตัดสินใจ)
|
|
|
|
|
const patch: { pdfUrl?: string; typeId?: string } = {};
|
|
|
|
|
if (existing.pdfUrl !== sourceUrl) patch.pdfUrl = sourceUrl;
|
|
|
|
|
if (typeId && existing.typeId !== typeId) patch.typeId = typeId;
|
|
|
|
|
if (Object.keys(patch).length > 0) {
|
|
|
|
|
await db.update(files).set(patch).where(eq(files.id, existing.id));
|
|
|
|
|
}
|
|
|
|
|
return { ok: true, fileId: existing.id };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const id = randomUUID();
|
|
|
|
|
try {
|
|
|
|
|
await db.insert(files).values({
|
|
|
|
|
id,
|
|
|
|
|
title,
|
|
|
|
|
pdfUrl: sourceUrl,
|
|
|
|
|
externalId,
|
|
|
|
|
typeId: typeId ?? null,
|
|
|
|
|
});
|
|
|
|
|
} catch (err) {
|
|
|
|
|
// 23505: เปิด 2 แท็บ URL/id เดียวกันครั้งแรกพร้อมกัน — คืน row ของฝ่ายที่ชนะไปก่อน
|
|
|
|
|
if ((err as { code?: string })?.code === "23505") {
|
|
|
|
|
const [row] = await db
|
|
|
|
|
.select()
|
|
|
|
|
.from(files)
|
|
|
|
|
.where(eq(files.externalId, externalId))
|
|
|
|
|
.limit(1);
|
|
|
|
|
if (row) return { ok: true, fileId: row.id };
|
|
|
|
|
}
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
|
|
|
|
return { ok: true, fileId: id };
|
|
|
|
|
}
|