Files
boursecu_user/src/utils/file.ts
T

30 lines
979 B
TypeScript
Raw Normal View History

2026-05-18 10:13:50 +03:30
import { baseURL } from '@/themeConfig'
2026-06-03 11:57:40 +03:30
import type { User } from '@/models/login'
2026-05-18 10:13:50 +03:30
export interface ProductFile {
id?: number
url?: string | null
src?: string
2026-06-03 11:57:40 +03:30
full_src?: string
2026-05-18 10:13:50 +03:30
}
export function getFileUrl(file?: ProductFile | null): string | null {
if (!file) return null
2026-06-03 11:57:40 +03:30
if (file.full_src) return file.full_src
2026-05-18 10:13:50 +03:30
if (file.url) return file.url
if (!file.src) return null
if (/^https?:\/\//i.test(file.src)) return file.src
const base = baseURL.replace(/\/$/, '')
const path = file.src.replace(/^\//, '')
return `${base}/${path}`
}
2026-06-03 11:57:40 +03:30
/** Resolve profile avatar URL from GET /customer/me payload */
export function getUserAvatarUrl(user?: User | null): string | null {
if (!user) return null
if (typeof user.avatar_url === 'string' && user.avatar_url) return user.avatar_url
if (typeof user.full_src === 'string' && user.full_src) return user.full_src
const nested = user.avatar || user.file
return getFileUrl(nested as ProductFile | undefined)
}