Files
MOPC-Portal/src/server/utils/avatar-url.ts
Matt 78334676d0
All checks were successful
Build and Push Docker Image / build (push) Successful in 8m31s
fix: avatar/logo display diagnostics and upload error handling
Add error logging to silent catch blocks in avatar/logo URL generation,
show user avatar on admin member detail page, and surface specific error
messages for upload failures (CORS/network issues) instead of generic errors.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-05 02:11:16 +01:00

37 lines
1.2 KiB
TypeScript

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)
} catch (error) {
console.error('[AvatarURL] Failed to generate URL for key:', profileImageKey, error)
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),
}))
)
}