#!/usr/bin/env python3 """ย้ายรูปออกจาก repo → ชี้ไป object storage (SeaweedFS) ใช้ครั้งเดียว (2026-11-27) แล้วเก็บไว้เป็นบันทึก: python scripts/media-to-storage.py --dry-run python scripts/media-to-storage.py แทนที่ /images/... → https://storage.hobbypj.com/hobbypj/alterflixhub/images/... /og/... → https://storage.hobbypj.com/hobbypj/alterflixhub/og/... https://alterflixhub.hobbypj.com/og/ → https://storage.hobbypj.com/hobbypj/alterflixhub/og/ ไม่แตะ docs/ (เป็นเอกสาร ไม่ใช่หน้าจริง) และไม่แตะ URL ที่ชี้ storage อยู่แล้ว """ from __future__ import annotations import argparse import re from pathlib import Path REPO = Path(__file__).resolve().parents[1] STORAGE = "https://storage.hobbypj.com/hobbypj/alterflixhub" SUFFIXES = {".html", ".js", ".json", ".webmanifest", ".xml", ".txt"} SKIP_DIRS = {".git", "docs", "scripts", "node_modules", "_backup"} RULES = [ (re.compile(r"https://alterflixhub\.hobbypj\.com/og/"), STORAGE + "/og/"), (re.compile(r"(? int: ap = argparse.ArgumentParser() ap.add_argument("--dry-run", action="store_true") ap.add_argument("--base", default=STORAGE, help="เปลี่ยน base ได้ (default = production)") args = ap.parse_args() storage = args.base.rstrip("/") total_files = 0 total_hits = 0 per_file = [] for path in sorted(REPO.rglob("*")): if not path.is_file() or path.suffix.lower() not in SUFFIXES: continue rel = path.relative_to(REPO) if any(part in SKIP_DIRS for part in rel.parts): continue with path.open(encoding="utf-8", newline="") as fh: text = fh.read() hits = 0 new = text for pattern, repl in RULES: target = repl if repl.startswith("http") else repl if repl == STORAGE + "/og/" or repl == STORAGE + "/images/": # เคารพ --base tail = "/og/" if repl.endswith("/og/") else "/images/" target = storage + tail new, n = pattern.subn(lambda _m, t=target: t, new) hits += n if new != text: total_files += 1 total_hits += hits per_file.append((str(rel), hits)) if not args.dry_run: path.write_text(new, encoding="utf-8", newline="") mode = "[dry-run] " if args.dry_run else "" print(f"{mode}แก้ {total_files} ไฟล์ · {total_hits} จุด → {storage}") for rel, hits in per_file[:20]: print(f" • {rel} ({hits})") if len(per_file) > 20: print(f" … อีก {len(per_file) - 20} ไฟล์") return 0 if __name__ == "__main__": raise SystemExit(main())