modified: app.py

This commit is contained in:
SimolZimol
2024-10-31 19:55:52 +01:00
parent 5ee85e9468
commit 6dec96ebd9

22
app.py
View File

@@ -137,16 +137,26 @@ def fetch_and_cache_profile_picture(user_id, avatar_url):
print(f"Error fetching profile picture for user {user_id}: {e}") print(f"Error fetching profile picture for user {user_id}: {e}")
return "/static/default_profile.png" return "/static/default_profile.png"
def get_profile_picture(user_id, avatar_url): def get_profile_picture(user_id, avatar_url=None):
"""Gibt den Pfad zum Profilbild eines Benutzers zurück, entweder aus Redis oder durch Abruf.""" """Gibt den Pfad zum Profilbild eines Benutzers zurück. Nutzt Redis oder den lokalen Speicher."""
# 1. Versuche, den Pfad aus Redis abzurufen
cached_image = r.get(str(user_id)) cached_image = r.get(str(user_id))
if cached_image: if cached_image:
local_path = cached_image.decode('utf-8') local_path = cached_image.decode('utf-8')
print(f"Redis cache hit for user {user_id}: {local_path}") print(f"Redis cache hit for user {user_id}: {local_path}")
return local_path # Rückgabe des Pfads aus Redis return local_path
else:
print(f"Redis cache miss for user {user_id}, fetching from URL.") # 2. Überprüfen, ob das Bild lokal im `profile_images`-Ordner existiert
return fetch_and_cache_profile_picture(user_id, avatar_url) local_image_path = os.path.join('static', 'profile_images', f"{user_id}.png")
if os.path.exists(local_image_path):
print(f"Local image found for user {user_id}: {local_image_path}")
# Speichere den Pfad in Redis, um ihn für zukünftige Anfragen zwischenspeichern
r.set(str(user_id), local_image_path)
return local_image_path
# 3. Wenn weder Redis noch ein lokales Bild vorhanden ist, gib das Standardbild zurück
print(f"No profile picture found for user {user_id}, using default image.")
return "/static/default_profile.png"
@app.context_processor @app.context_processor
def utility_processor(): def utility_processor():