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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -25
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 path for a product
2521
- Returns the path to the product image if it exists, otherwise None
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(' ', '_').lower()
2530
-
2531
- # Check for common image extensions
2532
- image_extensions = ['.jpg', '.jpeg', '.png', '.webp', '.gif']
2533
-
 
2534
  for ext in image_extensions:
2535
- image_path = os.path.join(images_dir, f"{safe_name}{ext}")
2536
- if os.path.exists(image_path):
2537
- logger.info(f"[Image] Found product image: {image_path}")
2538
- return image_path
2539
-
2540
- # If no specific product image found, check for a default image
2541
- default_image_path = os.path.join(images_dir, "default_product.jpg")
2542
- if os.path.exists(default_image_path):
2543
- logger.info(f"[Image] Using default product image: {default_image_path}")
2544
- return default_image_path
2545
-
2546
- logger.warning(f"[Image] No image found for product: {product_name}")
 
 
 
 
 
 
2547
  return None
2548
-
2549
  except Exception as e:
2550
- logger.error(f"[Image] Error getting product image path: {e}")
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: