Add timestamp to avatar URLs to prevent browser caching of updated avatars

parent 033787b6
......@@ -124,6 +124,7 @@ Thumbs.db
# Project specific
static/uploads/
static/temp/
static/avatars/
*.db
*.sqlite
*.sqlite3
......
......@@ -29,11 +29,12 @@ def get_current_user_session():
user = get_current_user(session_id)
if user and user.get('email'):
import hashlib
import time
email_hash = hashlib.md5(user['email'].lower().encode()).hexdigest()
user['gravatar_url'] = f"https://www.gravatar.com/avatar/{email_hash}?s=32&d=404"
# If custom avatar, use that instead
if user.get('avatar'):
user['avatar_url'] = f"/static/avatars/{user['avatar']}"
user['avatar_url'] = f"/static/avatars/{user['avatar']}?t={int(time.time())}"
else:
user['avatar_url'] = user['gravatar_url']
return user
......
......@@ -1032,15 +1032,17 @@ def upload_avatar():
if file and allowed_file(file.filename, {'png', 'jpg', 'jpeg', 'gif'}):
# Generate unique filename
import uuid
filename = f"{user['id']}_{uuid.uuid4().hex}.{file.filename.rsplit('.', 1)[1].lower()}"
ext = file.filename.rsplit('.', 1)[1].lower() if '.' in file.filename else 'jpg'
filename = f"{user['id']}_{uuid.uuid4().hex}.{ext}"
filepath = os.path.join(avatars_dir, filename)
# Resize and save image
# Read file data
file_data = file.read()
from io import BytesIO
from PIL import Image
import io
# Read image
image = Image.open(file.stream)
# Open image
image = Image.open(BytesIO(file_data))
# Convert to RGB if necessary
if image.mode in ("RGBA", "P"):
......@@ -1050,7 +1052,10 @@ def upload_avatar():
image.thumbnail((128, 128), Image.Resampling.LANCZOS)
# Save
image.save(filepath, optimize=True, quality=85)
if ext == 'png':
image.save(filepath, optimize=True)
else:
image.save(filepath, optimize=True, quality=85)
# Update user avatar in database
from .database import update_user_avatar
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment