Fix: Altes Hundeprofil-Foto wird beim Überschreiben gelöscht (SW by-v930)

- dogs.py /photo: alte foto_url vor Upload merken, nach Speichern von Disk löschen
- dog-profile.js: Hintergrund-Cache nach Foto-Wechsel invalidieren
This commit is contained in:
rene 2026-05-14 11:47:08 +02:00
parent 3585958c47
commit 567478a866
5 changed files with 19 additions and 5 deletions

View file

@ -844,13 +844,14 @@ async def upload_photo(
file: UploadFile = File(...),
user=Depends(get_current_user)
):
# Hund gehört dem User?
# Hund gehört dem User? Altes Foto merken für späteres Löschen.
with db() as conn:
dog = conn.execute(
"SELECT id FROM dogs WHERE id=? AND user_id=?", (dog_id, user["id"])
"SELECT id, foto_url FROM dogs WHERE id=? AND user_id=?", (dog_id, user["id"])
).fetchone()
if not dog:
raise HTTPException(404, "Hund nicht gefunden.")
old_foto_url = dog["foto_url"]
# Datei immer als JPEG speichern (HEIC/PNG/WebP → kompatibel für alle Browser)
import io
@ -884,6 +885,15 @@ async def upload_photo(
with db() as conn:
conn.execute("UPDATE dogs SET foto_url=? WHERE id=?", (foto_url, dog_id))
# Altes Foto von Disk löschen
if old_foto_url:
try:
old_path = safe_media_path(MEDIA_DIR, old_foto_url)
if old_path and os.path.isfile(old_path):
os.remove(old_path)
except Exception:
pass
return {"foto_url": foto_url}