Add application codebase: PDF annotation for e-Court
Next.js 14 + Drizzle/PostgreSQL + SSE realtime sync — highlight, freehand, comment, bookmark + ฟีเจอร์ต่อยอด F1–F19 (edit/move, undo/redo, shapes, redaction, export PDF/CSV, reply, resolve, search, summary, textbox, stamp, version history) Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
parent
6fd0c3174f
commit
eddcec28c7
60 changed files with 15055 additions and 0 deletions
55
lib/annotate-events.ts
Normal file
55
lib/annotate-events.ts
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
import type { AnnotationDTO, BookmarkDTO, ReplyDTO } from "@/db/schema";
|
||||
|
||||
// pub/sub in-process (PLAN.md §4): Server Actions publish หลังเขียน DB สำเร็จ
|
||||
// → SSE route (app/api/annotations/stream) subscribe แล้วยิง event ให้ client ที่เปิดไฟล์เดียวกัน
|
||||
// ⚠ in-memory ของ process เดียว (dev / next start ธรรมดา) — ถ้าวันหนึ่งรันหลาย instance
|
||||
// ต้องเพิ่มตัวกลาง (Redis pub/sub หรือ Postgres LISTEN/NOTIFY) ก่อน ห้ามแอบ sync ผ่าน DB polling
|
||||
|
||||
export type AnnotateEvent =
|
||||
| { table: "annotations"; action: "create"; row: AnnotationDTO }
|
||||
| { table: "annotations"; action: "update"; row: AnnotationDTO }
|
||||
| { table: "annotations"; action: "delete"; id: string }
|
||||
| { table: "replies"; action: "create"; row: ReplyDTO }
|
||||
| { table: "replies"; action: "delete"; id: string }
|
||||
| { table: "bookmarks"; action: "create"; row: BookmarkDTO }
|
||||
| { table: "bookmarks"; action: "update"; row: BookmarkDTO }
|
||||
| { table: "bookmarks"; action: "delete"; id: string };
|
||||
|
||||
type Listener = (event: AnnotateEvent) => void;
|
||||
|
||||
// ⚠ เก็บบน globalThis เพราะ Next dev bundle "use server" (actions) กับ route handler
|
||||
// แบบคนละส่วน — module-level state จะถูก instantiate เป็น 2 อินสแตนซ์แล้ว event ไม่ไหลถึงกัน
|
||||
// (ยังช่วยให้ state รอดจาก HMR recompile ด้วย)
|
||||
type EventsGlobal = { __annotateEventListeners?: Map<string, Set<Listener>> };
|
||||
const g = globalThis as typeof globalThis & EventsGlobal;
|
||||
const listenersByFile: Map<string, Set<Listener>> =
|
||||
g.__annotateEventListeners ?? (g.__annotateEventListeners = new Map());
|
||||
|
||||
/** คืนฟังก์ชัน unsubscribe */
|
||||
export function subscribeAnnotateEvents(
|
||||
fileId: string,
|
||||
listener: Listener,
|
||||
): () => void {
|
||||
let set = listenersByFile.get(fileId);
|
||||
if (!set) {
|
||||
set = new Set();
|
||||
listenersByFile.set(fileId, set);
|
||||
}
|
||||
set.add(listener);
|
||||
return () => {
|
||||
set.delete(listener);
|
||||
if (set.size === 0) listenersByFile.delete(fileId);
|
||||
};
|
||||
}
|
||||
|
||||
export function publishAnnotateEvent(fileId: string, event: AnnotateEvent): void {
|
||||
const set = listenersByFile.get(fileId);
|
||||
if (!set) return;
|
||||
for (const listener of Array.from(set)) {
|
||||
try {
|
||||
listener(event);
|
||||
} catch (err) {
|
||||
console.error("annotate-events listener error", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
24
lib/server-pdf.ts
Normal file
24
lib/server-pdf.ts
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
|
||||
// อ่าน PDF ต้นฉบับจาก public/ ฝั่ง server (F8 export)
|
||||
// - **อ่านอย่างเดียว ห้ามเขียนทับ** (invariant #1) — pdf-lib load เป็นสำเนาในหน่วยความจำ
|
||||
// - path guard: pdfUrl จาก DB ต้อง resolve แล้วอยู่ใน public/ เท่านั้น กัน path traversal (§5)
|
||||
|
||||
export class PdfPathError extends Error {}
|
||||
|
||||
export function resolvePublicPdfPath(pdfUrl: string): string {
|
||||
const publicDir = path.resolve(process.cwd(), "public");
|
||||
// ตัด root นำหน้าออกก่อน — ไม่งั้น path.resolve บน Windows ถือ "/samples/.." เป็น absolute
|
||||
// ของไดรว์ (D:\samples\..) แล้วหลุดนอก publicDir
|
||||
const pdfPath = path.resolve(publicDir, pdfUrl.replace(/^[/\\]+/, ""));
|
||||
if (!pdfPath.startsWith(publicDir + path.sep)) {
|
||||
throw new PdfPathError("เส้นทางไฟล์ไม่ถูกต้อง");
|
||||
}
|
||||
return pdfPath;
|
||||
}
|
||||
|
||||
/** ไฟล์ PDF ตาม path ที่ผ่าน guard แล้ว (อ่านอย่างเดียว) */
|
||||
export function readPublicPdf(pdfUrl: string): Buffer {
|
||||
return fs.readFileSync(resolvePublicPdfPath(pdfUrl));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue