feat(backend): generate presigned avatar URLs for collection and share link creators

This commit is contained in:
yiling
2026-06-06 12:25:07 +08:00
parent d3dd5f9b0c
commit 02e845ef71
2 changed files with 17 additions and 17 deletions
+9 -9
View File
@@ -2,6 +2,7 @@ import { zValidator } from '@hono/zod-validator'
import { Hono } from 'hono'
import { collectionService } from '@shumai/core/src/collection/collection'
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
import { getAvatarUrl } from '@shumai/core/src/user/avatar'
import {
createCollectionRequestSchema,
updateCollectionRequestSchema,
@@ -13,9 +14,10 @@ import type { Prisma } from '@shumai/db'
type User = Prisma.UserGetPayload<Record<string, never>>
function toCollectionInfo(
async function toCollectionInfo(
c: Prisma.CollectionGetPayload<{ include: { creator: true } }>,
): CollectionInfo {
): Promise<CollectionInfo> {
const avatarUrl = c.creator ? await getAvatarUrl(c.creator.image) : undefined
return {
id: c.id,
name: c.name,
@@ -23,9 +25,7 @@ function toCollectionInfo(
projectId: c.projectId,
createdAt: c.createdAt.toISOString(),
updatedAt: c.updatedAt.toISOString(),
creator: c.creator
? { id: c.creator.id, name: c.creator.name, image: c.creator.image ?? undefined }
: null,
creator: c.creator ? { id: c.creator.id, name: c.creator.name, image: avatarUrl } : null,
}
}
@@ -46,7 +46,7 @@ const route = new Hono<{ Variables: { user: User } }>()
})
const collection = await collectionService.createCollection(projectId, req, user.id)
return c.json(toCollectionInfo(collection))
return c.json(await toCollectionInfo(collection))
},
)
.get(
@@ -67,7 +67,7 @@ const route = new Hono<{ Variables: { user: User } }>()
const collections = await collectionService.listCollections(projectId, req)
return c.json({
// eslint-disable-next-line @typescript-eslint/no-explicit-any
data: collections.data.map((c: any) => toCollectionInfo(c)),
data: await Promise.all(collections.data.map((c: any) => toCollectionInfo(c))),
pageInfo: collections.pageInfo,
})
},
@@ -88,7 +88,7 @@ const route = new Hono<{ Variables: { user: User } }>()
return c.json({ error: 'Collection not found' }, 404)
}
return c.json(toCollectionInfo(collection))
return c.json(await toCollectionInfo(collection))
})
.patch(
'/collections/:collectionId',
@@ -106,7 +106,7 @@ const route = new Hono<{ Variables: { user: User } }>()
})
const collection = await collectionService.updateCollection(collectionId, req)
return c.json(toCollectionInfo(collection))
return c.json(await toCollectionInfo(collection))
},
)
.delete('/collections/:collectionId', async (c) => {
+8 -8
View File
@@ -8,6 +8,7 @@ import {
AddAssetToShareRequest,
} from '@shumai/dtos'
import { PaginatedData, paginateQuery } from '@shumai/core/src/pagination'
import { getAvatarUrl } from '@shumai/core/src/user/avatar'
export class ShareService {
async createShareLink(
@@ -64,7 +65,7 @@ export class ShareService {
include: { creator: true },
})
return this.toShareLinkInfo(shareLink)
return await this.toShareLinkInfo(shareLink)
})
}
@@ -95,7 +96,7 @@ export class ShareService {
})
}
return this.toShareLinkInfo(updated)
return await this.toShareLinkInfo(updated)
}
async deleteShareLink(shareLinkId: string): Promise<void> {
@@ -128,7 +129,7 @@ export class ShareService {
take,
orderBy: { createdAt: 'desc' },
})
return links.map((l) => this.toShareLinkInfo(l))
return await Promise.all(links.map((l) => this.toShareLinkInfo(l)))
},
() => prisma.shareLink.count({ where }),
req,
@@ -141,7 +142,7 @@ export class ShareService {
include: { creator: true },
})
if (!shareLink) throw new Error('Share link not found')
return this.toShareLinkInfo(shareLink)
return await this.toShareLinkInfo(shareLink)
}
async addAssetToShare(shareLinkId: string, req: AddAssetToShareRequest): Promise<number> {
@@ -289,8 +290,9 @@ export class ShareService {
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private toShareLinkInfo(l: any): ShareLinkInfo {
private async toShareLinkInfo(l: any): Promise<ShareLinkInfo> {
const isExpired = l.expireAt ? new Date(l.expireAt) < new Date() : false
const avatarUrl = l.creator ? await getAvatarUrl(l.creator.image) : undefined
return {
id: l.id,
name: l.name,
@@ -304,9 +306,7 @@ export class ShareService {
isExpired,
createdAt: l.createdAt.toISOString(),
updatedAt: l.updatedAt.toISOString(),
creator: l.creator
? { id: l.creator.id, name: l.creator.name, image: l.creator.image ?? undefined }
: null,
creator: l.creator ? { id: l.creator.id, name: l.creator.name, image: avatarUrl } : null,
}
}
}