File size: 2,102 Bytes
a9af6e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import base64
import io
from PIL import Image
from typing import Optional, Dict, Any

def validate_base64_image(base64_string: str) -> bool:
    """
    Validate if a string is a valid base64 encoded image
    """
    try:
        if base64_string.startswith('data:'):
            # Extract base64 part from data URL
            base64_part = base64_string.split(',', 1)[1] if ',' in base64_string else base64_string
        else:
            base64_part = base64_string
            
        # Try to decode and open as image
        image_data = base64.b64decode(base64_part)
        image = Image.open(io.BytesIO(image_data))
        image.verify()  # Verify it's a valid image
        return True
        
    except Exception:
        return False

def get_image_mime_type(base64_string: str) -> str:
    """
    Detect MIME type of base64 encoded image
    """
    try:
        if base64_string.startswith('data:'):
            # Extract MIME type from data URL
            if ';' in base64_string:
                return base64_string.split(';')[0].replace('data:', '')
        
        # Try to detect from image data
        if base64_string.startswith('data:'):
            base64_part = base64_string.split(',', 1)[1] if ',' in base64_string else base64_string
        else:
            base64_part = base64_string
            
        image_data = base64.b64decode(base64_part)
        image = Image.open(io.BytesIO(image_data))
        
        format_mapping = {
            'JPEG': 'image/jpeg',
            'PNG': 'image/png',
            'GIF': 'image/gif',
            'WEBP': 'image/webp'
        }
        
        return format_mapping.get(image.format, 'image/jpeg')
        
    except Exception:
        return 'image/jpeg'  # Default fallback

def ensure_data_url_format(image_data: str) -> str:
    """
    Ensure image data is in proper data URL format
    """
    if image_data.startswith('data:'):
        return image_data
    
    # Detect MIME type and add data URL prefix
    mime_type = get_image_mime_type(image_data)
    return f"data:{mime_type};base64,{image_data}"