from django.core.files.base import ContentFile
from django.db.models.functions import TruncDate
import base64


def base64_to_image_file(base64_string, filename="image.jpg"):
    """ convert base64 to image, pdf,..... """

    img_format, img_str = base64_string.split(';base64,')  # split before & after of ';base64,'
    ext = img_format.split('/')[-1]  # split format of file
    return ContentFile(base64.b64decode(img_str), name=f"{filename}.{ext}"), ext


def get_client_ip(request):  # noqa
    forwarded = request.META.get('HTTP_X_FORWARDED_FOR')
    return forwarded.split(',')[0] if forwarded else request.META.get('REMOTE_ADDR')
