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}")
|
||||
return "/static/default_profile.png"
|
||||
|
||||
|
||||
def get_profile_picture(user_id, avatar_url):
|
||||
"""Versucht, das Profilbild eines Benutzers aus Redis, lokalem Speicher und zuletzt durch Download zu holen."""
|
||||
# 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
|
||||
"""Gibt den Pfad zum Profilbild eines Benutzers zurück, entweder aus Redis oder durch Abruf."""
|
||||
cached_image = r.get(str(user_id))
|
||||
if cached_image:
|
||||
print(f"Redis cache hit for user {user_id}: {cached_image}")
|
||||
return cached_image.decode('utf-8')
|
||||
|
||||
# 2. Prüfen, ob das Bild lokal gespeichert ist
|
||||
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. Wenn das Bild weder in Redis noch lokal ist, herunterladen und speichern
|
||||
print(f"Downloading profile picture for user {user_id}")
|
||||
local_path = 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
|
||||
else:
|
||||
print(f"Redis cache miss for user {user_id}, fetching from URL.")
|
||||
return fetch_and_cache_profile_picture(user_id, avatar_url)
|
||||
|
||||
|
||||
@app.context_processor
|
||||
def utility_processor():
|
||||
def get_profile_picture(user_id, avatar_url):
|
||||
|
||||
Reference in New Issue
Block a user