modified: app.py
This commit is contained in:
25
app.py
25
app.py
@@ -137,30 +137,17 @@ def fetch_and_cache_profile_picture(user_id, avatar_url):
|
|||||||
print(f"Error fetching profile picture for user {user_id}: {e}")
|
print(f"Error fetching profile picture for user {user_id}: {e}")
|
||||||
return "/static/default_profile.png"
|
return "/static/default_profile.png"
|
||||||
|
|
||||||
|
|
||||||
def get_profile_picture(user_id, avatar_url):
|
def get_profile_picture(user_id, avatar_url):
|
||||||
"""Versucht, das Profilbild eines Benutzers aus Redis, lokalem Speicher und zuletzt durch Download zu holen."""
|
"""Gibt den Pfad zum Profilbild eines Benutzers zurück, entweder aus Redis oder durch Abruf."""
|
||||||
# Debugging: Überprüfen der empfangenen Werte
|
|
||||||
print(f"Checking profile picture for user {user_id} with avatar URL: {avatar_url}")
|
|
||||||
|
|
||||||
# 1. Prüfen, ob das Bild in Redis gespeichert ist
|
|
||||||
cached_image = r.get(str(user_id))
|
cached_image = r.get(str(user_id))
|
||||||
if cached_image:
|
if cached_image:
|
||||||
print(f"Redis cache hit for user {user_id}: {cached_image}")
|
local_path = cached_image.decode('utf-8')
|
||||||
return 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
|
||||||
# 2. Prüfen, ob das Bild lokal gespeichert ist
|
else:
|
||||||
local_image_path = os.path.join(PROFILE_IMAGE_DIR, f"{user_id}.png")
|
print(f"Redis cache miss for user {user_id}, fetching from URL.")
|
||||||
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. Wenn das Bild weder in Redis noch lokal ist, herunterladen und speichern
|
|
||||||
print(f"Downloading profile picture for user {user_id}")
|
|
||||||
return fetch_and_cache_profile_picture(user_id, avatar_url)
|
return fetch_and_cache_profile_picture(user_id, avatar_url)
|
||||||
|
|
||||||
|
|
||||||
@app.context_processor
|
@app.context_processor
|
||||||
def utility_processor():
|
def utility_processor():
|
||||||
def get_profile_picture(user_id, avatar_url):
|
def get_profile_picture(user_id, avatar_url):
|
||||||
|
|||||||
Reference in New Issue
Block a user