import os

from werkzeug.utils import secure_filename

from . import alphagen as ag

UPLOAD_FOLDER = 'app/static/incoming'
REPO_FOLDER = 'app/static/repository'
ALLOWED_EXT = {'png', 'jpg', 'jpeg'}

# Checks file for allowed extension
def allowed_file(filename):
    return '.' in filename and \
            filename.rsplit('.', 1)[1].lower() in ALLOWED_EXT

# And sanitizes
def sanitize(filename):
    sfn = secure_filename(filename) # strips any slashes
    ssfn, fx = os.path.splitext(sfn) # ensures that internal filenames are not
    rsfn = ag.gen_alphanum() # known to users.
    ffn = f'{rsfn}{fx}'
    return ffn

