Spaces:
Running
Running
Update use_template_app.py
Browse files- use_template_app.py +61 -5
use_template_app.py
CHANGED
|
@@ -20,6 +20,46 @@ TEMP_DIR = pathlib.Path("temp_gradio_app_files") # Renamed for clarity
|
|
| 20 |
# Ensure temp directory exists
|
| 21 |
TEMP_DIR.mkdir(parents=True, exist_ok=True) # No need to clean it aggressively on each start
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
# --- Helper Functions ---
|
| 24 |
|
| 25 |
def sanitize_url(url_string: str) -> str:
|
|
@@ -72,7 +112,20 @@ def fetch_template_details(m_param, u_param, category_param, name_param):
|
|
| 72 |
try:
|
| 73 |
response = requests.get(api_url, timeout=10)
|
| 74 |
response.raise_for_status()
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
except requests.exceptions.RequestException as e:
|
| 77 |
print(f"Error fetching template details from {api_url}: {e}")
|
| 78 |
return None
|
|
@@ -80,7 +133,9 @@ def fetch_template_details(m_param, u_param, category_param, name_param):
|
|
| 80 |
def download_svg_content(svg_url):
|
| 81 |
"""Downloads the raw SVG content from a given URL."""
|
| 82 |
try:
|
| 83 |
-
|
|
|
|
|
|
|
| 84 |
response.raise_for_status()
|
| 85 |
return response.text # SVG is XML text
|
| 86 |
except requests.exceptions.RequestException as e:
|
|
@@ -334,11 +389,11 @@ def initial_load_template_info(request: gr.Request):
|
|
| 334 |
u = query_params.get("u")
|
| 335 |
category = query_params.get("category")
|
| 336 |
name = query_params.get("name") # This is the template name
|
| 337 |
-
|
| 338 |
if not all([m, u, category, name]):
|
| 339 |
print("Initial load: URL parameters (m, u, category, name) are incomplete or missing.")
|
| 340 |
return None, "无模板信息 (请检查URL参数)", None, "无模板"
|
| 341 |
-
|
| 342 |
details = fetch_template_details(m, u, category, name)
|
| 343 |
|
| 344 |
local_thumbnail_path = None
|
|
@@ -348,10 +403,11 @@ def initial_load_template_info(request: gr.Request):
|
|
| 348 |
if details and "name" in details:
|
| 349 |
template_display_name = details["name"] # Use name from details for display
|
| 350 |
svg_download_url = details.get("svg_download_url")
|
| 351 |
-
|
| 352 |
if details.get("thumbnail_url"):
|
| 353 |
try:
|
| 354 |
thumb_url = details["thumbnail_url"]
|
|
|
|
| 355 |
print(f"Fetching thumbnail from: {thumb_url}")
|
| 356 |
thumb_response = requests.get(thumb_url, timeout=10)
|
| 357 |
thumb_response.raise_for_status()
|
|
|
|
| 20 |
# Ensure temp directory exists
|
| 21 |
TEMP_DIR.mkdir(parents=True, exist_ok=True) # No need to clean it aggressively on each start
|
| 22 |
|
| 23 |
+
# --- Helper Functions ---
|
| 24 |
+
def fix_internal_urls(url_string: str) -> str:
|
| 25 |
+
"""
|
| 26 |
+
Replaces internal IP address URLs (like http://10.10.71.201:8002) with the external API base URL.
|
| 27 |
+
This makes internal URLs accessible externally.
|
| 28 |
+
"""
|
| 29 |
+
if not url_string or not isinstance(url_string, str):
|
| 30 |
+
return url_string
|
| 31 |
+
|
| 32 |
+
try:
|
| 33 |
+
# Parse the URL
|
| 34 |
+
parts = urllib.parse.urlsplit(url_string)
|
| 35 |
+
|
| 36 |
+
# Check if the netloc (domain/IP) looks like an internal address
|
| 37 |
+
# This covers internal IPs (10.x.x.x, 172.16-31.x.x, 192.168.x.x) and localhost
|
| 38 |
+
internal_patterns = ['10.', '172.16.', '172.17.', '172.18.', '172.19.', '172.2', '172.3', '192.168.', 'localhost', '127.0.0.1']
|
| 39 |
+
is_internal = any(parts.netloc.startswith(pattern) for pattern in internal_patterns)
|
| 40 |
+
|
| 41 |
+
if is_internal and parts.scheme in ['http', 'https']:
|
| 42 |
+
# Keep the path and query components, but replace the domain with TEMPLATE_API_BASE_URL
|
| 43 |
+
# First, get the external base URL parts
|
| 44 |
+
base_parts = urllib.parse.urlsplit(TEMPLATE_API_BASE_URL)
|
| 45 |
+
|
| 46 |
+
# Create new URL with base URL's scheme and netloc, but original path and query
|
| 47 |
+
new_url = urllib.parse.urlunsplit((
|
| 48 |
+
base_parts.scheme,
|
| 49 |
+
base_parts.netloc,
|
| 50 |
+
parts.path,
|
| 51 |
+
parts.query,
|
| 52 |
+
parts.fragment
|
| 53 |
+
))
|
| 54 |
+
|
| 55 |
+
print(f"URL transformed: {url_string} -> {new_url}")
|
| 56 |
+
return new_url
|
| 57 |
+
|
| 58 |
+
return url_string
|
| 59 |
+
except Exception as e:
|
| 60 |
+
print(f"Error fixing internal URL '{url_string}': {e}")
|
| 61 |
+
return url_string
|
| 62 |
+
|
| 63 |
# --- Helper Functions ---
|
| 64 |
|
| 65 |
def sanitize_url(url_string: str) -> str:
|
|
|
|
| 112 |
try:
|
| 113 |
response = requests.get(api_url, timeout=10)
|
| 114 |
response.raise_for_status()
|
| 115 |
+
template_data = response.json()
|
| 116 |
+
|
| 117 |
+
# Fix internal URLs in the template details
|
| 118 |
+
if isinstance(template_data, dict):
|
| 119 |
+
if "thumbnail_url" in template_data:
|
| 120 |
+
template_data["thumbnail_url"] = fix_internal_urls(template_data["thumbnail_url"])
|
| 121 |
+
if "svg_download_url" in template_data:
|
| 122 |
+
template_data["svg_download_url"] = fix_internal_urls(template_data["svg_download_url"])
|
| 123 |
+
# Handle any other URLs that might be in the response
|
| 124 |
+
for key, value in template_data.items():
|
| 125 |
+
if isinstance(value, str) and (value.startswith("http://") or value.startswith("https://")):
|
| 126 |
+
template_data[key] = fix_internal_urls(value)
|
| 127 |
+
|
| 128 |
+
return template_data
|
| 129 |
except requests.exceptions.RequestException as e:
|
| 130 |
print(f"Error fetching template details from {api_url}: {e}")
|
| 131 |
return None
|
|
|
|
| 133 |
def download_svg_content(svg_url):
|
| 134 |
"""Downloads the raw SVG content from a given URL."""
|
| 135 |
try:
|
| 136 |
+
# Fix any internal URLs before downloading
|
| 137 |
+
fixed_url = fix_internal_urls(svg_url)
|
| 138 |
+
response = requests.get(fixed_url, timeout=10)
|
| 139 |
response.raise_for_status()
|
| 140 |
return response.text # SVG is XML text
|
| 141 |
except requests.exceptions.RequestException as e:
|
|
|
|
| 389 |
u = query_params.get("u")
|
| 390 |
category = query_params.get("category")
|
| 391 |
name = query_params.get("name") # This is the template name
|
| 392 |
+
|
| 393 |
if not all([m, u, category, name]):
|
| 394 |
print("Initial load: URL parameters (m, u, category, name) are incomplete or missing.")
|
| 395 |
return None, "无模板信息 (请检查URL参数)", None, "无模板"
|
| 396 |
+
|
| 397 |
details = fetch_template_details(m, u, category, name)
|
| 398 |
|
| 399 |
local_thumbnail_path = None
|
|
|
|
| 403 |
if details and "name" in details:
|
| 404 |
template_display_name = details["name"] # Use name from details for display
|
| 405 |
svg_download_url = details.get("svg_download_url")
|
| 406 |
+
|
| 407 |
if details.get("thumbnail_url"):
|
| 408 |
try:
|
| 409 |
thumb_url = details["thumbnail_url"]
|
| 410 |
+
# thumb_url is already fixed in fetch_template_details
|
| 411 |
print(f"Fetching thumbnail from: {thumb_url}")
|
| 412 |
thumb_response = requests.get(thumb_url, timeout=10)
|
| 413 |
thumb_response.raise_for_status()
|