# plugins/media/utils.py

import os
import uuid
from PIL import Image

def generate_random_filename(original_filename):
    """
    Returns a random filename preserving the original extension.
    e.g. “abcd1234efgh.jpg” for “myphoto.jpg”.
    """
    ext = os.path.splitext(original_filename)[1].lower()  # includes dot, e.g. ".jpg"
    random_name = uuid.uuid4().hex  # 32‐char hex string
    return f"{random_name}{ext}"

def strip_metadata_and_save(source_file, destination_path):
    """
    Opens an image with Pillow, strips EXIF (metadata), and saves it cleanly.
    Supports common formats (JPEG, PNG).
    """
    with Image.open(source_file) as img:
        data = list(img.getdata())
        clean_image = Image.new(img.mode, img.size)
        clean_image.putdata(data)
        clean_image.save(destination_path)
