``` filename = secure_filename(mod.name) + '-' + secure_filename(version) + '.zip' base_path = os.path.join(secure_filename(current_user.username) + '_' + str(current_user.id), secure_filename(mod.name)) full_path = os.path.join(_cfg('storage'), base_path) if not os.path.exists(full_path): os.makedirs(full_path) path = os.path.join(full_path, filename) for v in mod.versions: if v.friendly_version == secure_filename(version): return { 'error': True, 'reason': 'We already have this version. Did you mistype the version number?' }, 400 if os.path.isfile(path): os.remove(path) zipball.save(path) if not zipfile.is_zipfile(path): os.remove(path) return { 'error': True, 'reason': 'This is not a valid zip file.' }, 400 version = ModVersion(secure_filename(version), game_version_id, os.path.join(base_path, filename)) version.changelog = changelog # Assign a sort index if len(mod.versions) == 0: version.sort_index = 0 else: version.sort_index = max([v.sort_index for v in mod.versions]) + 1 mod.versions.append(version) mod.updated = datetime.now() if notify: send_update_notification(mod, version, current_user) db.add(version) db.commit() mod.default_version_id = version.id db.commit() notify_ckan.delay(mod_id, 'update') return { 'url': url_for("mods.mod", id=mod.id, mod_name=mod.name), "id": version.id }```