2026-05-19 16:05:09 +08:00
|
|
|
import { Hono } from 'hono'
|
|
|
|
|
import { zValidator } from '@hono/zod-validator'
|
2026-08-01 15:28:23 +08:00
|
|
|
import { postAttachmentRequestSchema, AuditAction } from '@shumai/dtos'
|
2026-06-01 23:16:22 +08:00
|
|
|
import { assetService } from '@shumai/core/src/asset/asset'
|
|
|
|
|
import { s3Service } from '@shumai/core/src/s3/s3'
|
2026-08-01 14:49:08 +08:00
|
|
|
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
|
2026-08-01 15:28:23 +08:00
|
|
|
import { AssetType, prisma } from '@shumai/db'
|
2026-05-19 16:05:09 +08:00
|
|
|
import { ulid } from 'ulid'
|
2026-06-01 23:16:22 +08:00
|
|
|
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
|
|
|
|
import type { Prisma } from '@shumai/db'
|
2026-05-19 16:05:09 +08:00
|
|
|
|
|
|
|
|
type User = Prisma.UserGetPayload<Record<string, never>>
|
|
|
|
|
|
|
|
|
|
const route = new Hono<{ Variables: { user: User } }>().post(
|
|
|
|
|
'/projects/:projectId/attachments',
|
|
|
|
|
zValidator('json', postAttachmentRequestSchema),
|
|
|
|
|
async (c) => {
|
|
|
|
|
const projectId = c.req.param('projectId')
|
|
|
|
|
const user = c.get('user')
|
|
|
|
|
const req = c.req.valid('json')
|
|
|
|
|
|
|
|
|
|
await authzService.hasPermission({
|
|
|
|
|
user,
|
|
|
|
|
permission: Permission.Edit,
|
2026-05-25 23:51:37 +08:00
|
|
|
type: ResourceType.Project,
|
|
|
|
|
id: projectId,
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const key = `attachments/${ulid()}/${req.fileName}`
|
|
|
|
|
|
|
|
|
|
const assetInfo = await assetService.createAsset({
|
|
|
|
|
name: req.fileName,
|
|
|
|
|
type: AssetType.attachment,
|
|
|
|
|
projectId: projectId,
|
|
|
|
|
key: key,
|
|
|
|
|
sizeByte: req.size,
|
|
|
|
|
contentType: req.contentType,
|
|
|
|
|
creatorId: user.id,
|
|
|
|
|
})
|
|
|
|
|
|
2026-08-01 14:49:08 +08:00
|
|
|
const proj = await prisma.project.findUnique({
|
|
|
|
|
where: { id: projectId },
|
|
|
|
|
select: { teamId: true },
|
|
|
|
|
})
|
|
|
|
|
if (proj) {
|
|
|
|
|
await auditLogService.logAction({
|
|
|
|
|
action: AuditAction.file_create,
|
|
|
|
|
teamId: proj.teamId,
|
|
|
|
|
userId: user.id,
|
|
|
|
|
projectId,
|
|
|
|
|
itemId: assetInfo.id,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-19 16:05:09 +08:00
|
|
|
const uploadUrl = await s3Service.presign(process.env.S3_BUCKET || 'shumai', key, 'PUT')
|
|
|
|
|
|
|
|
|
|
return c.json({
|
|
|
|
|
id: assetInfo.id,
|
|
|
|
|
uploadUrl: uploadUrl,
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
export default route
|