55 lines
2 KiB
TypeScript
55 lines
2 KiB
TypeScript
|
|
// ชนิดไฟล์เอกสาร — derive จาก extension ของ path ที่เก็บใน files.pdf_url (N3)
|
||
|
|
// ไม่มี column kind ใน DB: extension คือ single source of truth (seed idempotent อิง URL)
|
||
|
|
// pure module — ใช้ได้ทั้ง server และ client
|
||
|
|
|
||
|
|
export type FileKind = "pdf" | "image";
|
||
|
|
export type ImageFormat = "jpeg" | "png" | "tiff";
|
||
|
|
|
||
|
|
const IMAGE_EXTENSIONS: Record<string, ImageFormat> = {
|
||
|
|
".jpg": "jpeg",
|
||
|
|
".jpeg": "jpeg",
|
||
|
|
".png": "png",
|
||
|
|
".tif": "tiff",
|
||
|
|
".tiff": "tiff",
|
||
|
|
};
|
||
|
|
|
||
|
|
/** extension (ตัวเล็ก พร้อมจุดนำหน้า) ของ path หลังตัด query string — ไม่มี = "" */
|
||
|
|
function extOf(url: string): string {
|
||
|
|
const clean = url.split(/[?#]/, 1)[0];
|
||
|
|
const slash = Math.max(clean.lastIndexOf("/"), clean.lastIndexOf("\\"));
|
||
|
|
const name = slash >= 0 ? clean.slice(slash + 1) : clean;
|
||
|
|
const dot = name.lastIndexOf(".");
|
||
|
|
return dot <= 0 ? "" : name.slice(dot).toLowerCase();
|
||
|
|
}
|
||
|
|
|
||
|
|
export function imageFormatFromUrl(url: string): ImageFormat | null {
|
||
|
|
return IMAGE_EXTENSIONS[extOf(url)] ?? null;
|
||
|
|
}
|
||
|
|
|
||
|
|
/** ไม่รู้จัก extension → "pdf" ตามพฤติกรรมเดิม (ก่อน N3 ทุกไฟล์เปิดเป็น PDF) */
|
||
|
|
export function fileKindFromUrl(url: string): FileKind {
|
||
|
|
if (imageFormatFromUrl(url)) return "image";
|
||
|
|
return "pdf";
|
||
|
|
}
|
||
|
|
|
||
|
|
/** whitelist extension ที่ seed-samples sync จาก public/samples/ (seed .mjs มีสำเนา — sync กัน) */
|
||
|
|
export const SAMPLE_FILE_EXTENSIONS: readonly string[] = [
|
||
|
|
".pdf",
|
||
|
|
".jpg",
|
||
|
|
".jpeg",
|
||
|
|
".png",
|
||
|
|
".tif",
|
||
|
|
".tiff",
|
||
|
|
];
|
||
|
|
|
||
|
|
const IMAGE_CONTENT_TYPES: Record<ImageFormat, string> = {
|
||
|
|
jpeg: "image/jpeg",
|
||
|
|
png: "image/png",
|
||
|
|
tiff: "image/tiff",
|
||
|
|
};
|
||
|
|
|
||
|
|
export function imageContentTypeFromUrl(url: string): string {
|
||
|
|
const format = imageFormatFromUrl(url);
|
||
|
|
return format ? IMAGE_CONTENT_TYPES[format] : "application/octet-stream";
|
||
|
|
}
|