2026-05-19 16:05:09 +08:00
|
|
|
import { Hono } from 'hono'
|
|
|
|
|
import { zValidator } from '@hono/zod-validator'
|
2026-05-25 23:51:37 +08:00
|
|
|
import { authzService, Permission, ResourceType } from '@/services/authz/authz'
|
2026-05-19 16:05:09 +08:00
|
|
|
import { assetService } from '@/services/asset/asset'
|
|
|
|
|
import { metadataService } from '@/services/metadata/metadata'
|
|
|
|
|
import { notificationService } from '@/services/notification/notification'
|
|
|
|
|
import { s3Service } from '@/services/s3/s3'
|
|
|
|
|
import {
|
|
|
|
|
updateFileRequestSchema,
|
|
|
|
|
updateAssetOrderRequestSchema,
|
|
|
|
|
deleteFilesRequestSchema,
|
|
|
|
|
restoreFilesRequestSchema,
|
|
|
|
|
createCommentRequestSchema,
|
|
|
|
|
uploadFileRequestSchema,
|
|
|
|
|
} from '@/dtos/asset'
|
|
|
|
|
import { updateAssetMetadataRequestSchema } from '@/dtos/metadata'
|
|
|
|
|
import { paginationParamsSchema } from '@/dtos/pagination'
|
|
|
|
|
import { ulid } from 'ulid'
|
|
|
|
|
import { z } from 'zod'
|
|
|
|
|
import type { Prisma } from '@/generated/prisma/client'
|
|
|
|
|
|
|
|
|
|
type User = Prisma.UserGetPayload<Record<string, never>>
|
|
|
|
|
|
|
|
|
|
const route = new Hono<{ Variables: { user: User } }>()
|
2026-05-25 23:51:37 +08:00
|
|
|
.get('/files/:fileId', async (c) => {
|
2026-05-19 16:05:09 +08:00
|
|
|
const fileId = c.req.param('fileId')
|
|
|
|
|
const user = c.get('user')
|
|
|
|
|
|
|
|
|
|
await authzService.hasPermission({
|
|
|
|
|
user,
|
|
|
|
|
permission: Permission.Read,
|
2026-05-25 23:51:37 +08:00
|
|
|
type: ResourceType.Asset,
|
|
|
|
|
id: fileId,
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const asset = await assetService.getAsset({ assetId: fileId })
|
|
|
|
|
return c.json(asset)
|
|
|
|
|
})
|
|
|
|
|
.patch(
|
2026-05-25 23:51:37 +08:00
|
|
|
'/files/:fileId/order',
|
2026-05-19 16:05:09 +08:00
|
|
|
zValidator('json', updateAssetOrderRequestSchema),
|
|
|
|
|
async (c) => {
|
|
|
|
|
const fileId = c.req.param('fileId')
|
|
|
|
|
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.Asset,
|
|
|
|
|
id: fileId,
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const updated = await assetService.updateAssetOrder(fileId, req)
|
|
|
|
|
return c.json(updated)
|
|
|
|
|
},
|
|
|
|
|
)
|
2026-05-25 23:51:37 +08:00
|
|
|
.put('/files/:fileId', zValidator('json', updateFileRequestSchema), async (c) => {
|
2026-05-19 16:05:09 +08:00
|
|
|
const fileId = c.req.param('fileId')
|
|
|
|
|
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.Asset,
|
|
|
|
|
id: fileId,
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const updatedAsset = await assetService.updateAssetName({
|
|
|
|
|
id: fileId,
|
|
|
|
|
name: req.name,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return c.json(updatedAsset)
|
|
|
|
|
})
|
2026-05-25 23:51:37 +08:00
|
|
|
.delete('/files', zValidator('json', deleteFilesRequestSchema), async (c) => {
|
2026-05-19 16:05:09 +08:00
|
|
|
const user = c.get('user')
|
|
|
|
|
const req = c.req.valid('json')
|
|
|
|
|
|
|
|
|
|
for (const id of req.ids) {
|
|
|
|
|
await authzService.hasPermission({
|
|
|
|
|
user,
|
|
|
|
|
permission: Permission.Edit,
|
2026-05-25 23:51:37 +08:00
|
|
|
type: ResourceType.Asset,
|
|
|
|
|
id,
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await assetService.deleteAssets(req.ids)
|
|
|
|
|
return c.body(null, 204)
|
|
|
|
|
})
|
|
|
|
|
.patch(
|
2026-05-25 23:51:37 +08:00
|
|
|
'/files/:fileId/metadata',
|
2026-05-19 16:05:09 +08:00
|
|
|
zValidator('json', z.array(updateAssetMetadataRequestSchema)),
|
|
|
|
|
async (c) => {
|
|
|
|
|
const fileId = c.req.param('fileId')
|
|
|
|
|
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.Asset,
|
|
|
|
|
id: fileId,
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
await metadataService.updateAssetMetadata(fileId, req)
|
|
|
|
|
|
|
|
|
|
const hasStatus = req.some((m) => m.key === 'status')
|
|
|
|
|
if (hasStatus) {
|
2026-05-25 23:51:37 +08:00
|
|
|
const context = await assetService.getAssetContext(fileId)
|
|
|
|
|
if (context?.teamId) {
|
|
|
|
|
notificationService.create({
|
|
|
|
|
type: 'metadata_field_updated_status',
|
|
|
|
|
teamId: context.teamId,
|
|
|
|
|
creatorId: user.id,
|
|
|
|
|
assetId: fileId,
|
|
|
|
|
})
|
|
|
|
|
}
|
2026-05-19 16:05:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return c.json('')
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.post(
|
2026-05-25 23:51:37 +08:00
|
|
|
'/files/:fileId/comments',
|
2026-05-19 16:05:09 +08:00
|
|
|
zValidator('json', createCommentRequestSchema.omit({ assetId: true, userId: true })),
|
|
|
|
|
async (c) => {
|
|
|
|
|
const fileId = c.req.param('fileId')
|
|
|
|
|
const user = c.get('user')
|
|
|
|
|
const req = c.req.valid('json')
|
|
|
|
|
|
|
|
|
|
await authzService.hasPermission({
|
|
|
|
|
user,
|
|
|
|
|
permission: Permission.Read,
|
2026-05-25 23:51:37 +08:00
|
|
|
type: ResourceType.Asset,
|
|
|
|
|
id: fileId,
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const comment = await assetService.createComment({
|
|
|
|
|
assetId: fileId,
|
|
|
|
|
userId: user.id,
|
|
|
|
|
replyToId: req.replyToId,
|
|
|
|
|
message: req.message,
|
|
|
|
|
annotations: req.annotations,
|
|
|
|
|
second: req.second,
|
|
|
|
|
attachmentIds: req.attachmentIds,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const notifType = req.replyToId ? 'reply_created' : 'comment_created'
|
|
|
|
|
|
2026-05-25 23:51:37 +08:00
|
|
|
const context = await assetService.getAssetContext(fileId)
|
|
|
|
|
|
|
|
|
|
if (context?.teamId) {
|
|
|
|
|
notificationService.create({
|
|
|
|
|
type: notifType,
|
|
|
|
|
teamId: context.teamId,
|
|
|
|
|
creatorId: user.id,
|
|
|
|
|
assetId: fileId,
|
|
|
|
|
commentMessage: req.message,
|
|
|
|
|
})
|
|
|
|
|
}
|
2026-05-19 16:05:09 +08:00
|
|
|
|
|
|
|
|
return c.json(comment, 201)
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.get(
|
2026-05-25 23:51:37 +08:00
|
|
|
'/files/:fileId/comments',
|
2026-05-19 16:05:09 +08:00
|
|
|
zValidator('query', paginationParamsSchema),
|
|
|
|
|
async (c) => {
|
|
|
|
|
const fileId = c.req.param('fileId')
|
|
|
|
|
const user = c.get('user')
|
|
|
|
|
const req = c.req.valid('query')
|
|
|
|
|
|
|
|
|
|
await authzService.hasPermission({
|
|
|
|
|
user,
|
|
|
|
|
permission: Permission.Read,
|
2026-05-25 23:51:37 +08:00
|
|
|
type: ResourceType.Asset,
|
|
|
|
|
id: fileId,
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const comments = await assetService.listComments(fileId, req)
|
|
|
|
|
return c.json(comments)
|
|
|
|
|
},
|
|
|
|
|
)
|
2026-05-25 23:51:37 +08:00
|
|
|
.get('/comments/:commentId', async (c) => {
|
2026-05-19 16:05:09 +08:00
|
|
|
const commentId = c.req.param('commentId')
|
|
|
|
|
const user = c.get('user')
|
|
|
|
|
|
|
|
|
|
await authzService.hasPermission({
|
|
|
|
|
user,
|
|
|
|
|
permission: Permission.Read,
|
2026-05-25 23:51:37 +08:00
|
|
|
type: ResourceType.Comment,
|
|
|
|
|
id: commentId,
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
|
2026-05-25 23:51:37 +08:00
|
|
|
const comment = await assetService.getComment(commentId)
|
2026-05-19 16:05:09 +08:00
|
|
|
return c.json(comment)
|
|
|
|
|
})
|
|
|
|
|
.post(
|
2026-05-25 23:51:37 +08:00
|
|
|
'/files/restore',
|
2026-05-19 16:05:09 +08:00
|
|
|
zValidator('json', restoreFilesRequestSchema),
|
|
|
|
|
async (c) => {
|
|
|
|
|
const user = c.get('user')
|
|
|
|
|
const req = c.req.valid('json')
|
|
|
|
|
|
|
|
|
|
for (const id of req.ids) {
|
|
|
|
|
await authzService.hasPermission({
|
|
|
|
|
user,
|
|
|
|
|
permission: Permission.Edit,
|
2026-05-25 23:51:37 +08:00
|
|
|
type: ResourceType.Asset,
|
|
|
|
|
id,
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await assetService.restoreAssets(req.ids)
|
|
|
|
|
return c.body(null, 204)
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.post('/teams/:teamId/files', zValidator('form', uploadFileRequestSchema), async (c) => {
|
|
|
|
|
const teamId = c.req.param('teamId')
|
|
|
|
|
const user = c.get('user')
|
|
|
|
|
const { file } = c.req.valid('form')
|
|
|
|
|
|
|
|
|
|
await authzService.hasPermission({
|
|
|
|
|
user,
|
|
|
|
|
permission: Permission.Read,
|
2026-05-25 23:51:37 +08:00
|
|
|
type: ResourceType.Team,
|
|
|
|
|
id: teamId,
|
2026-05-19 16:05:09 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (file.size > 10 * 1024 * 1024) {
|
|
|
|
|
return c.json('file too large, max 10MB', 400)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const key = `files/${ulid()}`
|
|
|
|
|
const buffer = Buffer.from(await file.arrayBuffer())
|
|
|
|
|
|
|
|
|
|
await s3Service.putObject(process.env.S3_BUCKET || 'shumai', key, buffer, file.size, file.type)
|
|
|
|
|
|
|
|
|
|
return c.json({ key })
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
export default route
|