+
{
+ if (!event.dataTransfer?.types.includes("Files")) return;
+ event.preventDefault();
+ setDragging(true);
+ }}
+ onDragLeave={() => setDragging(false)}
+ onDrop={(event) => {
+ if (!event.dataTransfer?.files?.length) return;
+ event.preventDefault();
+ setDragging(false);
+ void uploadFiles(event.dataTransfer.files);
+ }}
+ onPaste={(event) => {
+ const files = Array.from(event.clipboardData?.files ?? []);
+ if (files.length === 0) return;
+ event.preventDefault();
+ void uploadFiles(files);
+ }}
+ >
รูปภาพโรงหนัง
{photos.length === 0 ? (
- ยังไม่มีรูป — หน้าเว็บจะแสดงช่องแทน (ภาพที่ 1/2/3)
+ ยังไม่มีรูป — อัปโหลดจากเครื่อง หรือวาง URL ด้านล่าง (ภาพที่ 1/2/3)
) : null}
@@ -93,14 +183,48 @@ export function PhotosEditor({ photos, onChange, inputClass, labelClass }: Props
))}
-
+
+
+
+
+
+
+ ลากไฟล์มาวาง หรือ Ctrl+V ในกล่องนี้ · JPG/PNG/WebP/AVIF ≤{" "}
+ {Math.round(MAX_UPLOAD_BYTES / 1024 / 1024)} MB
+
+
+
+
{
+ if (event.target.files) void uploadFiles(event.target.files);
+ }}
+ />
+
+ {error ? (
+
+ {error}
+
+ ) : null}
);
}
diff --git a/app/lib/upload.spec.ts b/app/lib/upload.spec.ts
new file mode 100644
index 0000000..23c4142
--- /dev/null
+++ b/app/lib/upload.spec.ts
@@ -0,0 +1,115 @@
+/**
+ * ทดสอบกติกาการอัปโหลดรูป — กัน 3 เคสที่พังเงียบ:
+ * - รับไฟล์ที่ไม่ใช่รูป (เปลี่ยนนามสกุลมา)
+ * - path traversal ตอนเสิร์ฟไฟล์ (`../`)
+ * - ชื่อไฟล์ชนกัน (ต้องมี timestamp + random)
+ */
+import { describe, expect, it } from "vitest";
+
+import {
+ MAX_UPLOAD_BYTES,
+ extensionForType,
+ isValidImageUpload,
+ makeUploadName,
+ safeUploadPath,
+ sanitizeSlug,
+ sniffImageType,
+ typeForExtension,
+} from "@/lib/upload";
+
+const jpeg = new Uint8Array([
+ 0xff, 0xd8, 0xff, 0xe0, 0, 0x10, 0x4a, 0x46, 0x49, 0x46, 0, 1,
+]);
+const png = new Uint8Array([
+ 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0, 0, 0, 0,
+]);
+const webp = new Uint8Array([
+ 0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00, 0x57, 0x45, 0x42, 0x50,
+]);
+const gif = new Uint8Array([
+ 0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0, 0, 0, 0, 0, 0,
+]);
+
+describe("sniffImageType", () => {
+ it("ตรวจ JPEG / PNG / WebP จาก magic bytes", () => {
+ expect(sniffImageType(jpeg)).toBe("image/jpeg");
+ expect(sniffImageType(png)).toBe("image/png");
+ expect(sniffImageType(webp)).toBe("image/webp");
+ });
+
+ it("ไฟล์ที่ไม่ใช่รูป (หรือสั้นเกิน) → null", () => {
+ expect(sniffImageType(gif)).toBeNull();
+ expect(sniffImageType(new Uint8Array([1, 2, 3]))).toBeNull();
+ });
+});
+
+describe("isValidImageUpload", () => {
+ it("รับรูปที่ถูกต้อง", () => {
+ expect(isValidImageUpload(jpeg)).toBe(true);
+ });
+
+ it("ปฏิเสธไฟล์ว่างและไฟล์ที่ใหญ่เกินเพดาน", () => {
+ expect(isValidImageUpload(new Uint8Array([]))).toBe(false);
+ const huge = new Uint8Array(MAX_UPLOAD_BYTES + 1);
+ huge.set(jpeg);
+ expect(isValidImageUpload(huge)).toBe(false);
+ });
+});
+
+describe("extensionForType / typeForExtension", () => {
+ it("แมปชนิด ↔ นามสกุลไป-กลับได้", () => {
+ expect(extensionForType("image/jpeg")).toBe("jpg");
+ expect(typeForExtension("webp")).toBe("image/webp");
+ });
+
+ it("ชนิดที่ไม่รองรับ → null", () => {
+ expect(extensionForType("image/gif")).toBeNull();
+ expect(extensionForType("text/html")).toBeNull();
+ });
+});
+
+describe("sanitizeSlug", () => {
+ it("เหลือ a-z 0-9 และ - เท่านั้น", () => {
+ expect(sanitizeSlug("Phahol Theatre!")).toBe("phahol-theatre");
+ expect(sanitizeSlug("../../etc/passwd")).toBe("etc-passwd");
+ });
+
+ it("ว่าง → ใช้ค่า fallback", () => {
+ expect(sanitizeSlug("!!!")).toBe("cinema");
+ });
+});
+
+describe("makeUploadName", () => {
+ it("มี slug + random + timestamp + นามสกุลที่ถูกต้อง", () => {
+ const name = makeUploadName(
+ "house-cinema-samyan",
+ "image/webp",
+ 1700000000000,
+ "abc123",
+ );
+ expect(name).toBe("house-cinema-samyan-abc123-1700000000000.webp");
+ });
+
+ it("ชื่อไม่ชนกันเมื่อเรียกซ้ำในมิลลิวินาทีเดียวกัน", () => {
+ const a = makeUploadName("x", "image/jpeg", 1, "aaa111");
+ const b = makeUploadName("x", "image/jpeg", 1, "bbb222");
+ expect(a).not.toBe(b);
+ });
+
+ it("ชนิดที่ไม่รองรับ → โยน error", () => {
+ expect(() => makeUploadName("x", "image/gif")).toThrow();
+ });
+});
+
+describe("safeUploadPath", () => {
+ it("รับพาธปกติ", () => {
+ expect(safeUploadPath("cinemas/abc-1.webp")).toBe("cinemas/abc-1.webp");
+ });
+
+ it("ปฏิเสธ path traversal และ absolute path", () => {
+ expect(safeUploadPath("../secrets.json")).toBeNull();
+ expect(safeUploadPath("cinemas/../../x")).toBeNull();
+ expect(safeUploadPath("/etc/passwd")).toBeNull();
+ expect(safeUploadPath("")).toBeNull();
+ });
+});
diff --git a/app/lib/upload.ts b/app/lib/upload.ts
new file mode 100644
index 0000000..0bb8bec
--- /dev/null
+++ b/app/lib/upload.ts
@@ -0,0 +1,104 @@
+/**
+ * กติกาการอัปโหลดรูป (ใช้ร่วมทั้ง 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