สาเหตุ: photos[] อ้างลิงก์ hotlink (googleusercontent/gstatic/fbcdn ฯลฯ) ซึ่งตอบ 200 แต่ 0 ไบต์เมื่อลิงก์หมดอายุ/ถูกจำกัด rate → รูปแตกเงียบ ๆ เช่น /cinema/phahol-theatre - scripts/mirror-photos.mjs: ดาวน์โหลด 20 รูปจาก 12 โรง มาเก็บ public/images/cinemas แล้วแก้ content.json ชี้ path ในโปรเจกต์ (สำรอง .bak-photos-* ก่อนเขียน) → เหลือรูปภายนอก 0 - บีบอัดรูปใหญ่ (<=1600px, q82): 6.12 MB → 2.91 MB - PhotoImage: img + onError fallback เป็นโลโก้ (ใช้ใน CinemaCard/Gallery) กันรูปแตกทุกกรณี - scripts/audit-photos.mjs: ตรวจรูปเสีย exit 1 - DESIGN.md: กฎรูปภาพ (ห้าม hotlink, ต้องใช้ PhotoImage, เพดานขนาด)
100 lines
3.7 KiB
JavaScript
100 lines
3.7 KiB
JavaScript
/**
|
|
* ตรวจว่ารูปที่อ้างจากภายนอก (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/<slug>.jpg แล้วเปลี่ยน photos[] เป็นพาธนั้น",
|
|
);
|
|
process.exit(1);
|
|
}
|