2026-02-02 13:19:28 +01:00
|
|
|
import { createStorageProvider, type StorageProviderType } from '@/lib/storage'
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate a pre-signed download URL for a user's avatar.
|
|
|
|
|
* Returns null if the user has no avatar.
|
|
|
|
|
*/
|
|
|
|
|
export async function getUserAvatarUrl(
|
|
|
|
|
profileImageKey: string | null | undefined,
|
|
|
|
|
profileImageProvider: string | null | undefined
|
|
|
|
|
): Promise<string | null> {
|
|
|
|
|
if (!profileImageKey) return null
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const providerType = (profileImageProvider as StorageProviderType) || 's3'
|
|
|
|
|
const provider = createStorageProvider(providerType)
|
|
|
|
|
return await provider.getDownloadUrl(profileImageKey)
|
2026-03-05 02:11:16 +01:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error('[AvatarURL] Failed to generate URL for key:', profileImageKey, error)
|
2026-02-02 13:19:28 +01:00
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Batch-generate avatar URLs for multiple users.
|
|
|
|
|
* Adds `avatarUrl` field to each user object.
|
|
|
|
|
*/
|
|
|
|
|
export async function attachAvatarUrls<
|
|
|
|
|
T extends { profileImageKey?: string | null; profileImageProvider?: string | null }
|
|
|
|
|
>(users: T[]): Promise<(T & { avatarUrl: string | null })[]> {
|
|
|
|
|
return Promise.all(
|
|
|
|
|
users.map(async (user) => ({
|
|
|
|
|
...user,
|
|
|
|
|
avatarUrl: await getUserAvatarUrl(user.profileImageKey, user.profileImageProvider),
|
|
|
|
|
}))
|
|
|
|
|
)
|
|
|
|
|
}
|