Spaces:
Running
Running
Normalize GitHub asset URLs
#6
by Dontcryx - opened
- backend/Odin.py +15 -2
- backend/pathfinder.py +13 -1
backend/Odin.py
CHANGED
|
@@ -14,6 +14,7 @@ import socket
|
|
| 14 |
from contextlib import asynccontextmanager
|
| 15 |
from datetime import datetime, timedelta
|
| 16 |
from pathlib import Path
|
|
|
|
| 17 |
from fastapi import FastAPI, WebSocket, WebSocketDisconnect, Query, Depends, HTTPException, Header
|
| 18 |
from fastapi.responses import FileResponse, JSONResponse
|
| 19 |
from pydantic import BaseModel, Field
|
|
@@ -31,6 +32,18 @@ FRONTEND = os.path.join(ROOT, "frontend")
|
|
| 31 |
DATA_DIR = os.path.join(ROOT, "backend", "data")
|
| 32 |
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
# --------------------------------------------------------------------------- #
|
| 35 |
# Sim Manager — connection broadcast + background WorldEngine
|
| 36 |
# --------------------------------------------------------------------------- #
|
|
@@ -971,8 +984,8 @@ def get_asset_urls():
|
|
| 971 |
a Space variable does not require committing image files to the Space.
|
| 972 |
"""
|
| 973 |
return {
|
| 974 |
-
"map_image_url": os.environ.get("MAP_IMAGE_URL", "")
|
| 975 |
-
"night_image_url": os.environ.get("MAP_NIGHT_IMAGE_URL", "")
|
| 976 |
}
|
| 977 |
|
| 978 |
|
|
|
|
| 14 |
from contextlib import asynccontextmanager
|
| 15 |
from datetime import datetime, timedelta
|
| 16 |
from pathlib import Path
|
| 17 |
+
from urllib.parse import urlparse
|
| 18 |
from fastapi import FastAPI, WebSocket, WebSocketDisconnect, Query, Depends, HTTPException, Header
|
| 19 |
from fastapi.responses import FileResponse, JSONResponse
|
| 20 |
from pydantic import BaseModel, Field
|
|
|
|
| 32 |
DATA_DIR = os.path.join(ROOT, "backend", "data")
|
| 33 |
|
| 34 |
|
| 35 |
+
def _public_asset_url(value: str) -> str:
|
| 36 |
+
"""Return a browser-loadable URL, correcting GitHub's HTML blob links."""
|
| 37 |
+
value = value.strip()
|
| 38 |
+
parsed = urlparse(value)
|
| 39 |
+
parts = parsed.path.strip("/").split("/")
|
| 40 |
+
if parsed.scheme == "https" and parsed.netloc in {"github.com", "www.github.com"} \
|
| 41 |
+
and len(parts) >= 5 and parts[2] == "blob":
|
| 42 |
+
owner, repository, _, revision, *path = parts
|
| 43 |
+
return f"https://raw.githubusercontent.com/{owner}/{repository}/{revision}/{'/'.join(path)}"
|
| 44 |
+
return value
|
| 45 |
+
|
| 46 |
+
|
| 47 |
# --------------------------------------------------------------------------- #
|
| 48 |
# Sim Manager — connection broadcast + background WorldEngine
|
| 49 |
# --------------------------------------------------------------------------- #
|
|
|
|
| 984 |
a Space variable does not require committing image files to the Space.
|
| 985 |
"""
|
| 986 |
return {
|
| 987 |
+
"map_image_url": _public_asset_url(os.environ.get("MAP_IMAGE_URL", "")),
|
| 988 |
+
"night_image_url": _public_asset_url(os.environ.get("MAP_NIGHT_IMAGE_URL", "")),
|
| 989 |
}
|
| 990 |
|
| 991 |
|
backend/pathfinder.py
CHANGED
|
@@ -20,6 +20,18 @@ _W = _H = 0
|
|
| 20 |
_load_lock = threading.Lock()
|
| 21 |
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
def _load():
|
| 24 |
global _path_img, _white_pixels, _W, _H
|
| 25 |
if _white_pixels is not None:
|
|
@@ -35,7 +47,7 @@ def _load():
|
|
| 35 |
os.path.join(ROOT, "frontend", "dist", "path.png"),
|
| 36 |
]
|
| 37 |
path_file = next((p for p in candidates if os.path.exists(p)), None)
|
| 38 |
-
path_url = os.environ.get("PATH_IMAGE_URL", "")
|
| 39 |
try:
|
| 40 |
if path_file:
|
| 41 |
image_source = path_file
|
|
|
|
| 20 |
_load_lock = threading.Lock()
|
| 21 |
|
| 22 |
|
| 23 |
+
def _public_asset_url(value):
|
| 24 |
+
"""Convert a GitHub blob page URL to its downloadable raw-file URL."""
|
| 25 |
+
value = value.strip()
|
| 26 |
+
parsed = urlparse(value)
|
| 27 |
+
parts = parsed.path.strip("/").split("/")
|
| 28 |
+
if parsed.scheme == "https" and parsed.netloc in {"github.com", "www.github.com"} \
|
| 29 |
+
and len(parts) >= 5 and parts[2] == "blob":
|
| 30 |
+
owner, repository, _, revision, *path = parts
|
| 31 |
+
return f"https://raw.githubusercontent.com/{owner}/{repository}/{revision}/{'/'.join(path)}"
|
| 32 |
+
return value
|
| 33 |
+
|
| 34 |
+
|
| 35 |
def _load():
|
| 36 |
global _path_img, _white_pixels, _W, _H
|
| 37 |
if _white_pixels is not None:
|
|
|
|
| 47 |
os.path.join(ROOT, "frontend", "dist", "path.png"),
|
| 48 |
]
|
| 49 |
path_file = next((p for p in candidates if os.path.exists(p)), None)
|
| 50 |
+
path_url = _public_asset_url(os.environ.get("PATH_IMAGE_URL", ""))
|
| 51 |
try:
|
| 52 |
if path_file:
|
| 53 |
image_source = path_file
|