Perf: 9 Performance-Fixes — SW by-v1072
Backend: - DB: 3 neue Indizes (forum_posts thread+user, routes user) — Forum/Routen-Queries - Caching: cache.py (TTL-Cache ohne neue Dependency) für 5 statische Listen (training_exercises, pflege_tipps, wiki_stats, wiki_gruppen, help_articles) - diary.py + breeder_photos.py: Bildverarbeitung (ffmpeg/PIL/EXIF) per run_in_executor → blockiert Event-Loop nicht mehr - scheduler.py: 11 kollidierende Jobs auf 5-Min-Intervalle gestaggert, coalesce=True - social.py: ORDER BY RANDOM() ohne LIMIT in 2 Stellen gefixt - alerts.py: Haversine-Loop bekommt SQL-Bounding-Box-Vorfilter Frontend: - sw.js: Tile-Cache mit LRU-Eviction (max 500 Einträge) - admin.js: Event-Listener-Leak — Tab-Klicks per Delegation statt N Listener - api.js: compressImage() Helper — Client-seitiges Resize auf max 2000px (HEIC/Videos/<500KB unverändert), integriert in 8 Upload-Stellen (diary, dog-profile×2, walks, poison, lost, health×2) Bump APP_VER 1071 → 1072 (sw.js, app.js, main.py, index.html)
This commit is contained in:
parent
3abf974d29
commit
c03884cb81
23 changed files with 461 additions and 120 deletions
|
|
@ -3,7 +3,7 @@ from fastapi import APIRouter, Depends, HTTPException, UploadFile, File, Form
|
|||
from fastapi.responses import FileResponse
|
||||
from pydantic import BaseModel
|
||||
from typing import Optional
|
||||
import os, logging
|
||||
import os, logging, asyncio
|
||||
from database import db
|
||||
from auth import get_current_user, get_current_user_optional
|
||||
from media_utils import validate_upload, generate_preview
|
||||
|
|
@ -112,27 +112,37 @@ async def upload_photo(
|
|||
file_uuid = str(uuid.uuid4())
|
||||
file_path = os.path.join(save_dir, f"{file_uuid}.webp")
|
||||
|
||||
# Blockierende Bildverarbeitung in Threadpool auslagern,
|
||||
# damit der Event-Loop für andere Requests frei bleibt.
|
||||
loop = asyncio.get_event_loop()
|
||||
|
||||
def _write_bytes(p: str, data: bytes) -> None:
|
||||
with open(p, "wb") as f:
|
||||
f.write(data)
|
||||
|
||||
# Thumbnail erzeugen
|
||||
thumb_bytes = generate_preview(raw_data, ext)
|
||||
thumb_bytes = await loop.run_in_executor(
|
||||
None, lambda: generate_preview(raw_data, ext)
|
||||
)
|
||||
thumb_path = None
|
||||
if thumb_bytes:
|
||||
thumb_path = os.path.join(save_dir, f"{file_uuid}_thumb.webp")
|
||||
with open(thumb_path, "wb") as f:
|
||||
f.write(thumb_bytes)
|
||||
await loop.run_in_executor(None, lambda: _write_bytes(thumb_path, thumb_bytes))
|
||||
|
||||
# Originalbild konvertieren und speichern
|
||||
# generate_preview liefert WebP, für das Original nehmen wir Pillow direkt
|
||||
try:
|
||||
import io
|
||||
from PIL import Image, ImageOps
|
||||
img = Image.open(io.BytesIO(raw_data))
|
||||
img = ImageOps.exif_transpose(img)
|
||||
img = img.convert("RGB")
|
||||
img.save(file_path, format="WEBP", quality=85)
|
||||
except Exception:
|
||||
# Fallback: Rohdaten speichern
|
||||
with open(file_path, "wb") as f:
|
||||
f.write(raw_data)
|
||||
# Originalbild konvertieren und speichern (Pillow direkt — WebP-Qualität 85)
|
||||
def _save_original():
|
||||
try:
|
||||
import io
|
||||
from PIL import Image, ImageOps
|
||||
img = Image.open(io.BytesIO(raw_data))
|
||||
img = ImageOps.exif_transpose(img)
|
||||
img = img.convert("RGB")
|
||||
img.save(file_path, format="WEBP", quality=85)
|
||||
except Exception:
|
||||
# Fallback: Rohdaten speichern
|
||||
_write_bytes(file_path, raw_data)
|
||||
|
||||
await loop.run_in_executor(None, _save_original)
|
||||
|
||||
# Relative Pfade für DB (relativ zu MEDIA_DIR)
|
||||
rel_file = os.path.relpath(file_path, MEDIA_DIR)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue