Files
web-alterflixhub/scripts/media-to-storage.py
T
Bkk1234 ddfb0a5053 refactor(media): แยกรูปออกจาก code — รูปทั้งหมดอยู่บน object storage
code อยู่ใน git · รูปอยู่บน SeaweedFS (https://storage.hobbypj.com/hobbypj/alterflixhub/)

- อัปโหลด images/ (21 ไฟล์) + og/ (1 ไฟล์) ขึ้น storage แล้วตรวจ sha256 ตรงทุกไฟล์
- แทนที่อ้างอิง 894 จุด ใน 37 ไฟล์: /images/… → <storage>/images/…, /og/… → <storage>/og/…
  (absolute URL ทุกที่ รวม JSON-LD/og:image/twitter:image และ placeholder ในหลังบ้าน)
- ลบ images/ + og/ ออกจาก repo · สำเนาในเครื่องย้ายไป media/ (gitignored)
- scripts/media-to-storage.py: สคริปต์ที่ใช้ย้าย (บันทึกวิธี + ใช้ซ้ำได้เมื่อเปลี่ยน base)
- scripts/media-sync.py: --check / --upload รูปขึ้น storage (basic auth หรือ SSH tunnel ไป filer)
- sw.js: bump v2.1.6 + แคชรูปจาก storage แบบ cache-first (isMedia/mediaCacheFirst) กันออฟไลน์แล้วรูปหาย
- docker/traefik-storage-alterflixhub.yml: router เปิดอ่านสาธารณะเฉพาะ /hobbypj/alterflixhub/
  (host storage บังคับ Basic auth ทั้ง host — ต้องมี router priority 300 + CORS ก่อน)
- docs/media-storage.md: วิธีอัปโหลด/เพิ่มรูป + ข้อกำหนดฝั่ง Traefik
- .gitignore/.dockerignore: กัน media/, images/, og/ กลับเข้า repo/image

ไอคอน PWA (icon-*.png, favicon.svg) ยังอยู่ใน repo — ต้อง same-origin ให้ติดตั้ง PWA ได้
2026-11-27 02:08:19 +07:00

80 lines
3.1 KiB
Python

#!/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"(?<![\w./-])/og/"), STORAGE + "/og/"),
(re.compile(r"(?<![\w./-])/images/"), STORAGE + "/images/"),
]
def main() -> 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())