DreamStream-1 commited on
Commit
23d5a63
·
verified ·
1 Parent(s): 8697036

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -7
app.py CHANGED
@@ -2517,19 +2517,19 @@ load_products_data()
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:
@@ -2537,6 +2537,17 @@ def get_product_image_path(product_name: str) -> str:
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:
 
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, robust to formatting, preserving dashes.
 
2521
  """
2522
  try:
2523
+ # Normalize: lowercase, remove spaces, underscores, and dots, but keep dashes
2524
+ def normalize(name):
2525
+ return re.sub(r'[\s_\.]', '', name).lower()
2526
+ normalized_name = normalize(product_name)
2527
  image_extensions = ['.png', '.jpg', '.jpeg', '.webp']
 
2528
  base_url = "https://amgocus.com/uploads/images/"
2529
  import requests
2530
+ # Try normalized name (with dashes preserved)
2531
  for ext in image_extensions:
2532
+ image_url = f"{base_url}{normalized_name}{ext}"
2533
  try:
2534
  resp = requests.head(image_url, timeout=5)
2535
  if resp.status_code == 200:
 
2537
  return image_url
2538
  except Exception as e:
2539
  logger.warning(f"[Image] Error checking image URL {image_url}: {e}")
2540
+ # Fallback: try original name with spaces as %20
2541
+ safe_name = product_name.strip().replace(' ', '%20')
2542
+ for ext in image_extensions:
2543
+ image_url = f"{base_url}{safe_name}{ext}"
2544
+ try:
2545
+ resp = requests.head(image_url, timeout=5)
2546
+ if resp.status_code == 200:
2547
+ logger.info(f"[Image] Found public image URL (legacy): {image_url}")
2548
+ return image_url
2549
+ except Exception as e:
2550
+ logger.warning(f"[Image] Error checking image URL {image_url}: {e}")
2551
  # Default image fallback
2552
  default_image_url = f"{base_url}default_product.jpg"
2553
  try: