From 6d6efa659609531e26e37ec92b9696e1c6df4417 Mon Sep 17 00:00:00 2001 From: SimolZimol <70102430+SimolZimol@users.noreply.github.com> Date: Thu, 31 Oct 2024 21:56:03 +0100 Subject: [PATCH] modified: app.py --- app.py | 48 +++++++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/app.py b/app.py index b2273ba..281410c 100644 --- a/app.py +++ b/app.py @@ -105,6 +105,31 @@ def make_discord_session(token=None, state=None): token_updater=token_updater ) +def get_profile_picture(user_id, avatar_url=None): + """Gibt den Pfad zum Profilbild eines Benutzers zurück, entweder aus Redis, lokal oder durch Abruf.""" + if not user_id: + print("Error: user_id is undefined or None. Returning default profile picture.") + return "/static/default_profile.png" # Rückgabe des Standardbildes bei ungültiger user_id + + print(f"Fetching profile picture for user_id: {user_id}, avatar_url: {avatar_url}") + + # In Redis nach dem Bild suchen + 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 + + # Lokale Überprüfung, falls nicht in Redis vorhanden + 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}") + r.set(str(user_id), local_image_path) # In Redis zwischenspeichern + return local_image_path + + # Wenn weder Redis noch lokal vorhanden, dann herunterladen + return fetch_and_cache_profile_picture(user_id, avatar_url) + def fetch_and_cache_profile_picture(user_id, avatar_url): """Lädt das Profilbild herunter, speichert es lokal und fügt es zu Redis hinzu.""" if not user_id or not avatar_url: @@ -138,29 +163,6 @@ def fetch_and_cache_profile_picture(user_id, avatar_url): print(f"Failed to download profile picture for user {user_id}: {e}") return "/static/default_profile.png" -def get_profile_picture(user_id, avatar_url=None): - """Gibt den Pfad zum Profilbild eines Benutzers zurück, entweder aus Redis, lokal oder durch Abruf.""" - if not user_id: - print("Error: user_id is undefined or None. Returning default profile picture.") - return "/static/default_profile.png" # Rückgabe des Standardbildes bei ungültiger user_id - - # In Redis nach dem Bild suchen - 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 - - # Lokale Überprüfung, falls nicht in Redis vorhanden - 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}") - r.set(str(user_id), local_image_path) # In Redis zwischenspeichern - return local_image_path - - # Wenn weder Redis noch lokal vorhanden, dann herunterladen - return fetch_and_cache_profile_picture(user_id, avatar_url) - @app.context_processor def utility_processor(): def get_profile_picture(user_id, avatar_url):