diff --git a/app.py b/app.py index 2f02596..67e5342 100644 --- a/app.py +++ b/app.py @@ -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}") return "/static/default_profile.png" -def get_profile_picture(user_id, avatar_url): - """Gibt den Pfad zum Profilbild eines Benutzers zurück, entweder aus Redis oder durch Abruf.""" +def get_profile_picture(user_id, avatar_url=None): + """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)) if cached_image: local_path = cached_image.decode('utf-8') print(f"Redis cache hit for user {user_id}: {local_path}") - return local_path # Rückgabe des Pfads aus Redis - else: - print(f"Redis cache miss for user {user_id}, fetching from URL.") - return fetch_and_cache_profile_picture(user_id, avatar_url) + return local_path + + # 2. Überprüfen, ob das Bild lokal im `profile_images`-Ordner existiert + 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 def utility_processor():