modified: app.py

deleted:    static/default_profile.png
This commit is contained in:
SimolZimol
2024-10-31 18:14:16 +01:00
parent a60052b361
commit a4af653e56
2 changed files with 14 additions and 9 deletions

23
app.py
View File

@@ -114,12 +114,6 @@ def fetch_and_cache_profile_picture(user_id, avatar_url):
local_image_path = os.path.join(PROFILE_IMAGE_DIR, f"{user_id}.png")
# Prüfen, ob das Bild bereits lokal gespeichert ist
if os.path.exists(local_image_path):
print(f"Profile picture for user {user_id} already exists locally.")
r.set(str(user_id), local_image_path)
return local_image_path
try:
# Bild von der URL herunterladen und speichern
response = requests.get(avatar_url, timeout=10)
@@ -137,12 +131,23 @@ 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."""
"""Versucht, das Profilbild eines Benutzers aus Redis, lokalem Speicher und zuletzt durch Download zu holen."""
# 1. Versuch: Redis
cached_image = r.get(str(user_id))
if cached_image:
return cached_image.decode('utf-8') # Rückgabe des Pfads aus Redis
print(f"Redis cache hit for user {user_id}: {cached_image}")
return cached_image.decode('utf-8')
# 2. Versuch: Lokaler Speicher
local_image_path = os.path.join(PROFILE_IMAGE_DIR, f"{user_id}.png")
if os.path.exists(local_image_path):
print(f"Local cache hit for user {user_id}")
r.set(str(user_id), local_image_path) # Cache das Bild in Redis
return local_image_path
# 3. Download und Cache des Bilds
print(f"Downloading profile picture for user {user_id}")
return fetch_and_cache_profile_picture(user_id, avatar_url)
@app.context_processor

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB