Spaces:
Sleeping
Sleeping
File size: 2,171 Bytes
3c5146b 386a178 3c5146b 386a178 3c5146b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | """Share link encoding/decoding — base64-encodes compressed plan JSON for URL sharing."""
import base64
import json
import zlib
from urllib.parse import parse_qs, urlparse
from core.planner import ProjectPlan
def encode_plan(plan: ProjectPlan) -> str:
"""
Encode a ProjectPlan to a URL-safe base64 string.
Process: model_dump(exclude_none=True) -> json.dumps -> zlib.compress -> base64 urlsafe encode
Returns encoded string, or empty string on failure.
"""
try:
plan_dict = plan.model_dump(exclude_none=True)
json_bytes = json.dumps(plan_dict, separators=(",", ":")).encode("utf-8")
compressed = zlib.compress(json_bytes)
token = base64.urlsafe_b64encode(compressed).decode("ascii")
return token
except Exception:
return ""
def decode_plan(token: str) -> ProjectPlan | None:
"""
Decode a share token back to a ProjectPlan.
Process: base64 urlsafe decode -> zlib.decompress -> json.loads -> parse_plan_json
Returns ProjectPlan on success, None on any failure.
"""
try:
compressed = base64.urlsafe_b64decode(token.encode("ascii"))
json_bytes = zlib.decompress(compressed)
plan_dict = json.loads(json_bytes.decode("utf-8"))
# plan_dict is already a dict — use model_validate directly
return ProjectPlan.model_validate(plan_dict)
except Exception:
return None
def build_share_url(base_url: str, plan: ProjectPlan) -> str:
"""
Build a full share URL: {base_url}?share={encoded_token}
Returns empty string if encoding fails.
"""
token = encode_plan(plan)
if not token:
return ""
separator = "&" if "?" in base_url else "?"
return f"{base_url}{separator}share={token}"
def parse_share_url(url: str) -> ProjectPlan | None:
"""
Extract and decode the ?share= token from a URL.
Returns ProjectPlan or None.
"""
try:
parsed = urlparse(url)
params = parse_qs(parsed.query)
tokens = params.get("share", [])
if not tokens:
return None
return decode_plan(tokens[0])
except Exception:
return None
|