modified: app.py
This commit is contained in:
49
app.py
49
app.py
@@ -106,57 +106,48 @@ def make_discord_session(token=None, state=None):
|
||||
)
|
||||
|
||||
def fetch_and_cache_profile_picture(user_id, avatar_url):
|
||||
"""Lädt das Profilbild herunter und speichert es lokal und in Redis."""
|
||||
if not user_id or not avatar_url:
|
||||
# Fallback auf ein Standardprofilbild, wenn keine gültigen Werte vorhanden sind
|
||||
print(f"Skipping user {user_id}: Missing avatar URL.")
|
||||
"""Lädt das Profilbild herunter, wenn es nicht lokal vorhanden ist, und speichert es in Redis."""
|
||||
if not user_id:
|
||||
print(f"Invalid user_id: {user_id}. Using default profile picture.")
|
||||
return "/static/default_profile.png"
|
||||
|
||||
# Erstellen des lokalen Pfads für das Bild
|
||||
local_image_path = os.path.join(PROFILE_IMAGE_DIR, f"{user_id}.png")
|
||||
|
||||
# Prüfen, ob das Bild bereits lokal gespeichert ist
|
||||
# Überprüfen, ob das Bild lokal gespeichert ist
|
||||
if os.path.exists(local_image_path):
|
||||
print(f"Profile picture for user {user_id} already exists locally.")
|
||||
print(f"Local image already exists for user {user_id}: {local_image_path}")
|
||||
r.set(str(user_id), local_image_path)
|
||||
return local_image_path
|
||||
|
||||
# Wenn das Bild nicht lokal vorhanden ist, versuche, es herunterzuladen
|
||||
if avatar_url:
|
||||
try:
|
||||
# Bild von der URL herunterladen und speichern
|
||||
print(f"Downloading profile picture for user {user_id} from {avatar_url}")
|
||||
response = requests.get(avatar_url, timeout=10)
|
||||
response.raise_for_status()
|
||||
|
||||
# Speichere das Bild lokal
|
||||
with open(local_image_path, 'wb') as img_file:
|
||||
img_file.write(response.content)
|
||||
|
||||
# Pfad in Redis speichern
|
||||
# Speichere den Pfad in Redis
|
||||
r.set(str(user_id), local_image_path)
|
||||
print(f"Profile picture for user {user_id} downloaded and cached.")
|
||||
|
||||
print(f"Profile picture for user {user_id} downloaded and cached at {local_image_path}")
|
||||
return local_image_path
|
||||
except requests.RequestException as e:
|
||||
print(f"Error fetching profile picture for user {user_id}: {e}")
|
||||
print(f"Failed to download profile picture for user {user_id}: {e}")
|
||||
return "/static/default_profile.png"
|
||||
|
||||
def get_profile_picture(user_id, avatar_url=None):
|
||||
"""Gibt den Pfad zum Profilbild eines Benutzers zurück. Nutzt Redis oder den lokalen Speicher."""
|
||||
# 1. Versuche, den Pfad aus Redis abzurufen
|
||||
print(f"No avatar URL provided for user {user_id}. Using default profile picture.")
|
||||
return "/static/default_profile.png"
|
||||
|
||||
def get_profile_picture(user_id, avatar_url):
|
||||
"""Gibt den Pfad zum Profilbild zurück oder lädt es herunter, wenn es nicht in Redis oder lokal vorhanden ist."""
|
||||
cached_image = r.get(str(user_id))
|
||||
if cached_image:
|
||||
local_path = cached_image.decode('utf-8')
|
||||
print(f"Redis cache hit for user {user_id}: {local_path}")
|
||||
return local_path
|
||||
|
||||
# 2. Überprüfen, ob das Bild lokal im `profile_images`-Ordner existiert
|
||||
local_image_path = os.path.join('static', 'profile_images', f"{user_id}.png")
|
||||
if os.path.exists(local_image_path):
|
||||
print(f"Local image found for user {user_id}: {local_image_path}")
|
||||
# Speichere den Pfad in Redis, um ihn für zukünftige Anfragen zwischenspeichern
|
||||
r.set(str(user_id), local_image_path)
|
||||
return local_image_path
|
||||
|
||||
# 3. Wenn weder Redis noch ein lokales Bild vorhanden ist, gib das Standardbild zurück
|
||||
print(f"No profile picture found for user {user_id}, using default image.")
|
||||
return "/static/default_profile.png"
|
||||
return cached_image.decode('utf-8')
|
||||
return fetch_and_cache_profile_picture(user_id, avatar_url)
|
||||
|
||||
@app.context_processor
|
||||
def utility_processor():
|
||||
|
||||
Reference in New Issue
Block a user