Jun 28 Optimize code

This commit is contained in:
2026-06-28 10:16:20 -04:00
parent ee7b0286b2
commit 45ae2b9c64
13 changed files with 536 additions and 61 deletions
+24
View File
@@ -71,6 +71,25 @@ def _save_logo(file_obj):
return f'uploads/logos/{filename}'
def _delete_logo(logo_url):
"""Remove a logo file from disk. Silently ignores missing files.
Safety guard: only deletes files inside the uploads/logos/ subfolder."""
try:
# Reconstruct the absolute path from the relative URL stored in the DB
# logo_url is like "uploads/logos/<filename>"
rel = logo_url.replace('uploads/', '', 1) # → "logos/<filename>"
full_path = os.path.join(current_app.config['UPLOAD_FOLDER'], rel)
logos_dir = os.path.join(current_app.config['UPLOAD_FOLDER'], 'logos')
abs_path = os.path.abspath(full_path)
abs_logos = os.path.abspath(logos_dir)
# Path traversal guard — only remove files inside logos/
if abs_path.startswith(abs_logos + os.sep) and os.path.isfile(abs_path):
os.remove(abs_path)
logger.info('SETTINGS | logo_deleted | path=%s', abs_path)
except Exception as exc:
logger.warning('SETTINGS | logo_delete_failed | url=%s err=%s', logo_url, exc)
def _mt_enabled():
return current_app.config.get('MULTI_TENANT_ENABLED', False)
@@ -135,8 +154,13 @@ def branding():
row.accent_color = accent_color or '#16a34a'
row.support_email = support_email
if new_logo_url:
# Delete old logo file from disk before replacing
if row.logo_url:
_delete_logo(row.logo_url)
row.logo_url = new_logo_url
elif request.form.get('clear_logo'):
if row.logo_url:
_delete_logo(row.logo_url)
row.logo_url = None
row.updated_at = now_eastern()
row.updated_by = current_user.id