import project_path import os; from datetime import datetime; USER_DATA_DIR = "user_data/" def save_data(bytes, filename): """Take a file and saved it to a new user_data folder""" dirname = create_data_dir() filepath = os.path.join(dirname, filename) assert bytes[0:3] == b'DDF' with open(filepath, 'wb') as out: out.write(bytes) # check this is actually a valid ARIS file to catch any malicious fish scientists try: with open(filepath, 'rb') as file: assert file.read(3) == b'DDF' except: print("Bad file!", filepath) return False, None, None return True, filepath, dirname def allowed_file(filename): """Only allow an ARIS file to be uploaded.""" return '.' in filename and \ filename.rsplit('.', 1)[1].lower() in ['aris', 'ddf'] def create_data_dir(): """Create a (probably) unique directory for a task.""" dirname = os.path.join(USER_DATA_DIR, str(int(datetime.now().timestamp()))) if os.path.exists(dirname): print("Warning,", dirname, "already exists.") os.makedirs(dirname, exist_ok=True) return dirname