2026-06-03 19:56:07 +08:00
|
|
|
import { s3Service } from '@shumai/core/src/s3/s3'
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the signed avatar URL if the image is an S3 key,
|
|
|
|
|
* or returns it directly if it's already an external URL (e.g. OAuth avatar).
|
|
|
|
|
*/
|
|
|
|
|
export async function getAvatarUrl(
|
|
|
|
|
imageKeyOrUrl: string | null | undefined,
|
|
|
|
|
): Promise<string | undefined> {
|
|
|
|
|
if (!imageKeyOrUrl) return undefined
|
2026-06-03 23:42:48 +08:00
|
|
|
if (
|
|
|
|
|
imageKeyOrUrl.startsWith('data:') ||
|
|
|
|
|
imageKeyOrUrl.startsWith('http://') ||
|
|
|
|
|
imageKeyOrUrl.startsWith('https://')
|
|
|
|
|
) {
|
2026-06-03 19:56:07 +08:00
|
|
|
return imageKeyOrUrl
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
const bucket = process.env.S3_BUCKET || 'shumai'
|
|
|
|
|
return await s3Service.presign(bucket, imageKeyOrUrl, 'GET')
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(`Failed to presign avatar URL for key ${imageKeyOrUrl}:`, e)
|
|
|
|
|
return undefined
|
|
|
|
|
}
|
|
|
|
|
}
|