chore: clean up avatar and gravatar files by removing extra blank lines for improved readability

This commit is contained in:
Julien Froidefond
2025-10-09 13:26:57 +02:00
parent 17dade54e6
commit 1fe59f26e4
2 changed files with 84 additions and 70 deletions

View File

@@ -1,12 +1,12 @@
import { getGravatarUrl, isGravatarUrl } from './gravatar' import { getGravatarUrl, isGravatarUrl } from './gravatar';
export type AvatarType = 'custom' | 'gravatar' | 'default' export type AvatarType = 'custom' | 'gravatar' | 'default';
export interface AvatarConfig { export interface AvatarConfig {
type: AvatarType type: AvatarType;
url?: string url?: string;
email?: string email?: string;
size?: number size?: number;
} }
/** /**
@@ -15,20 +15,20 @@ export interface AvatarConfig {
* @returns URL finale de l'avatar ou null * @returns URL finale de l'avatar ou null
*/ */
export function getAvatarUrl(config: AvatarConfig): string | null { export function getAvatarUrl(config: AvatarConfig): string | null {
const { type, url, email, size = 200 } = config const { type, url, email, size = 200 } = config;
switch (type) { switch (type) {
case 'custom': case 'custom':
if (!url) return null if (!url) return null;
return url return url;
case 'gravatar': case 'gravatar':
if (!email) return null if (!email) return null;
return getGravatarUrl(email, { size }) return getGravatarUrl(email, { size });
case 'default': case 'default':
default: default:
return null return null;
} }
} }
@@ -38,22 +38,25 @@ export function getAvatarUrl(config: AvatarConfig): string | null {
* @param email Email de l'utilisateur (pour vérifier si c'est déjà un Gravatar) * @param email Email de l'utilisateur (pour vérifier si c'est déjà un Gravatar)
* @returns Type d'avatar détecté * @returns Type d'avatar détecté
*/ */
export function detectAvatarType(url: string | null | undefined, email?: string): AvatarType { export function detectAvatarType(
if (!url) return 'default' url: string | null | undefined,
email?: string
): AvatarType {
if (!url) return 'default';
if (isGravatarUrl(url)) { if (isGravatarUrl(url)) {
return 'gravatar' return 'gravatar';
} }
// Si on a un email et que l'URL correspond à un Gravatar généré pour cet email // Si on a un email et que l'URL correspond à un Gravatar généré pour cet email
if (email && typeof url === 'string') { if (email && typeof url === 'string') {
const expectedGravatarUrl = getGravatarUrl(email) const expectedGravatarUrl = getGravatarUrl(email);
if (url.includes(expectedGravatarUrl.split('?')[0].split('avatar/')[1])) { if (url.includes(expectedGravatarUrl.split('?')[0].split('avatar/')[1])) {
return 'gravatar' return 'gravatar';
} }
} }
return 'custom' return 'custom';
} }
/** /**
@@ -68,29 +71,29 @@ export function optimizeAvatarConfig(
email: string | undefined, email: string | undefined,
size: number = 200 size: number = 200
): AvatarConfig { ): AvatarConfig {
const type = detectAvatarType(url, email) const type = detectAvatarType(url, email);
switch (type) { switch (type) {
case 'gravatar': case 'gravatar':
return { return {
type: 'gravatar', type: 'gravatar',
email, email,
size size,
} };
case 'custom': case 'custom':
return { return {
type: 'custom', type: 'custom',
url: url!, url: url!,
size size,
} };
case 'default': case 'default':
default: default:
return { return {
type: 'default', type: 'default',
size size,
} };
} }
} }
@@ -101,26 +104,25 @@ export function optimizeAvatarConfig(
*/ */
export function validateCustomAvatarUrl(url: string): boolean { export function validateCustomAvatarUrl(url: string): boolean {
try { try {
const parsedUrl = new URL(url) const parsedUrl = new URL(url);
// Vérifier le protocole (seulement HTTPS pour la sécurité) // Vérifier le protocole (seulement HTTPS pour la sécurité)
if (parsedUrl.protocol !== 'https:') { if (parsedUrl.protocol !== 'https:') {
return false return false;
} }
// Vérifier que c'est bien une URL d'image // Vérifier que c'est bien une URL d'image
const imageExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.webp', '.svg'] const imageExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.webp', '.svg'];
const hasImageExtension = imageExtensions.some(ext => const hasImageExtension = imageExtensions.some((ext) =>
parsedUrl.pathname.toLowerCase().endsWith(ext) parsedUrl.pathname.toLowerCase().endsWith(ext)
) );
if (!hasImageExtension && parsedUrl.pathname.includes('.')) { if (!hasImageExtension && parsedUrl.pathname.includes('.')) {
return false return false;
} }
return true return true;
} catch { } catch {
return false return false;
} }
} }

