mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-04 06:54:28 +00:00
91 lines
3.0 KiB
TypeScript
91 lines
3.0 KiB
TypeScript
import { zValidator } from '@hono/zod-validator'
|
|||
|
|
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
||
|
|
import { transcriptService } from '@shumai/core/src/transcript/transcript'
|
||
|
|
import type { User } from '@shumai/db'
|
||
|
|
import {
|
||
|
|
generateTranscriptRequestSchema,
|
||
|
|
transcriptLanguageCodeSchema,
|
||
|
|
updateTranscriptSegmentRequestSchema,
|
||
|
|
} from '@shumai/dtos'
|
||
|
|
import { Hono } from 'hono'
|
||
|
|
import { z } from 'zod'
|
||
|
|
|
||
|
|
const route = new Hono<{ Variables: { user: User } }>()
|
||
|
|
.get('/transcription/languages', async (c) => c.json(await transcriptService.listLanguages()))
|
||
|
|
.get('/files/:fileId/transcripts', async (c) => {
|
||
|
|
const assetId = c.req.param('fileId')
|
||
|
|
await authzService.hasPermission({
|
||
|
|
user: c.get('user'),
|
||
|
|
permission: Permission.Read,
|
||
|
|
type: ResourceType.Asset,
|
||
|
|
id: assetId,
|
||
|
|
})
|
||
|
|
return c.json(await transcriptService.listSavedLanguages(assetId))
|
||
|
|
})
|
||
|
|
.get(
|
||
|
|
'/files/:fileId/transcripts/:language',
|
||
|
|
zValidator('param', z.object({ fileId: z.string(), language: transcriptLanguageCodeSchema })),
|
||
|
|
async (c) => {
|
||
|
|
const { fileId, language } = c.req.valid('param')
|
||
|
|
await authzService.hasPermission({
|
||
|
|
user: c.get('user'),
|
||
|
|
permission: Permission.Read,
|
||
|
|
type: ResourceType.Asset,
|
||
|
|
id: fileId,
|
||
|
|
})
|
||
|
|
return c.json(await transcriptService.getSavedTranscript(fileId, language))
|
||
|
|
},
|
||
|
|
)
|
||
|
|
.patch(
|
||
|
|
'/files/:fileId/transcripts/:language/segments/:segmentId',
|
||
|
|
zValidator(
|
||
|
|
'param',
|
||
|
|
z.object({
|
||
|
|
fileId: z.string(),
|
||
|
|
language: transcriptLanguageCodeSchema,
|
||
|
|
segmentId: z.string(),
|
||
|
|
}),
|
||
|
|
),
|
||
|
|
zValidator('json', updateTranscriptSegmentRequestSchema),
|
||
|
|
async (c) => {
|
||
|
|
const { fileId, language, segmentId } = c.req.valid('param')
|
||
|
|
await authzService.hasPermission({
|
||
|
|
user: c.get('user'),
|
||
|
|
permission: Permission.Edit,
|
||
|
|
type: ResourceType.Asset,
|
||
|
|
id: fileId,
|
||
|
|
})
|
||
|
|
const { text } = c.req.valid('json')
|
||
|
|
return c.json(await transcriptService.updateSegmentText(fileId, language, segmentId, text))
|
||
|
|
},
|
||
|
|
)
|
||
|
|
.post(
|
||
|
|
'/files/:fileId/transcripts',
|
||
|
|
zValidator('json', generateTranscriptRequestSchema),
|
||
|
|
async (c) => {
|
||
|
|
const assetId = c.req.param('fileId')
|
||
|
|
await authzService.hasPermission({
|
||
|
|
user: c.get('user'),
|
||
|
|
permission: Permission.Edit,
|
||
|
|
type: ResourceType.Asset,
|
||
|
|
id: assetId,
|
||
|
|
})
|
||
|
|
|
||
|
|
const { language } = c.req.valid('json')
|
||
|
|
const result = await transcriptService.generate(assetId, language ?? null)
|
||
|
|
return c.json(result, result.kind === 'queued' || result.kind === 'busy' ? 202 : 200)
|
||
|
|
},
|
||
|
|
)
|
||
|
|
.get('/files/:fileId/transcription-status', async (c) => {
|
||
|
|
const assetId = c.req.param('fileId')
|
||
|
|
await authzService.hasPermission({
|
||
|
|
user: c.get('user'),
|
||
|
|
permission: Permission.Edit,
|
||
|
|
type: ResourceType.Asset,
|
||
|
|
id: assetId,
|
||
|
|
})
|
||
|
|
return c.json(await transcriptService.getStatus(assetId))
|
||
|
|
})
|
||
|
|
|
||
|
|
export default route
|