from unidecode import unidecode
import os
import re
import uuid
import datetime

def filename_to_regex(filename):
    filename = unidecode(filename)
    filename = re.sub(r'[^0-9a-zA-Z._-]+', '', filename)
    return "^[0-9a-zA-Z._-]+" + filename + "$"

def generate_name(filename):
    filechar,ext = os.path.splitext(filename)
    # # rewrite file name and remove any unwanted character
    # filechar = filechar.replace(".", "").replace(" ", "_").replace("(", "_").replace(")", "_").replace("&", "AND")
    # add date and time to video name to avoide conflect by name
    now = (str(datetime.datetime.now()).split('.')[0].replace(' ','_').replace(':','_').replace('-',"_"))
    # return new filename
    return f"{uuid.uuid4().hex}_{now}{ext}"

def handle_uploaded_file(file, path,filename):
    # generate file name
    # with open(path+"/"+filename, 'wb+') as destination:
    with open(path+str(filename), 'wb+') as destination:
        # for chunk in file.chunks():
        #     destination.write(chunk)

        #with open(image_path, 'wb+') as destination:
        if type(file) == str:
            destination.write(file.encode())   # or whatever you actually want to do if it's a string
        else:
            for chunk in file.chunks():
                destination.write(chunk)