View File

@@ -1,10 +1,18 @@
import crypto from 'crypto' import crypto from 'crypto';
export interface GravatarOptions { export interface GravatarOptions {
size?: number size?: number;
defaultImage?: '404' | 'mp' | 'identicon' | 'monsterid' | 'wavatar' | 'retro' | 'robohash' | 'blank' defaultImage?:
rating?: 'g' | 'pg' | 'r' | 'x' | '404'
forcedefault?: 'y' | 'n' | 'mp'
| 'identicon'
| 'monsterid'
| 'wavatar'
| 'retro'
| 'robohash'
| 'blank';
rating?: 'g' | 'pg' | 'r' | 'x';
forcedefault?: 'y' | 'n';
} }
/** /**
@@ -21,30 +29,32 @@ export function getGravatarUrl(
size = 200, size = 200,
defaultImage = 'identicon', defaultImage = 'identicon',
rating = 'g', rating = 'g',
forcedefault = 'n' forcedefault = 'n',
} = options } = options;
// Hacher l'email en MD5 (en minuscules et trimé) // Hacher l'email en MD5 (en minuscules et trimé)
const normalizedEmail = email.toLowerCase().trim() const normalizedEmail = email.toLowerCase().trim();
const hash = crypto.createHash('md5').update(normalizedEmail).digest('hex') const hash = crypto.createHash('md5').update(normalizedEmail).digest('hex');
// Construire l'URL // Construire l'URL
const params = new URLSearchParams({ const params = new URLSearchParams({
size: size.toString(), size: size.toString(),
default: defaultImage, default: defaultImage,
rating, rating,
forcedefault forcedefault,
}) });
return `https://www.gravatar.com/avatar/${hash}?${params.toString()}` return `https://www.gravatar.com/avatar/${hash}?${params.toString()}`;
} }
/** /**
* Valide si une URL est une URL Gravatar valide * Valide si une URL est une URL Gravatar valide
*/ */
export function isGravatarUrl(url: string): boolean { export function isGravatarUrl(url: string): boolean {
return url.startsWith('https://www.gravatar.com/avatar/') || return (
url.startsWith('https://www.gravatar.com/avatar/') ||
url.startsWith('https://gravatar.com/avatar/') url.startsWith('https://gravatar.com/avatar/')
);
} }
/** /**
@@ -54,16 +64,18 @@ export function isGravatarUrl(url: string): boolean {
*/ */
export async function checkGravatarExists(email: string): Promise<boolean> { export async function checkGravatarExists(email: string): Promise<boolean> {
try { try {
const gravatarUrl = getGravatarUrl(email, { defaultImage: '404', forcedefault: 'y' }) const gravatarUrl = getGravatarUrl(email, {
defaultImage: '404',
forcedefault: 'y',
});
const response = await fetch(gravatarUrl, { const response = await fetch(gravatarUrl, {
method: 'HEAD', method: 'HEAD',
signal: AbortSignal.timeout(5000) // 5s timeout signal: AbortSignal.timeout(5000), // 5s timeout
}) });
return response.ok return response.ok;
} catch { } catch {
return false return false;
} }
} }