modified: app.py
deleted: static/default_profile.png
This commit is contained in:
23
app.py
23
app.py
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user