Refactor image URL handling: Update API routes to return image URLs via the API instead of direct paths, ensuring consistency across avatar and background uploads. Introduce normalization functions for avatar and background URLs to maintain compatibility with existing URLs.
All checks were successful
Deploy with Docker Compose / deploy (push) Successful in 2m57s

This commit is contained in:
Julien Froidefond
2025-12-12 15:59:18 +01:00
parent 702476c349
commit f69fbbd0e1
11 changed files with 238 additions and 13 deletions

53
lib/avatars.ts Normal file
View File

@@ -0,0 +1,53 @@
/**
* Normalise une URL d'avatar pour utiliser la route API
* Gère la compatibilité avec les anciennes URLs directes
*/
export function normalizeAvatarUrl(
avatarUrl: string | null | undefined
): string | null {
if (!avatarUrl) {
return null;
}
// Si c'est déjà une URL API, la retourner telle quelle
if (avatarUrl.startsWith("/api/avatars/")) {
return avatarUrl;
}
// Si c'est une ancienne URL directe, la convertir en URL API
if (avatarUrl.startsWith("/uploads/avatars/")) {
const filename = avatarUrl.replace("/uploads/avatars/", "");
return `/api/avatars/${filename}`;
}
// Si c'est une URL complète (http/https) ou un chemin relatif autre, la retourner telle quelle
// (pour les avatars par défaut comme /avatar-1.jpg)
return avatarUrl;
}
/**
* Normalise une URL d'image de fond pour utiliser la route API
* Gère la compatibilité avec les anciennes URLs directes
*/
export function normalizeBackgroundUrl(
backgroundUrl: string | null | undefined
): string | null {
if (!backgroundUrl) {
return null;
}
// Si c'est déjà une URL API, la retourner telle quelle
if (backgroundUrl.startsWith("/api/backgrounds/")) {
return backgroundUrl;
}
// Si c'est une ancienne URL directe, la convertir en URL API
if (backgroundUrl.startsWith("/uploads/backgrounds/")) {
const filename = backgroundUrl.replace("/uploads/backgrounds/", "");
return `/api/backgrounds/${filename}`;
}
// Si c'est une URL complète (http/https) ou un chemin relatif autre, la retourner telle quelle
// (pour les images par défaut comme /got-2.jpg)
return backgroundUrl;
}