219 lines
6.9 KiB
Python
219 lines
6.9 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Upload images from script/images/ to SeaweedFS filer and generate
|
|
seed_images.sql with INSERT statements for the images table.
|
|
|
|
Usage:
|
|
python script/upload_seed_images.py --filer-url http://VPS_IP:3103
|
|
python script/upload_seed_images.py --filer-url http://VPS_IP:3103 --dry-run
|
|
|
|
Output:
|
|
script/seed_images.sql (always written)
|
|
"""
|
|
|
|
import argparse
|
|
import hashlib
|
|
import mimetypes
|
|
import os
|
|
import sys
|
|
import uuid
|
|
from pathlib import Path
|
|
|
|
try:
|
|
import requests
|
|
except ImportError:
|
|
print("pip install requests", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
SCRIPT_DIR = Path(__file__).parent
|
|
IMAGES_DIR = SCRIPT_DIR / "images"
|
|
OUTPUT_SQL = SCRIPT_DIR / "seed_images.sql"
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
# folder → (entity_type, kind, display_order_hint)
|
|
# --------------------------------------------------------------------------- #
|
|
FOLDER_MAP = {
|
|
"stops": ("station", "photo", 0),
|
|
"qrcode": ("route", "map", 9),
|
|
"partners": ("sponsor", "logo", 0),
|
|
"landmarks": ("misc", "photo", 0),
|
|
"network": ("misc", "map", 0),
|
|
"extras": ("misc", "photo", 0),
|
|
"icons": ("misc", "icon", 0),
|
|
}
|
|
|
|
def classify(rel: Path):
|
|
"""Return (entity_type, entity_id, kind, display_order) or None to skip."""
|
|
parts = rel.parts # e.g. ('routes', '902', 'car.jpeg')
|
|
folder = parts[0]
|
|
|
|
if folder == "routes" and len(parts) == 3:
|
|
route_id = parts[1]
|
|
stem = Path(parts[2]).stem.lower()
|
|
kind = "hero" if stem == "hero" else "photo"
|
|
order = 1 if kind == "hero" else 0
|
|
return "route", route_id, kind, order
|
|
|
|
if folder in FOLDER_MAP and len(parts) == 2:
|
|
entity_type, kind, order = FOLDER_MAP[folder]
|
|
entity_id = Path(parts[1]).stem
|
|
return entity_type, entity_id, kind, order
|
|
|
|
return None
|
|
|
|
|
|
def get_mime(path: Path) -> str:
|
|
# mimetypes doesn't know .webp by default on some platforms
|
|
ext = path.suffix.lower()
|
|
if ext == ".webp":
|
|
return "image/webp"
|
|
mime, _ = mimetypes.guess_type(str(path))
|
|
return mime or "application/octet-stream"
|
|
|
|
|
|
def sha256_of(path: Path) -> str:
|
|
h = hashlib.sha256()
|
|
with open(path, "rb") as f:
|
|
for chunk in iter(lambda: f.read(65536), b""):
|
|
h.update(chunk)
|
|
return h.hexdigest()
|
|
|
|
|
|
def get_dimensions(path: Path):
|
|
try:
|
|
from PIL import Image
|
|
with Image.open(path) as img:
|
|
return img.size # (width, height)
|
|
except Exception:
|
|
return None, None
|
|
|
|
|
|
def upload(filer_url: str, entity_type: str, entity_id: str,
|
|
filename: str, mime: str, data: bytes) -> dict:
|
|
"""
|
|
PUT raw bytes to filer — mirrors seaweed.rs upload logic.
|
|
Returns {"fid": ..., "url_path": ...}
|
|
"""
|
|
key = (f"/buckets/transit/{sanitize(entity_type)}"
|
|
f"/{sanitize(entity_id)}/{sanitize_name(filename)}")
|
|
url = filer_url.rstrip("/") + key
|
|
resp = requests.put(url, data=data,
|
|
headers={"Content-Type": mime}, timeout=30)
|
|
resp.raise_for_status()
|
|
body = {}
|
|
try:
|
|
body = resp.json()
|
|
except Exception:
|
|
pass
|
|
fid = body.get("fid") or key # fallback: path itself (matches Rust code)
|
|
url_path = key.removeprefix("/buckets/transit") # stored in DB without prefix
|
|
return {"fid": fid, "url_path": url_path}
|
|
|
|
|
|
def sanitize(s: str) -> str:
|
|
return "".join(c if c.isalnum() or c in "-_" else "_" for c in s)
|
|
|
|
|
|
def sanitize_name(s: str) -> str:
|
|
return "".join(c if c.isalnum() or c in "-_." else "_" for c in s)
|
|
|
|
|
|
def q(s: str) -> str:
|
|
"""SQL single-quote escape."""
|
|
return s.replace("'", "''")
|
|
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--filer-url", default="http://localhost:3103",
|
|
help="SeaweedFS filer base URL (default: http://localhost:3103)")
|
|
parser.add_argument("--dry-run", action="store_true",
|
|
help="Skip actual upload; use path as fid placeholder")
|
|
args = parser.parse_args()
|
|
|
|
filer_url = args.filer_url.rstrip("/")
|
|
dry_run = args.dry_run
|
|
|
|
sql_lines = [
|
|
"-- Auto-generated by upload_seed_images.py",
|
|
"-- Run this AFTER 0003_seed.sql is applied.",
|
|
"-- ON CONFLICT (fid) DO NOTHING → safe to re-run.",
|
|
"",
|
|
]
|
|
|
|
image_files = sorted(p for p in IMAGES_DIR.rglob("*") if p.is_file())
|
|
ok = skip = 0
|
|
errors = []
|
|
|
|
for img_path in image_files:
|
|
rel = img_path.relative_to(IMAGES_DIR)
|
|
meta = classify(rel)
|
|
if meta is None:
|
|
print(f" SKIP {rel}")
|
|
skip += 1
|
|
continue
|
|
|
|
entity_type, entity_id, kind, display_order = meta
|
|
mime = get_mime(img_path)
|
|
file_size = img_path.stat().st_size
|
|
sha = sha256_of(img_path)
|
|
width, height = get_dimensions(img_path)
|
|
img_id = str(uuid.uuid4())
|
|
|
|
if dry_run:
|
|
safe_key = (f"/buckets/transit/{sanitize(entity_type)}"
|
|
f"/{sanitize(entity_id)}/{sanitize_name(img_path.name)}")
|
|
fid = safe_key # use path as pseudo-fid
|
|
url_path = safe_key.removeprefix("/buckets/transit")
|
|
print(f" DRY {rel}")
|
|
else:
|
|
try:
|
|
data = img_path.read_bytes()
|
|
result = upload(filer_url, entity_type, entity_id,
|
|
img_path.name, mime, data)
|
|
fid = result["fid"]
|
|
url_path = result["url_path"]
|
|
print(f" OK {rel} fid={fid}")
|
|
ok += 1
|
|
except Exception as exc:
|
|
print(f" ERR {rel} {exc}", file=sys.stderr)
|
|
errors.append((str(rel), str(exc)))
|
|
continue
|
|
|
|
# Build meta JSONB inline
|
|
meta_kv = [f'"mime":"{q(mime)}"', f'"size_bytes":{file_size}',
|
|
f'"sha256":"{sha}"']
|
|
if width is not None:
|
|
meta_kv.append(f'"width":{width}')
|
|
meta_kv.append(f'"height":{height}')
|
|
meta_json = "{" + ",".join(meta_kv) + "}"
|
|
|
|
sql_lines.append(f"-- {rel}")
|
|
sql_lines.append(
|
|
f"INSERT INTO images "
|
|
f"(id, entity_type, entity_id, kind, fid, url_path, display_order, meta) VALUES ("
|
|
f"'{img_id}', '{entity_type}', '{q(entity_id)}', '{kind}', "
|
|
f"'{q(fid)}', '{q(url_path)}', {display_order}, "
|
|
f"'{meta_json}'::jsonb"
|
|
f") ON CONFLICT (fid) DO NOTHING;"
|
|
)
|
|
|
|
sql_lines += [
|
|
"",
|
|
f"-- done: ok={ok if not dry_run else 'DRY'}, skip={skip}, err={len(errors)}",
|
|
]
|
|
|
|
OUTPUT_SQL.write_text("\n".join(sql_lines) + "\n", encoding="utf-8")
|
|
print(f"\nSQL written → {OUTPUT_SQL.relative_to(SCRIPT_DIR.parent)}")
|
|
if errors:
|
|
print(f"Errors ({len(errors)}):")
|
|
for r, e in errors:
|
|
print(f" {r}: {e}")
|
|
sys.exit(1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|