chore: clean up avatar and gravatar files by removing extra blank lines for improved readability
This commit is contained in:
@@ -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 {
|
||||
type: AvatarType
|
||||
url?: string
|
||||
email?: string
|
||||
size?: number
|
||||
type: AvatarType;
|
||||
url?: string;
|
||||
email?: string;
|
||||
size?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -15,20 +15,20 @@ export interface AvatarConfig {
|
||||
* @returns URL finale de l'avatar ou null
|
||||
*/
|
||||
export function getAvatarUrl(config: AvatarConfig): string | null {
|
||||
const { type, url, email, size = 200 } = config
|
||||
const { type, url, email, size = 200 } = config;
|
||||
|
||||
switch (type) {
|
||||
case 'custom':
|
||||
if (!url) return null
|
||||
return url
|
||||
if (!url) return null;
|
||||
return url;
|
||||
|
||||
case 'gravatar':
|
||||
if (!email) return null
|
||||
return getGravatarUrl(email, { size })
|
||||
if (!email) return null;
|
||||
return getGravatarUrl(email, { size });
|
||||
|
||||
case '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)
|
||||
* @returns Type d'avatar détecté
|
||||
*/
|
||||
export function detectAvatarType(url: string | null | undefined, email?: string): AvatarType {
|
||||
if (!url) return 'default'
|
||||
|
||||
export function detectAvatarType(
|
||||
url: string | null | undefined,
|
||||
email?: string
|
||||
): AvatarType {
|
||||
if (!url) return 'default';
|
||||
|
||||
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
|
||||
if (email && typeof url === 'string') {
|
||||
const expectedGravatarUrl = getGravatarUrl(email)
|
||||
const expectedGravatarUrl = getGravatarUrl(email);
|
||||
if (url.includes(expectedGravatarUrl.split('?')[0].split('avatar/')[1])) {
|
||||
return 'gravatar'
|
||||
return 'gravatar';
|
||||
}
|
||||
}
|
||||
|
||||
return 'custom'
|
||||
|
||||
return 'custom';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -64,33 +67,33 @@ export function detectAvatarType(url: string | null | undefined, email?: string)
|
||||
* @returns Configuration optimisée pour l'avatar
|
||||
*/
|
||||
export function optimizeAvatarConfig(
|
||||
url: string | null | undefined,
|
||||
email: string | undefined,
|
||||
url: string | null | undefined,
|
||||
email: string | undefined,
|
||||
size: number = 200
|
||||
): AvatarConfig {
|
||||
const type = detectAvatarType(url, email)
|
||||
|
||||
const type = detectAvatarType(url, email);
|
||||
|
||||
switch (type) {
|
||||
case 'gravatar':
|
||||
return {
|
||||
type: 'gravatar',
|
||||
email,
|
||||
size
|
||||
}
|
||||
|
||||
size,
|
||||
};
|
||||
|
||||
case 'custom':
|
||||
return {
|
||||
type: 'custom',
|
||||
url: url!,
|
||||
size
|
||||
}
|
||||
|
||||
size,
|
||||
};
|
||||
|
||||
case 'default':
|
||||
default:
|
||||
return {
|
||||
type: 'default',
|
||||
size
|
||||
}
|
||||
size,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,26 +104,25 @@ export function optimizeAvatarConfig(
|
||||
*/
|
||||
export function validateCustomAvatarUrl(url: string): boolean {
|
||||
try {
|
||||
const parsedUrl = new URL(url)
|
||||
|
||||
const parsedUrl = new URL(url);
|
||||
|
||||
// Vérifier le protocole (seulement HTTPS pour la sécurité)
|
||||
if (parsedUrl.protocol !== 'https:') {
|
||||
return false
|
||||
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 =>
|
||||
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 false;
|
||||
}
|
||||
|
||||
return true
|
||||
|
||||
return true;
|
||||
} catch {
|
||||
return false
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user