148 lines
6.8 KiB
TypeScript
148 lines
6.8 KiB
TypeScript
// 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,
|
|
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: "โฮสต์ของลิงก์ไฟล์ไม่อยู่ในรายการอนุญาต",
|
|
};
|
|
|
|
/** รหัสกลุ่ม normalize: trim → ว่าง = ไม่มี, cap 200 ตัวอักษร (เทียบ title) */
|
|
function normGroup(s: string | undefined): string | undefined {
|
|
const v = (s ?? "").trim().slice(0, 200);
|
|
return v || undefined;
|
|
}
|
|
|
|
/** 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}`;
|
|
}
|
|
|
|
const typeId = normGroup(rawTypeId);
|
|
|
|
// สองทาง: 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 };
|
|
}
|