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
|
|
@ -814,9 +814,57 @@ const API = (() => {
|
|||
} catch {}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------
|
||||
// BILD-KOMPRESSION (Client-Side vor Upload)
|
||||
// ----------------------------------------------------------
|
||||
// iPhone-Fotos sind 4-12 MB — vor Upload auf max. 2000px / JPEG q=0.85
|
||||
// skalieren. HEIC/HEIF unverändert lassen (Browser-Canvas kann sie nicht
|
||||
// decoden, Backend hat eigene HEIC-Konvertierung). Nicht-Bilder unverändert.
|
||||
async function compressImage(file, maxSize = 2000, quality = 0.85) {
|
||||
try {
|
||||
if (!file || !(file instanceof File || file instanceof Blob)) return file;
|
||||
const type = (file.type || '').toLowerCase();
|
||||
if (!type.startsWith('image/')) return file;
|
||||
if (type === 'image/heic' || type === 'image/heif') return file;
|
||||
if (type === 'image/gif') return file; // GIF-Animation nicht kaputt skalieren
|
||||
if (file.size < 500_000) return file; // <500KB: lohnt sich nicht
|
||||
|
||||
const img = await createImageBitmap(file);
|
||||
try {
|
||||
const longest = Math.max(img.width, img.height);
|
||||
const scale = Math.min(1, maxSize / longest);
|
||||
// Nur skalieren wenn Bild wirklich größer ist; bei scale=1 trotzdem als JPEG
|
||||
// neu kodieren — spart bei iPhone-Originalen oft trotzdem viel (EXIF, weniger Qualität).
|
||||
const w = Math.round(img.width * scale);
|
||||
const h = Math.round(img.height * scale);
|
||||
|
||||
const canvas = document.createElement('canvas');
|
||||
canvas.width = w; canvas.height = h;
|
||||
const ctx = canvas.getContext('2d');
|
||||
if (!ctx) return file;
|
||||
ctx.drawImage(img, 0, 0, w, h);
|
||||
|
||||
const blob = await new Promise(res => canvas.toBlob(res, 'image/jpeg', quality));
|
||||
if (!blob || blob.size >= file.size) return file; // Kompression hat nichts gebracht
|
||||
|
||||
const newName = (file.name || 'photo.jpg').replace(/\.(heic|heif|png|webp|jpeg|jpg)$/i, '.jpg');
|
||||
const finalName = /\.jpe?g$/i.test(newName) ? newName : (newName + '.jpg');
|
||||
return new File([blob], finalName, { type: 'image/jpeg', lastModified: Date.now() });
|
||||
} finally {
|
||||
// ImageBitmap-Ressourcen freigeben (wo unterstützt)
|
||||
if (typeof img.close === 'function') img.close();
|
||||
}
|
||||
} catch {
|
||||
// Bei jedem Fehler (z.B. createImageBitmap auf HEIC) — original zurück
|
||||
return file;
|
||||
}
|
||||
}
|
||||
// Auch global verfügbar, damit Seiten-Module ihn direkt nutzen können
|
||||
if (typeof window !== 'undefined') window.compressImage = compressImage;
|
||||
|
||||
// Öffentliche API
|
||||
return {
|
||||
get, post, put, patch, del, upload, swCacheDelete,
|
||||
get, post, put, patch, del, upload, swCacheDelete, compressImage,
|
||||
auth, dogs, diary, health, tieraerzte, healthDocs, poison,
|
||||
places, routes, walks, events, sitting, forum, lost, knigge, weather, push,
|
||||
friends, chat, webcal, importData, sharing, widget, notifications, services, ratings, sittingAccess, training, notes,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue