Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2517,37 +2517,39 @@ load_products_data()
|
|
2517 |
|
2518 |
def get_product_image_path(product_name: str) -> str:
|
2519 |
"""
|
2520 |
-
Get the image
|
2521 |
-
Returns the
|
2522 |
"""
|
2523 |
try:
|
2524 |
-
# Create images directory if it doesn't exist
|
2525 |
-
images_dir = "static/images"
|
2526 |
-
os.makedirs(images_dir, exist_ok=True)
|
2527 |
-
|
2528 |
# Clean product name for filename
|
2529 |
-
safe_name = re.sub(r'[^\w\s-]', '', product_name).replace(' ', '_').
|
2530 |
-
|
2531 |
-
|
2532 |
-
|
2533 |
-
|
|
|
2534 |
for ext in image_extensions:
|
2535 |
-
|
2536 |
-
|
2537 |
-
|
2538 |
-
|
2539 |
-
|
2540 |
-
|
2541 |
-
|
2542 |
-
|
2543 |
-
|
2544 |
-
|
2545 |
-
|
2546 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
2547 |
return None
|
2548 |
-
|
2549 |
except Exception as e:
|
2550 |
-
logger.error(f"[Image] Error
|
2551 |
return None
|
2552 |
|
2553 |
def get_product_image_media_type(image_path: str) -> str:
|
|
|
2517 |
|
2518 |
def get_product_image_path(product_name: str) -> str:
|
2519 |
"""
|
2520 |
+
Get the public image URL for a product based on its name.
|
2521 |
+
Returns the public URL if the image exists, otherwise None or a default image URL.
|
2522 |
"""
|
2523 |
try:
|
|
|
|
|
|
|
|
|
2524 |
# Clean product name for filename
|
2525 |
+
safe_name = re.sub(r'[^\w\s-]', '', product_name).replace(' ', '%20').replace('_', '%20').strip()
|
2526 |
+
# List of possible extensions
|
2527 |
+
image_extensions = ['.png', '.jpg', '.jpeg', '.webp']
|
2528 |
+
# Base public URL for images
|
2529 |
+
base_url = "https://amgocus.com/uploads/images/"
|
2530 |
+
import requests
|
2531 |
for ext in image_extensions:
|
2532 |
+
image_url = f"{base_url}{safe_name}{ext}"
|
2533 |
+
try:
|
2534 |
+
resp = requests.head(image_url, timeout=5)
|
2535 |
+
if resp.status_code == 200:
|
2536 |
+
logger.info(f"[Image] Found public image URL: {image_url}")
|
2537 |
+
return image_url
|
2538 |
+
except Exception as e:
|
2539 |
+
logger.warning(f"[Image] Error checking image URL {image_url}: {e}")
|
2540 |
+
# Default image fallback
|
2541 |
+
default_image_url = f"{base_url}default_product.jpg"
|
2542 |
+
try:
|
2543 |
+
resp = requests.head(default_image_url, timeout=5)
|
2544 |
+
if resp.status_code == 200:
|
2545 |
+
logger.info(f"[Image] Using default public image URL: {default_image_url}")
|
2546 |
+
return default_image_url
|
2547 |
+
except Exception as e:
|
2548 |
+
logger.warning(f"[Image] Error checking default image URL {default_image_url}: {e}")
|
2549 |
+
logger.warning(f"[Image] No public image found for product: {product_name}")
|
2550 |
return None
|
|
|
2551 |
except Exception as e:
|
2552 |
+
logger.error(f"[Image] Error generating public image URL for {product_name}: {e}")
|
2553 |
return None
|
2554 |
|
2555 |
def get_product_image_media_type(image_path: str) -> str:
|