Files
Bkk1234 2c5918a614 feat(admin): อัปโหลดรูปโรงหนังจากเครื่อง (เดิมใส่ได้แค่ URL)
- POST /api/admin/uploads (multipart, ≤8MB, ตรวจ magic bytes JPEG/PNG/WebP/AVIF)
  เก็บ data/uploads/cinemas/ คืน /uploads/cinemas/<slug>-<random>-<ts>.<ext>
- GET /uploads/** เสิร์ฟไฟล์อัปโหลด (Nitro route, immutable cache, กัน path traversal)
- PhotosEditor: ปุ่มอัปโหลด + ลากไฟล์มาวาง + Ctrl+V วางจากคลิปบอร์ด, หลายไฟล์พร้อมกัน
- app/lib/upload.ts กติกากลาง + upload.spec.ts (13 tests)
- docs: api.md, admin-guide.md (วิธีอัปโหลด), DESIGN.md, README
2026-11-15 23:36:35 +07:00

105 lines
3.6 KiB
TypeScript

/**
* กติกาการอัปโหลดรูป (ใช้ร่วมทั้ง client และ server)
*
* เก็บไว้ที่ `data/uploads/cinemas/` แล้วเสิร์ฟผ่าน `/uploads/cinemas/<ไฟล์>`
* (ไม่เขียนลง `public/` เพราะตอน production โฟลเดอร์ public ถูก copy ตอน build — เขียน runtime ไม่ติด)
*/
export const MAX_UPLOAD_BYTES = 8 * 1024 * 1024; // 8 MB
export const UPLOAD_URL_PREFIX = "/uploads/cinemas";
export const ALLOWED_IMAGE_TYPES = [
"image/jpeg",
"image/png",
"image/webp",
"image/avif",
] as const;
const EXT_BY_TYPE: Record<string, string> = {
"image/jpeg": "jpg",
"image/png": "png",
"image/webp": "webp",
"image/avif": "avif",
};
export function extensionForType(type: string): string | null {
return EXT_BY_TYPE[type.trim().toLowerCase()] ?? null;
}
export function typeForExtension(ext: string): string {
const found = Object.entries(EXT_BY_TYPE).find(([, value]) => value === ext);
return found ? found[0] : "application/octet-stream";
}
/** ชื่อไฟล์ปลอดภัย: ตัวพิมพ์เล็ก a-z 0-9 และ `-` เท่านั้น */
export function sanitizeSlug(value: string, fallback = "cinema"): string {
const clean = String(value ?? "")
.toLowerCase()
.replace(/[^a-z0-9-]+/g, "-")
.replace(/^-+|-+$/g, "")
.slice(0, 64);
return clean === "" ? fallback : clean;
}
/** ตรวจชนิดไฟล์จาก "magic bytes" — ไม่เชื่อ content-type ที่ client ส่งมา */
export function sniffImageType(bytes: Uint8Array): string | null {
if (bytes.length < 12) return null;
// JPEG: FF D8 FF
if (bytes[0] === 0xff && bytes[1] === 0xd8 && bytes[2] === 0xff) return "image/jpeg";
// PNG: 89 50 4E 47 0D 0A 1A 0A
if (
bytes[0] === 0x89 &&
bytes[1] === 0x50 &&
bytes[2] === 0x4e &&
bytes[3] === 0x47
) {
return "image/png";
}
// WebP: "RIFF" .... "WEBP"
if (
bytes[0] === 0x52 &&
bytes[1] === 0x49 &&
bytes[2] === 0x46 &&
bytes[3] === 0x46 &&
bytes[8] === 0x57 &&
bytes[9] === 0x45 &&
bytes[10] === 0x42 &&
bytes[11] === 0x50
) {
return "image/webp";
}
// AVIF: ....ftyp + avif/avis
const brand = String.fromCharCode(...bytes.slice(4, 12));
if (brand.startsWith("ftyp") && (brand.includes("avif") || brand.includes("avis"))) {
return "image/avif";
}
return null;
}
/** true = ไฟล์นี้รับได้ (ขนาด + ชนิดจริง) */
export function isValidImageUpload(bytes: Uint8Array): boolean {
if (bytes.byteLength === 0 || bytes.byteLength > MAX_UPLOAD_BYTES) return false;
return sniffImageType(bytes) !== null;
}
/** ชื่อไฟล์ปลายทาง เช่น `cinema-a1b2c3-1712345678901.webp` */
export function makeUploadName(
slug: string,
type: string,
now: number = Date.now(),
random: string = Math.random().toString(36).slice(2, 8),
): string {
const ext = extensionForType(type);
if (!ext) throw new Error(`ชนิดไฟล์ไม่รองรับ: ${type}`);
return `${sanitizeSlug(slug)}-${random}-${now}.${ext}`;
}
/** ป้องกัน path traversal ตอนอ่านไฟล์ที่อัปโหลด */
export function safeUploadPath(requested: string): string | null {
const cleaned = String(requested ?? "").replace(/\\/g, "/").trim();
if (cleaned === "" || cleaned.includes("..") || cleaned.startsWith("/")) return null;
if (!/^[a-zA-Z0-9._/-]+$/.test(cleaned)) return null;
return cleaned;
}