feat: enhance avatar handling and update TODO.md

- Added Avatar component with support for custom URLs and Gravatar integration, improving user profile visuals.
- Implemented logic to determine avatar source based on user preferences in profile actions.
- Updated ProfilePage to utilize the new Avatar component for better consistency.
- Marked the integration of Gravatar and custom avatar handling as complete in TODO.md.
This commit is contained in:
Julien Froidefond
2025-10-04 11:35:08 +02:00
parent ad0b723e00
commit ffd3eb998a
11 changed files with 711 additions and 51 deletions

125
src/lib/avatars.ts Normal file
View File

@@ -0,0 +1,125 @@
import { getGravatarUrl, isGravatarUrl } from './gravatar'
export type AvatarType = 'custom' | 'gravatar' | 'default'
export interface AvatarConfig {
type: AvatarType
url?: string
email?: string
size?: number
}
/**
* Détermine l'URL finale de l'avatar selon la configuration
* @param config Configuration de l'avatar
* @returns URL finale de l'avatar ou null
*/
export function getAvatarUrl(config: AvatarConfig): string | null {
const { type, url, email, size = 200 } = config
switch (type) {
case 'custom':
if (!url) return null
return url
case 'gravatar':
if (!email) return null
return getGravatarUrl(email, { size })
case 'default':
default:
return null
}
}
/**
* Détermine le type d'avatar à partir d'une URL existante
* @param url URL de l'avatar
* @param email Email de l'utilisateur (pour vérifier si c'est déjà un Gravatar)
* @returns Type d'avatar détecté
*/
export function detectAvatarType(url: string | null | undefined, email?: string): AvatarType {
if (!url) return 'default'
if (isGravatarUrl(url)) {
return 'gravatar'
}
// Si on a un email et que l'URL correspond à un Gravatar généré pour cet email
if (email && typeof url === 'string') {
const expectedGravatarUrl = getGravatarUrl(email)
if (url.includes(expectedGravatarUrl.split('?')[0].split('avatar/')[1])) {
return 'gravatar'
}
}
return 'custom'
}
/**
* Génère une configuration d'avatar optimisée
* @param url URL existante de l'avatar
* @param email Email de l'utilisateur
* @param size Taille souhaitée
* @returns Configuration optimisée pour l'avatar
*/
export function optimizeAvatarConfig(
url: string | null | undefined,
email: string | undefined,
size: number = 200
): AvatarConfig {
const type = detectAvatarType(url, email)
switch (type) {
case 'gravatar':
return {
type: 'gravatar',
email,
size
}
case 'custom':
return {
type: 'custom',
url: url!,
size
}
case 'default':
default:
return {
type: 'default',
size
}
}
}
/**
* Valide une URL d'avatar personnalisée
* @param url URL à valider
* @returns true si valide, false sinon
*/
export function validateCustomAvatarUrl(url: string): boolean {
try {
const parsedUrl = new URL(url)
// Vérifier le protocole (seulement HTTPS pour la sécurité)
if (parsedUrl.protocol !== 'https:') {
return false
}
// Vérifier que c'est bien une URL d'image
const imageExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.webp', '.svg']
const hasImageExtension = imageExtensions.some(ext =>
parsedUrl.pathname.toLowerCase().endsWith(ext)
)
if (!hasImageExtension && parsedUrl.pathname.includes('.')) {
return false
}
return true
} catch {
return false
}
}

68
src/lib/gravatar.ts Normal file
View File

@@ -0,0 +1,68 @@
import crypto from 'crypto'
export interface GravatarOptions {
size?: number
defaultImage?: '404' | 'mp' | 'identicon' | 'monsterid' | 'wavatar' | 'retro' | 'robohash' | 'blank'
rating?: 'g' | 'pg' | 'r' | 'x'
forcedefault?: 'y' | 'n'
}
/**
* Génère une URL Gravatar à partir d'une adresse email
* @param email Email de l'utilisateur
* @param options Options pour personnaliser l'avatar
* @returns URL de l'avatar Gravatar
*/
export function getGravatarUrl(
email: string,
options: GravatarOptions = {}
): string {
const {
size = 200,
defaultImage = 'identicon',
rating = 'g',
forcedefault = 'n'
} = options
// Hacher l'email en MD5 (en minuscules et trimé)
const normalizedEmail = email.toLowerCase().trim()
const hash = crypto.createHash('md5').update(normalizedEmail).digest('hex')
// Construire l'URL
const params = new URLSearchParams({
size: size.toString(),
default: defaultImage,
rating,
forcedefault
})
return `https://www.gravatar.com/avatar/${hash}?${params.toString()}`
}
/**
* Valide si une URL est une URL Gravatar valide
*/
export function isGravatarUrl(url: string): boolean {
return url.startsWith('https://www.gravatar.com/avatar/') ||
url.startsWith('https://gravatar.com/avatar/')
}
/**
* Vérifie si l'avatar Gravatar existe réellement
* @param email Email de l'utilisateur
* @returns Promise<boolean> true si l'avatar existe, false sinon
*/
export async function checkGravatarExists(email: string): Promise<boolean> {
try {
const gravatarUrl = getGravatarUrl(email, { defaultImage: '404', forcedefault: 'y' })
const response = await fetch(gravatarUrl, {
method: 'HEAD',
signal: AbortSignal.timeout(5000) // 5s timeout
})
return response.ok
} catch {
return false
}
}