From fc3ce9e7d18ada447b15f2645f3a0e44f2fdd221 Mon Sep 17 00:00:00 2001 From: SimolZimol <70102430+SimolZimol@users.noreply.github.com> Date: Thu, 31 Oct 2024 20:16:32 +0100 Subject: [PATCH] modified: app.py --- app.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/app.py b/app.py index d99b060..03436fc 100644 --- a/app.py +++ b/app.py @@ -107,26 +107,28 @@ def make_discord_session(token=None, state=None): def fetch_and_cache_profile_picture(user_id, avatar_url): """Lädt das Profilbild herunter, speichert es lokal und in Redis, wenn es nicht vorhanden ist.""" - if not user_id: - print(f"Invalid user_id: {user_id}. Using default profile picture.") + # Überprüfen auf gültige user_id + if not user_id or user_id == "": + print(f"Invalid or empty user_id: {user_id}. Using default profile picture.") return "/static/default_profile.png" - # Pfad zum lokalen Bild - local_image_path = os.path.join(PROFILE_IMAGE_DIR, f"{user_id}.png") + # Konvertiere user_id sicher zu einem String + user_id_str = str(user_id) + local_image_path = os.path.join(PROFILE_IMAGE_DIR, f"{user_id_str}.png") # Prüfe, ob das Bild lokal vorhanden ist if os.path.exists(local_image_path): - print(f"Local image already exists for user {user_id}: {local_image_path}") - r.set(str(user_id), local_image_path) # Speichere in Redis für zukünftige Anfragen + print(f"Local image found for user {user_id}: {local_image_path}") + r.set(user_id_str, local_image_path) # Speichere den Pfad in Redis für zukünftige Anfragen return local_image_path # Prüfe Redis für den Pfad - cached_image = r.get(str(user_id)) + cached_image = r.get(user_id_str) if cached_image: print(f"Redis cache hit for user {user_id}: {cached_image.decode('utf-8')}") return cached_image.decode('utf-8') - # Falls das Bild nicht lokal vorhanden ist und Redis keinen Pfad hat, versuche das Bild herunterzuladen + # Wenn das Bild weder lokal noch in Redis vorhanden ist, versuche, es herunterzuladen if avatar_url: try: print(f"Downloading profile picture for user {user_id} from {avatar_url}") @@ -138,7 +140,7 @@ def fetch_and_cache_profile_picture(user_id, avatar_url): img_file.write(response.content) # Speichere den Pfad in Redis - r.set(str(user_id), local_image_path) + r.set(user_id_str, local_image_path) print(f"Profile picture for user {user_id} downloaded and cached at {local_image_path}") return local_image_path except requests.RequestException as e: