<commit_before>"""
    Operations dealing with uploaded files
"""

from django.conf import settings
from api.models import Hackathon
import string
import random
import os

__all__ = []


UPLOAD_DIR = os.path.join(settings.BASE_DIR, './uploads')
MAX_FILE_NAME_SIZE = 60
MAX_NAME_SIZE = 30


def id_generator(size, chars=(string.ascii_uppercase + string.digits + string.ascii_lowercase)):
    return ''.join(random.choice(chars) for _ in range(size))


def generate_file_name(original: str, file_extension: str) -> str:
    name = original[:MAX_NAME_SIZE]
    return name + '_' + id_generator(MAX_NAME_SIZE - len(name) - len(file_extension) - 2) + '.' + file_extension


def get_upload_relative_dir() -> str:
    """ Gets directory name based off of current hackathon """
    return '' + Hackathon.objects.current().id


def handle_file_upload(file, src_file_name: str, file_extension: str) -> str:
    """ Saves file and then returns the path to the file relative to the UPLOAD_DIR """
    while True:
        # Get file name that has not been taken
        relative_dir = get_upload_relative_dir()
        file_name = generate_file_name(src_file_name, file_extension)
        relative_path = os.path.join(relative_dir, './' + file_name)
        absolute_path = os.path.join(UPLOAD_DIR, './' + relative_path)
        if not os.path.isfile(absolute_path):
            break

    # Save file
    with open(absolute_path, 'wb+') as destination:
        for chunk in file.chunks():
            destination.write(chunk)

    return relative_path
<commit_msg>added get_url function to files utility<commit_after>"""
    Operations dealing with uploaded files
"""

from django.conf import settings
from api.models import Hackathon
import string
import random
import os

__all__ = []

RELATIVE_UPLOAD_DIR = '/uploads'
UPLOAD_DIR = os.path.join(settings.BASE_DIR, '.' + RELATIVE_UPLOAD_DIR)
MAX_FILE_NAME_SIZE = 60
MAX_NAME_SIZE = 30


def id_generator(size, chars=(string.ascii_uppercase + string.digits + string.ascii_lowercase)):
    return ''.join(random.choice(chars) for _ in range(size))


def generate_file_name(original: str, file_extension: str) -> str:
    name = original[:MAX_NAME_SIZE]
    return name + '_' + id_generator(MAX_NAME_SIZE - len(name) - len(file_extension) - 2) + '.' + file_extension


def get_upload_relative_dir() -> str:
    """ Gets directory name based off of current hackathon """
    return '' + Hackathon.objects.current().id


def handle_file_upload(file, src_file_name: str, file_extension: str) -> str:
    """ Saves file and then returns the path to the file relative to the UPLOAD_DIR """
    while True:
        # Get file name that has not been taken
        relative_dir = get_upload_relative_dir()
        file_name = generate_file_name(src_file_name, file_extension)
        relative_path = os.path.join(relative_dir, './' + file_name)
        absolute_path = os.path.join(UPLOAD_DIR, './' + relative_path)
        if not os.path.isfile(absolute_path):
            break

    # Save file
    with open(absolute_path, 'wb+') as destination:
        for chunk in file.chunks():
            destination.write(chunk)

    return relative_path


def get_url(file_name):
    """ Gets the url path to given file_name, which has already been made """
    return os.path.join(RELATIVE_UPLOAD_DIR, './' + file_name)

{'fraction_non_alphanumeric': 0.1052946455279689, 'fraction_numerical': 0.002991325157044571, 'mean_word_length': 3.663877266387727, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1649818', 'n_tokens_mistral': 1056, 'n_tokens_neox': 1036, 'n_words': 276}