feat(file): optimize upload and transcoding status polling with batch api (#512)

* feat(file): optimize upload and transcoding status polling with batch api

* fix(webui): throttle search invalidation during upload and fix stuck 100% progress ring

* fix(webui): deduplicate upload search invalidation and stabilize batch polling
This commit is contained in:
Yiling-J
2026-10-31 20:11:31 +08:00
committed by GitHub
parent c2348b0e68
commit fbff404b9b
11 changed files with 541 additions and 58 deletions
+74
View File
@@ -9,6 +9,7 @@ import { s3Service } from '@shumai/core/src/s3/s3'
import { transcodeService } from '@shumai/core'
import { authMiddleware } from './middleware/auth'
import { authzService, ResourceType, Permission } from '@shumai/core/src/authz/authz'
import type { AssetInfo } from '@shumai/dtos'
import {
renameAssetOp,
resolveCommentOp,
@@ -72,6 +73,7 @@ describe('file api', () => {
vi.mocked(authzService.hasPermission).mockClear()
vi.spyOn(assetService, 'getAsset').mockImplementation(vi.fn())
vi.spyOn(assetService, 'listAssetsByIds').mockImplementation(vi.fn())
vi.spyOn(assetService, 'getAssetContext').mockResolvedValue({ teamId: 'test-team' })
vi.spyOn(assetService, 'updateAssetName').mockImplementation(vi.fn())
vi.spyOn(assetService, 'deleteAssets').mockImplementation(vi.fn())
@@ -126,6 +128,78 @@ describe('file api', () => {
})
})
describe('POST /files/batch', () => {
it('returns empty array when ids is empty', async () => {
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
const res = await app.request('/files/batch', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ ids: [] }),
})
expect(res.status).toBe(200)
const json = await res.json()
expect(json).toEqual([])
expect(assetService.listAssetsByIds).not.toHaveBeenCalled()
})
it('returns asset list for accessible IDs', async () => {
const assets = [
{ id: 'f1', name: 'file-1.mp4', status: 'processing' },
{ id: 'f2', name: 'file-2.mp4', status: 'processed' },
]
vi.mocked(assetService.listAssetsByIds).mockResolvedValue(assets as unknown as AssetInfo[])
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
const res = await app.request('/files/batch', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ ids: ['f1', 'f2'] }),
})
expect(res.status).toBe(200)
const json = await res.json()
expect(json).toEqual(assets)
expect(assetService.listAssetsByIds).toHaveBeenCalledWith(['f1', 'f2'])
})
it('gracefully omits unauthorized or missing assets without failing the batch', async () => {
vi.mocked(authzService.hasPermission).mockImplementation(async (req) => {
if (req.id === 'unauthorized-id') {
throw new HTTPException(403, { message: 'Permission denied' })
}
})
const accessibleAssets = [{ id: 'f1', name: 'file-1.mp4', status: 'processing' }]
vi.mocked(assetService.listAssetsByIds).mockResolvedValue(
accessibleAssets as unknown as AssetInfo[],
)
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
const res = await app.request('/files/batch', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ ids: ['f1', 'unauthorized-id'] }),
})
expect(res.status).toBe(200)
const json = await res.json()
expect(json).toEqual(accessibleAssets)
expect(assetService.listAssetsByIds).toHaveBeenCalledWith(['f1'])
})
it('rejects with 400 when more than 100 IDs are requested', async () => {
const tooManyIds = Array.from({ length: 101 }, (_, i) => `id-${i}`)
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
const res = await app.request('/files/batch', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ ids: tooManyIds }),
})
expect(res.status).toBe(400)
})
})
it('PUT /files/:fileId runs rename_asset', async () => {
const summary = { id: 'test-id', name: 'new-name', type: 'file', size: 10, projectId: 'p1' }
vi.mocked(runOperation).mockResolvedValue(summary)
+35
View File
@@ -14,6 +14,7 @@ import {
import {
createCommentRequestSchema,
deleteFilesRequestSchema,
batchGetFilesRequestSchema,
paginationParamsSchema,
restoreFilesRequestSchema,
updateAssetMetadataRequestSchema,
@@ -39,6 +40,40 @@ import { z } from 'zod'
type User = Prisma.UserGetPayload<Record<string, never>>
const route = new Hono<{ Variables: { user: User } }>()
.post('/files/batch', zValidator('json', batchGetFilesRequestSchema), async (c) => {
const user = c.get('user')
const req = c.req.valid('json')
if (req.ids.length === 0) {
return c.json([])
}
const accessibleIds: string[] = []
await Promise.all(
req.ids.map(async (id) => {
try {
await authzService.hasPermission({
user,
permission: Permission.Read,
type: ResourceType.Asset,
id,
})
accessibleIds.push(id)
} catch {
// Permissive: silently ignore missing or unauthorized IDs
}
}),
)
if (accessibleIds.length === 0) {
return c.json([])
}
const accessibleSet = new Set(accessibleIds)
const orderedAccessibleIds = req.ids.filter((id) => accessibleSet.has(id))
const assets = await assetService.listAssetsByIds(orderedAccessibleIds)
return c.json(assets)
})
.get('/files/:fileId', async (c) => {
const fileId = c.req.param('fileId')
const user = c.get('user')