/** * Pull MST favicon + generate multi-size PNG icons from the faculty seal. */ import { copyFileSync, existsSync, mkdirSync, readdirSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { createRequire } from "node:module"; const require = createRequire(import.meta.url); const sharp = require("sharp"); const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); const outDir = path.join(root, "public", "brand", "icons"); mkdirSync(outDir, { recursive: true }); function findDownloadsLogo(name) { const downloads = path.join(process.env.USERPROFILE || "", "Downloads"); if (!existsSync(downloads)) return null; const folder = readdirSync(downloads, { withFileTypes: true }).find( (d) => d.isDirectory() && d.name.includes("شبکه"), ); if (!folder) return null; const base = path.join(downloads, folder.name); const filesDir = readdirSync(base, { withFileTypes: true }).find( (d) => d.isDirectory() && d.name.includes("_files"), ); if (!filesDir) return null; const p = path.join(base, filesDir.name, name); return existsSync(p) ? p : null; } async function fetchBuffer(url) { const res = await fetch(url, { headers: { "User-Agent": "Mozilla/5.0 AUT-OpenEdu-Favicon/1.0" }, redirect: "follow", }); if (!res.ok) throw new Error(`${url} → ${res.status}`); return Buffer.from(await res.arrayBuffer()); } const sizes = [16, 32, 48, 64, 128, 180, 192, 256, 512]; const logoFa = findDownloadsLogo("logo_fa.png"); const logoFaW = findDownloadsLogo("logo_fa_w.png"); if (!logoFa) { console.error("logo_fa.png not found in Downloads/شبکه اساتید"); process.exit(1); } // Copy originals into brand/ copyFileSync(logoFa, path.join(root, "public", "brand", "logo_fa.png")); if (logoFaW) { copyFileSync(logoFaW, path.join(root, "public", "brand", "logo_fa_w.png")); } console.log("copied logo_fa.png" + (logoFaW ? " + logo_fa_w.png" : "")); // Prefer white-on-dark seal for dark UI; dark-on-light for favicon (logo_fa) const source = logoFa; for (const size of sizes) { const dest = path.join(outDir, `favicon-${size}x${size}.png`); await sharp(source) .resize(size, size, { fit: "contain", background: { r: 0, g: 0, b: 0, alpha: 0 } }) .png() .toFile(dest); console.log("wrote", path.relative(root, dest)); } // Apple touch (180) + PWA-ish icons at public root aliases await sharp(source) .resize(180, 180, { fit: "contain", background: { r: 255, g: 255, b: 255, alpha: 1 } }) .png() .toFile(path.join(root, "public", "apple-touch-icon.png")); await sharp(source) .resize(192, 192, { fit: "contain", background: { r: 0, g: 0, b: 0, alpha: 0 } }) .png() .toFile(path.join(root, "public", "icon-192.png")); await sharp(source) .resize(512, 512, { fit: "contain", background: { r: 0, g: 0, b: 0, alpha: 0 } }) .png() .toFile(path.join(root, "public", "icon-512.png")); // Next.js app router file conventions await sharp(source) .resize(32, 32, { fit: "contain", background: { r: 0, g: 0, b: 0, alpha: 0 } }) .png() .toFile(path.join(root, "src", "app", "icon.png")); await sharp(source) .resize(180, 180, { fit: "contain", background: { r: 255, g: 255, b: 255, alpha: 1 } }) .png() .toFile(path.join(root, "src", "app", "apple-icon.png")); // Live MST favicon.ico try { const ico = await fetchBuffer("https://mst.aut.ac.ir/favicon.ico"); const icoPath = path.join(root, "public", "favicon.ico"); const { writeFileSync } = await import("node:fs"); writeFileSync(icoPath, ico); writeFileSync(path.join(root, "public", "MstAut_favicon.ico"), ico); writeFileSync(path.join(outDir, "favicon.ico"), ico); console.log("fetched mst.aut.ac.ir/favicon.ico (" + ico.length + " bytes)"); } catch (err) { console.warn("live favicon fetch failed, keeping existing:", err.message); // Fall back: copy existing if present const existing = path.join(root, "public", "MstAut_favicon.ico"); if (existsSync(existing)) { copyFileSync(existing, path.join(root, "public", "favicon.ico")); copyFileSync(existing, path.join(outDir, "favicon.ico")); console.log("copied local MstAut_favicon.ico"); } } // Brand icon used elsewhere await sharp(source) .resize(256, 256, { fit: "contain", background: { r: 0, g: 0, b: 0, alpha: 0 } }) .png() .toFile(path.join(root, "public", "brand", "mst-faculty-icon.png")); console.log("done → public/brand/icons/*");