diff --git a/DESIGN.md b/DESIGN.md index c8b0d5d..9d7870a 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -247,6 +247,11 @@ Single source of truth per concern — do not duplicate these: - `cinema.photos[0]` = **รูปปกการ์ดหน้าแรก** (`CinemaCard`) — ไม่มีรูป = ใช้ gradient 4 แบบ (`CARD_GRADIENTS`) - หน้า detail ใช้ `Gallery`: มีไม่ครบ 3 รูป ช่องที่ว่างแสดง **โลโก้ AlterFlixHub** (ดูหัวข้อโลโก้) - รูปในเครื่องวางที่ `public/images/…` แล้วใส่ path `/images/…` (ไฟล์ `hero.png` ใช้เป็นพื้นหลังฮีโร่หน้าแรก) +- **ห้ามใช้ลิงก์ hotlink จากภายนอกใน `photos[]`** (Google Maps/`googleusercontent`/`fbcdn`/`gstatic` ฯลฯ) — ลิงก์มีอายุ จำกัด rate และตอบ **200 แต่ 0 ไบต์** ทำให้รูปแตกโดยไม่มี error ให้จับ + - เก็บสำเนาลง `public/images/cinemas/[-n].` ด้วย `node scripts/mirror-photos.mjs` (สำรอง `content.json` ให้เอง · `--dry` = ดูเฉย ๆ) แล้ว `photos[]` ชี้ path ในโปรเจกต์ + - ตรวจรูปเสียก่อน commit: `node scripts/audit-photos.mjs` (exit 1 = มีรูปพัง) + - ขนาดรูป: กว้างไม่เกิน **1600px** และบีบอัด (JPEG q82 / WebP q82) — โฟลเดอร์ `public/images/cinemas` ควรอยู่ราว **≤ 3 MB** +- **ทุก `` ของโรงหนังต้องใช้ ``** (`app/components/site/PhotoImage.tsx`) — มี `onError` สลับไปโลโก้แทนรูปแตก และจำ `src` ที่พังเพื่อให้เปลี่ยนรูปใหม่แล้วโหลดได้อีก (ห้ามเขียน `` ตรง ๆ ใน component ใหม่) ## ลำดับการ์ดหน้าแรก (สลับทุกวัน) diff --git a/app/components/cinema/Gallery.tsx b/app/components/cinema/Gallery.tsx index 819431f..912ffb8 100644 --- a/app/components/cinema/Gallery.tsx +++ b/app/components/cinema/Gallery.tsx @@ -2,6 +2,7 @@ import { IconChevronLeft, IconChevronRight } from "@tabler/icons-react"; import { useState, type ReactNode } from "react"; import { Logo } from "@/components/site/Logo"; +import { PhotoImage } from "@/components/site/PhotoImage"; import { rotateGallery, splitGallery } from "@/lib/gallery"; import { useI18n } from "@/lib/i18n"; @@ -19,21 +20,13 @@ type GallerySlotProps = { }; function GallerySlot({ photo, alt, placeholder, className }: GallerySlotProps) { - if (!photo) { - return ( -
- {placeholder} -
- ); - } - return ( - {alt} ); } diff --git a/app/components/site/CinemaCard.tsx b/app/components/site/CinemaCard.tsx index 0e97e84..c1b03d3 100644 --- a/app/components/site/CinemaCard.tsx +++ b/app/components/site/CinemaCard.tsx @@ -5,6 +5,7 @@ import { } from "@tabler/icons-react"; import { Link } from "react-router"; +import { PhotoImage } from "@/components/site/PhotoImage"; import { ShareButton } from "@/components/site/ShareButton"; import { useNearMe } from "@/hooks/use-near-me"; import { useReveal } from "@/hooks/use-reveal"; @@ -60,11 +61,10 @@ export function CinemaCard({ > {cover ? ( <> - {/* ไล่เงาเพื่อให้ตัวอักษรบนการ์ดอ่านได้ */} diff --git a/app/components/site/PhotoImage.tsx b/app/components/site/PhotoImage.tsx new file mode 100644 index 0000000..5ed6c28 --- /dev/null +++ b/app/components/site/PhotoImage.tsx @@ -0,0 +1,61 @@ +import { useState, type ReactNode } from "react"; + +import { Logo } from "@/components/site/Logo"; + +type PhotoImageProps = { + /** URL รูป — ว่าง/null = แสดง fallback ทันที */ + src: string | null | undefined; + alt: string; + className?: string; + /** เนื้อหาที่แสดงแทนเมื่อไม่มีรูปหรือรูปโหลดไม่สำเร็จ (ค่าเริ่มต้น = โลโก้) */ + fallback?: ReactNode; + /** ขนาดโลโก้ตอนใช้ fallback */ + size?: "sm" | "md"; + /** โหลดแบบ lazy (ปิดได้ถ้าเป็นรูป hero ที่ต้องเห็นทันที) */ + lazy?: boolean; +}; + +/** + * รูปภาพที่มี fallback ในตัว + * + * ทำไมต้องมี: ข้อมูลโรงหนังอ้างรูปจากภายนอก (เช่น Google Maps) ซึ่งลิงก์หมดอายุได้ + * และบางโฮสต์ตอบ **200 แต่ 0 ไบต์** → เบราว์เซอร์ขึ้นรูปแตก หน้าเว็บดูพัง + * คอมโพเนนต์นี้จะสลับไปใช้โลโก้แทนทันทีที่ `onError` ทำงาน (จำ src ที่พังไว้ + * เพื่อให้เปลี่ยนรูปใหม่แล้วลองโหลดได้อีกครั้ง) + */ +export function PhotoImage({ + src, + alt, + className = "", + fallback, + size = "md", + lazy = true, +}: PhotoImageProps) { + const [failedSrc, setFailedSrc] = useState(null); + const usable = + typeof src === "string" && src.trim() !== "" && failedSrc !== src; + + if (!usable) { + return ( +
+ {fallback ?? } +
+ ); + } + + return ( + {alt} setFailedSrc(src)} + /> + ); +} diff --git a/dev.cmd b/dev.cmd new file mode 100644 index 0000000..7d6977c --- /dev/null +++ b/dev.cmd @@ -0,0 +1,4 @@ +@echo off +REM เปิด dev server ของ alterflixhub (http://127.0.0.1:8080) — ปิดด้วยการกด Enter +powershell -NoProfile -ExecutionPolicy Bypass -File "D:\Gitin\dev-server.ps1" -Name "alterflixhub" -Cwd "D:\Git\workspace\web-alterflixhub" -Port 8080 -Command "pnpm dev" -Url "http://127.0.0.1:8080/" -TimeoutSec 300 +pause diff --git a/public/images/cinemas/alliance-francaise-de-bangkok-1.jpg b/public/images/cinemas/alliance-francaise-de-bangkok-1.jpg new file mode 100644 index 0000000..789888c Binary files /dev/null and b/public/images/cinemas/alliance-francaise-de-bangkok-1.jpg differ diff --git a/public/images/cinemas/alliance-francaise-de-bangkok-2.webp b/public/images/cinemas/alliance-francaise-de-bangkok-2.webp new file mode 100644 index 0000000..666b112 Binary files /dev/null and b/public/images/cinemas/alliance-francaise-de-bangkok-2.webp differ diff --git a/public/images/cinemas/century-the-movie-plaza-sukhumvit.jpg b/public/images/cinemas/century-the-movie-plaza-sukhumvit.jpg new file mode 100644 index 0000000..02ab313 Binary files /dev/null and b/public/images/cinemas/century-the-movie-plaza-sukhumvit.jpg differ diff --git a/public/images/cinemas/century-the-movie-plaza.jpg b/public/images/cinemas/century-the-movie-plaza.jpg new file mode 100644 index 0000000..d15fe71 Binary files /dev/null and b/public/images/cinemas/century-the-movie-plaza.jpg differ diff --git a/public/images/cinemas/cinema-oasis-1.webp b/public/images/cinemas/cinema-oasis-1.webp new file mode 100644 index 0000000..d71d597 Binary files /dev/null and b/public/images/cinemas/cinema-oasis-1.webp differ diff --git a/public/images/cinemas/cinema-oasis-2.jpg b/public/images/cinemas/cinema-oasis-2.jpg new file mode 100644 index 0000000..c043acb Binary files /dev/null and b/public/images/cinemas/cinema-oasis-2.jpg differ diff --git a/public/images/cinemas/ck-multiplex-1.jpg b/public/images/cinemas/ck-multiplex-1.jpg new file mode 100644 index 0000000..a049b68 Binary files /dev/null and b/public/images/cinemas/ck-multiplex-1.jpg differ diff --git a/public/images/cinemas/ck-multiplex-2.jpg b/public/images/cinemas/ck-multiplex-2.jpg new file mode 100644 index 0000000..32c767e Binary files /dev/null and b/public/images/cinemas/ck-multiplex-2.jpg differ diff --git a/public/images/cinemas/coliseum-paradise.jpg b/public/images/cinemas/coliseum-paradise.jpg new file mode 100644 index 0000000..8231949 Binary files /dev/null and b/public/images/cinemas/coliseum-paradise.jpg differ diff --git a/public/images/cinemas/house-cinema-samyan-1.jpg b/public/images/cinemas/house-cinema-samyan-1.jpg new file mode 100644 index 0000000..5019685 Binary files /dev/null and b/public/images/cinemas/house-cinema-samyan-1.jpg differ diff --git a/public/images/cinemas/house-cinema-samyan-2.webp b/public/images/cinemas/house-cinema-samyan-2.webp new file mode 100644 index 0000000..076ea4e Binary files /dev/null and b/public/images/cinemas/house-cinema-samyan-2.webp differ diff --git a/public/images/cinemas/house-cinema-samyan-3.webp b/public/images/cinemas/house-cinema-samyan-3.webp new file mode 100644 index 0000000..be06efd Binary files /dev/null and b/public/images/cinemas/house-cinema-samyan-3.webp differ diff --git a/public/images/cinemas/house-cinema-samyan-4.webp b/public/images/cinemas/house-cinema-samyan-4.webp new file mode 100644 index 0000000..5f0a237 Binary files /dev/null and b/public/images/cinemas/house-cinema-samyan-4.webp differ diff --git a/public/images/cinemas/house-cinema-samyan-5.jpg b/public/images/cinemas/house-cinema-samyan-5.jpg new file mode 100644 index 0000000..72c112a Binary files /dev/null and b/public/images/cinemas/house-cinema-samyan-5.jpg differ diff --git a/public/images/cinemas/major-hollywood.jpg b/public/images/cinemas/major-hollywood.jpg new file mode 100644 index 0000000..3531441 Binary files /dev/null and b/public/images/cinemas/major-hollywood.jpg differ diff --git a/public/images/cinemas/nang-loeng-cinema-sala-chalerm-thani.webp b/public/images/cinemas/nang-loeng-cinema-sala-chalerm-thani.webp new file mode 100644 index 0000000..84a40b5 Binary files /dev/null and b/public/images/cinemas/nang-loeng-cinema-sala-chalerm-thani.webp differ diff --git a/public/images/cinemas/nevada-multiplex.jpg b/public/images/cinemas/nevada-multiplex.jpg new file mode 100644 index 0000000..da5ccf7 Binary files /dev/null and b/public/images/cinemas/nevada-multiplex.jpg differ diff --git a/public/images/cinemas/phahol-theatre.webp b/public/images/cinemas/phahol-theatre.webp new file mode 100644 index 0000000..bcd4fdc Binary files /dev/null and b/public/images/cinemas/phahol-theatre.webp differ diff --git a/public/images/cinemas/thana-cineplex-1.jpg b/public/images/cinemas/thana-cineplex-1.jpg new file mode 100644 index 0000000..41e5c68 Binary files /dev/null and b/public/images/cinemas/thana-cineplex-1.jpg differ diff --git a/public/images/cinemas/thana-cineplex-2.jpg b/public/images/cinemas/thana-cineplex-2.jpg new file mode 100644 index 0000000..67d323c Binary files /dev/null and b/public/images/cinemas/thana-cineplex-2.jpg differ diff --git a/scripts/audit-photos.mjs b/scripts/audit-photos.mjs new file mode 100644 index 0000000..6fd0e08 --- /dev/null +++ b/scripts/audit-photos.mjs @@ -0,0 +1,99 @@ +/** + * ตรวจว่ารูปที่อ้างจากภายนอก (http/https) ใน content.json ยังใช้งานได้จริงไหม + * + * ทำไมต้องมี: โฮสต์อย่าง Google Maps ตอบ **200 แต่ 0 ไบต์** เมื่อลิงก์หมดอายุ/ถูกบล็อก + * → หน้าเว็บขึ้นรูปแตก (เบราว์เซอร์ไม่ถือว่า error จึงไม่มี event อะไรให้จับ) + * + * ใช้: node scripts/audit-photos.mjs + * exit 1 = พบรูปเสีย (ใช้ใน CI ได้) + */ +import fs from "node:fs"; +import path from "node:path"; + +const CONTENT_PATH = path.join("data", "content", "content.json"); +const MIN_BYTES = 2000; // เล็กกว่านี้ถือว่าใช้ไม่ได้ +const CONCURRENCY = 5; + +function readContent() { + try { + return JSON.parse(fs.readFileSync(CONTENT_PATH, "utf8")); + } catch (error) { + console.error(`อ่าน ${CONTENT_PATH} ไม่ได้: ${error.message}`); + process.exit(2); + } +} + +async function check(url) { + try { + const res = await fetch(url, { + redirect: "follow", + headers: { + // ให้เหมือนเบราว์เซอร์ที่สุด (บางโฮสต์บังคับ UA) + "user-agent": + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0 Safari/537.36", + accept: "image/avif,image/webp,image/*,*/*;q=0.8", + }, + signal: AbortSignal.timeout(20000), + }); + const type = res.headers.get("content-type") ?? ""; + const bytes = (await res.arrayBuffer()).byteLength; + const ok = res.ok && type.startsWith("image/") && bytes >= MIN_BYTES; + return { ok, status: res.status, type, bytes }; + } catch (error) { + return { ok: false, status: 0, type: "-", bytes: 0, error: error.message }; + } +} + +const content = readContent(); +const targets = []; +for (const cinema of content.cinemas ?? []) { + (cinema.photos ?? []).forEach((url, index) => { + if (/^https?:\/\//i.test(url)) { + targets.push({ slug: cinema.slug, index, url }); + } + }); +} + +if (targets.length === 0) { + console.log("✓ ไม่มีรูปภายนอกให้ตรวจ (ทุกรูปเป็นพาธในโปรเจกต์)"); + process.exit(0); +} + +console.log(`ตรวจ ${targets.length} รูปภายนอก...\n`); +const results = []; +for (let i = 0; i < targets.length; i += CONCURRENCY) { + const batch = targets.slice(i, i + CONCURRENCY); + const checked = await Promise.all( + batch.map(async (t) => ({ ...t, ...(await check(t.url)) })), + ); + results.push(...checked); +} + +const dead = results.filter((r) => !r.ok); +const host = (url) => { + try { + return new URL(url).host; + } catch { + return "?"; + } +}; + +for (const row of results) { + const state = row.ok ? "OK " : "พัง "; + console.log( + `${state} ${row.slug} #${row.index} — ${row.status} ${row.type} ${row.bytes}B — ${host(row.url)}`, + ); +} + +console.log(`\nสรุป: ใช้ได้ ${results.length - dead.length} · พัง ${dead.length}`); + +if (dead.length > 0) { + console.log("\nรูปที่ต้องแก้ (slug → ตำแหน่งใน photos[]):"); + for (const row of dead) { + console.log(` ${row.slug} #${row.index}`); + } + console.log( + "\nแนะนำ: ดาวน์โหลดรูปจริงเก็บใน public/images/cinemas/.jpg แล้วเปลี่ยน photos[] เป็นพาธนั้น", + ); + process.exit(1); +} diff --git a/scripts/mirror-photos.mjs b/scripts/mirror-photos.mjs new file mode 100644 index 0000000..4dd5f69 --- /dev/null +++ b/scripts/mirror-photos.mjs @@ -0,0 +1,123 @@ +/** + * ทำสำเนารูปที่อ้างจากภายนอก (http/https) ลงเครื่อง แล้วเปลี่ยน content.json ให้ชี้ไฟล์ในโปรเจกต์ + * + * ทำไม: โฮสต์อย่าง Google Maps / googleusercontent / fbcdn / gstatic เป็น **hotlink-hostile** + * (ลิงก์มีอายุ, จำกัด rate, ตอบ 200 แต่ 0 ไบต์) → หน้าเว็บขึ้นรูปแตกเป็นครั้งคราว + * การเก็บสำเนาไว้ใน `public/images/cinemas/` ทำให้เว็บไม่พึ่งโฮสต์เหล่านั้นอีก + * + * ใช้: node scripts/mirror-photos.mjs [--dry] + * --dry = ดาวน์โหลดและรายงานเฉย ๆ ไม่แก้ไฟล์ + * + * ปลอดภัย: สำรอง content.json เป็น `content.json.bak-photos-<เวลา>` ก่อนเขียน + */ +import fs from "node:fs"; +import path from "node:path"; + +const CONTENT_PATH = path.join("data", "content", "content.json"); +const OUT_DIR = path.join("public", "images", "cinemas"); +const PUBLIC_PREFIX = "/images/cinemas"; +const MIN_BYTES = 2000; +const DELAY_MS = 900; +const RETRIES = 3; +const DRY = process.argv.includes("--dry"); + +const UA = + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0 Safari/537.36"; + +const EXT_BY_TYPE = { + "image/jpeg": "jpg", + "image/jpg": "jpg", + "image/png": "png", + "image/webp": "webp", + "image/gif": "gif", + "image/avif": "avif", +}; + +const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); + +async function download(url) { + for (let attempt = 1; attempt <= RETRIES; attempt++) { + try { + const res = await fetch(url, { + redirect: "follow", + headers: { + "user-agent": UA, + accept: "image/avif,image/webp,image/*,*/*;q=0.8", + "accept-language": "th,en;q=0.9", + }, + signal: AbortSignal.timeout(25000), + }); + const type = (res.headers.get("content-type") ?? "").split(";")[0].trim(); + const buffer = Buffer.from(await res.arrayBuffer()); + if (res.ok && type.startsWith("image/") && buffer.byteLength >= MIN_BYTES) { + return { ok: true, type, buffer, status: res.status }; + } + if (attempt < RETRIES) await sleep(DELAY_MS * attempt); + } catch (error) { + if (attempt < RETRIES) await sleep(DELAY_MS * attempt); + if (attempt === RETRIES) return { ok: false, reason: error.message }; + } + } + return { ok: false, reason: "ยังไม่ได้ไฟล์ที่ใช้ได้ (0 ไบต์ / 429 / ไม่ใช่รูป)" }; +} + +function slugifyName(value, fallback) { + const clean = String(value ?? "") + .toLowerCase() + .replace(/[^a-z0-9-]+/g, "-") + .replace(/^-+|-+$/g, ""); + return clean === "" ? fallback : clean; +} + +const raw = fs.readFileSync(CONTENT_PATH, "utf8"); +const content = JSON.parse(raw); +fs.mkdirSync(OUT_DIR, { recursive: true }); + +const report = { saved: [], failed: [], skipped: 0 }; + +for (const cinema of content.cinemas ?? []) { + const photos = cinema.photos ?? []; + for (let index = 0; index < photos.length; index++) { + const url = photos[index]; + if (!/^https?:\/\//i.test(url)) { + report.skipped++; + continue; + } + + const result = await download(url); + if (!result.ok) { + report.failed.push({ slug: cinema.slug, index, reason: result.reason }); + console.log(`พัง ${cinema.slug} #${index} — ${result.reason}`); + continue; + } + + const ext = EXT_BY_TYPE[result.type] ?? "jpg"; + const stem = slugifyName(cinema.slug, "cinema"); + const fileName = photos.length > 1 ? `${stem}-${index + 1}.${ext}` : `${stem}.${ext}`; + const filePath = path.join(OUT_DIR, fileName); + const publicPath = `${PUBLIC_PREFIX}/${fileName}`; + + if (!DRY) { + fs.writeFileSync(filePath, result.buffer); + photos[index] = publicPath; + } + report.saved.push({ slug: cinema.slug, index, publicPath, bytes: result.buffer.byteLength }); + console.log( + `OK ${cinema.slug} #${index} → ${publicPath} (${Math.round(result.buffer.byteLength / 1024)} KB)`, + ); + await sleep(DELAY_MS); + } +} + +if (!DRY) { + const stamp = new Date().toISOString().replace(/[:.]/g, "-"); + fs.writeFileSync(`${CONTENT_PATH}.bak-photos-${stamp}`, raw); + fs.writeFileSync(CONTENT_PATH, `${JSON.stringify(content, null, 2)}\n`); + console.log(`\nสำรองข้อมูลเดิม: ${CONTENT_PATH}.bak-photos-${stamp}`); +} + +console.log(`\nสรุป: บันทึก ${report.saved.length} · พัง ${report.failed.length} · ข้าม ${report.skipped}`); +if (report.failed.length > 0) { + console.log("รูปที่ยังพัง (หน้าเว็บจะแสดงโลโก้แทน): "); + for (const row of report.failed) console.log(` ${row.slug} #${row.index}`); +}