"""Utility functions for image handling and processing.""" import base64 import re def ensure_bytes_format(image_data): """ Ensure image data is in the proper format for storage. File paths and URLs should remain as strings, only actual image data should be bytes. Args: image_data: Image data that could be bytes, base64 string, file path, or URL string Returns: Properly formatted data for database storage (bytes for actual image data, string for paths/URLs) """ if image_data is None: return None # If it's already bytes, return as is (for actual image data) if isinstance(image_data, bytes): return image_data # If it's a string, determine if it's a file path, URL, or base64 data if isinstance(image_data, str): # Check if it's a data URL (base64 encoded image) if image_data.startswith('data:image/'): try: # Extract base64 part and decode to bytes base64_part = image_data.split(',')[1] return base64.b64decode(base64_part) except Exception: # If decoding fails, return the original string return image_data # Check if it looks like a file path # This regex matches common file path patterns like /tmp/..., ./path/, ../path/, C:\path\, etc. elif re.match(r'^[/\\]|[.][/\\]|[\w]:[/\\]|.*\.(jpg|jpeg|png|gif|webp|bmp|tiff|svg)$', image_data.lower()): # This appears to be a file path, return as string - don't convert to bytes return image_data # Check if it looks like a URL (has a scheme like http:// or https://) elif image_data.startswith(('http://', 'https://', 'ftp://', 'file://')): # This is a URL, return as string return image_data else: # Assume it's a string that should remain as is return image_data # For any other type, return as is return image_data