- 8 หน้า (home/stops/routes/stop/check/blog/about/credits) แปลง partials → React component ตรง mock
- ตรรกะพอร์ตเป็น src/lib/ui/*.ts (mount()) + runtime/PageRuntime
- CSS: base + pages/*.css import ต่อ route (คงลำดับ cascade ของ mock)
- asset/content: public/{image,resource/data,content,locales,vendor}
- แก้ path รูปไทล์พาร์ทเนอร์ (.image → /image) + fallback absolute
- ลบ UI เดิมที่ไม่ตรง mock (expanded/stops details/old components/globals.css)
- script/sync_data_to_postgres.mjs + sync_images_to_storage.mjs (dry-run/--apply, backup)
- docs/UI-PORT.md + DESIGN.md + README
136 lines
5.1 KiB
Python
136 lines
5.1 KiB
Python
#!/usr/bin/env python3
|
|
"""อัปโหลดรูปชุด template (frontend/public/image) เข้า SeaweedFS ผ่าน backend API.
|
|
|
|
รูปจะถูกเก็บใน core_management storage (SeaweedFS) แล้ว frontend อ่านผ่าน `/files/...`
|
|
ค่าเริ่มต้นเป็น dry-run — ต้องใส่ --apply ถึงจะอัปโหลดจริง
|
|
|
|
ตัวอย่าง:
|
|
python script/import_assets_to_storage.py --api http://localhost:8080/api --dry-run
|
|
python script/import_assets_to_storage.py --api http://localhost:8080/api --apply
|
|
|
|
รูปแบบโฟลเดอร์ -> entity:
|
|
image/stations/<station_id>.jpg -> entity_type=station, entity_id=<station_id>
|
|
image/routes/<route_id>/<file> -> entity_type=route, entity_id=<route_id>
|
|
image/stops/<code>.jpg -> entity_type=stop, entity_id=<code>
|
|
image/qrcode/<route_id>.png -> entity_type=route, entity_id=<route_id> (kind=qr)
|
|
image/{icons,partners,network,landmarks,extras}/... -> ข้าม (เป็น asset ของเว็บ ไม่ใช่รูป entity)
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
import mimetypes
|
|
import os
|
|
import sys
|
|
import urllib.error
|
|
import urllib.request
|
|
import uuid
|
|
from pathlib import Path
|
|
|
|
SKIP_DIRS = {"icons", "partners", "network", "landmarks", "extras"}
|
|
IMAGE_EXTS = {".jpg", ".jpeg", ".png", ".webp"}
|
|
|
|
# โฟลเดอร์ -> (entity_type, kind)
|
|
AREA_MAP = {
|
|
"stations": ("station", "photo"),
|
|
"routes": ("route", "photo"),
|
|
"stops": ("stop", "photo"),
|
|
"qrcode": ("route", "qr"),
|
|
}
|
|
|
|
|
|
def iter_assets(root: Path):
|
|
for area_dir in sorted(p for p in root.iterdir() if p.is_dir()):
|
|
area = area_dir.name
|
|
if area in SKIP_DIRS or area not in AREA_MAP:
|
|
continue
|
|
entity_type, kind = AREA_MAP[area]
|
|
for path in sorted(area_dir.rglob("*")):
|
|
if not path.is_file() or path.suffix.lower() not in IMAGE_EXTS:
|
|
continue
|
|
rel = path.relative_to(area_dir)
|
|
if area == "routes" and len(rel.parts) > 1:
|
|
entity_id = rel.parts[0]
|
|
else:
|
|
entity_id = path.stem
|
|
yield path, entity_type, entity_id, kind
|
|
|
|
|
|
def post_multipart(api: str, path: Path, entity_type: str, entity_id: str, kind: str) -> dict:
|
|
boundary = f"----atroute{uuid.uuid4().hex}"
|
|
mime = mimetypes.guess_type(path.name)[0] or "application/octet-stream"
|
|
parts: list[bytes] = []
|
|
|
|
def field(name: str, value: str) -> None:
|
|
parts.append(
|
|
f"--{boundary}\r\nContent-Disposition: form-data; name=\"{name}\"\r\n\r\n{value}\r\n".encode()
|
|
)
|
|
|
|
field("entity_type", entity_type)
|
|
field("entity_id", entity_id)
|
|
field("kind", kind)
|
|
field("display_order", "0")
|
|
parts.append(
|
|
(
|
|
f"--{boundary}\r\nContent-Disposition: form-data; name=\"file\"; filename=\"{path.name}\"\r\n"
|
|
f"Content-Type: {mime}\r\n\r\n"
|
|
).encode()
|
|
)
|
|
parts.append(path.read_bytes())
|
|
parts.append(f"\r\n--{boundary}--\r\n".encode())
|
|
body = b"".join(parts)
|
|
|
|
req = urllib.request.Request(
|
|
f"{api.rstrip('/')}/images",
|
|
data=body,
|
|
method="POST",
|
|
headers={"Content-Type": f"multipart/form-data; boundary={boundary}"},
|
|
)
|
|
with urllib.request.urlopen(req, timeout=120) as res:
|
|
return json.loads(res.read().decode("utf-8") or "{}")
|
|
|
|
|
|
def main() -> int:
|
|
ap = argparse.ArgumentParser()
|
|
ap.add_argument("--api", default=os.environ.get("API_BASE", "http://localhost:8080/api"))
|
|
ap.add_argument("--dir", default="frontend/public/image")
|
|
ap.add_argument("--apply", action="store_true", help="อัปโหลดจริง (default = dry-run)")
|
|
ap.add_argument("--limit", type=int, default=0)
|
|
args = ap.parse_args()
|
|
|
|
root = Path(args.dir)
|
|
if not root.is_dir():
|
|
print(f"ไม่พบโฟลเดอร์: {root}", file=sys.stderr)
|
|
return 2
|
|
|
|
items = list(iter_assets(root))
|
|
if args.limit:
|
|
items = items[: args.limit]
|
|
|
|
ok = fail = 0
|
|
for path, entity_type, entity_id, kind in items:
|
|
label = f"{entity_type}/{entity_id}/{path.name} [{kind}]"
|
|
if not args.apply:
|
|
print(f"[dry-run] {label}")
|
|
continue
|
|
try:
|
|
res = post_multipart(args.api, path, entity_type, entity_id, kind)
|
|
print(f"[ok] {label} -> {res.get('url_path') or res.get('id') or 'uploaded'}")
|
|
ok += 1
|
|
except urllib.error.HTTPError as exc:
|
|
body = exc.read().decode("utf-8", "replace")[:200]
|
|
print(f"[fail] {label} HTTP {exc.code} {body}", file=sys.stderr)
|
|
fail += 1
|
|
except Exception as exc: # noqa: BLE001 - CLI
|
|
print(f"[fail] {label} {exc}", file=sys.stderr)
|
|
fail += 1
|
|
|
|
total = len(items)
|
|
print(f"\nทั้งหมด {total} ไฟล์ · สำเร็จ {ok} · ล้มเหลว {fail}" + (" (dry-run)" if not args.apply else ""))
|
|
return 0 if fail == 0 else 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|