mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-24 10:16:56 +00:00
feat(file): add bulk download links API and frontend batch download dialog
This commit is contained in:
@@ -36,6 +36,7 @@ vi.mock('@shumai/core/src/authz/authz', () => ({
|
||||
ResourceType: {
|
||||
Asset: 'asset',
|
||||
Team: 'team',
|
||||
Project: 'project',
|
||||
},
|
||||
}))
|
||||
|
||||
@@ -452,4 +453,84 @@ describe('file api', () => {
|
||||
beforeIndex: 'index-1',
|
||||
})
|
||||
})
|
||||
|
||||
it('POST /files/download-links generates links if assets belong to the same project', async () => {
|
||||
const mockFindMany = vi.spyOn(prisma.asset, 'findMany').mockResolvedValue([
|
||||
{
|
||||
id: 'asset1',
|
||||
projectId: 'project-id-1',
|
||||
type: 'file',
|
||||
targetId: null,
|
||||
},
|
||||
{
|
||||
id: 'asset2',
|
||||
projectId: 'project-id-1',
|
||||
type: 'folder',
|
||||
targetId: null,
|
||||
},
|
||||
// Mocking only the required subset of Prisma Asset properties for the test
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
] as any)
|
||||
|
||||
const mockGetDownloadLinks = vi.spyOn(assetService, 'getDownloadLinks').mockResolvedValue([
|
||||
{ id: 'file1', name: 'file1.txt', url: 'http://link1' },
|
||||
{ id: 'file2', name: 'file2.txt', url: 'http://link2' },
|
||||
])
|
||||
|
||||
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
||||
const res = await app.request('/files/download-links', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ ids: ['asset1', 'asset2'] }),
|
||||
})
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const json = await res.json()
|
||||
expect(json.files).toHaveLength(2)
|
||||
expect(json.files[0].name).toBe('file1.txt')
|
||||
|
||||
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
||||
user: { id: 'user1', name: 'Test User' },
|
||||
permission: Permission.Read,
|
||||
type: 'project',
|
||||
id: 'project-id-1',
|
||||
})
|
||||
|
||||
expect(assetService.getDownloadLinks).toHaveBeenCalledWith(['asset1', 'asset2'])
|
||||
|
||||
mockFindMany.mockRestore()
|
||||
mockGetDownloadLinks.mockRestore()
|
||||
})
|
||||
|
||||
it('POST /files/download-links rejects if assets belong to different projects', async () => {
|
||||
const mockFindMany = vi.spyOn(prisma.asset, 'findMany').mockResolvedValue([
|
||||
{
|
||||
id: 'asset1',
|
||||
projectId: 'project-id-1',
|
||||
type: 'file',
|
||||
targetId: null,
|
||||
},
|
||||
{
|
||||
id: 'asset2',
|
||||
projectId: 'project-id-2',
|
||||
type: 'file',
|
||||
targetId: null,
|
||||
},
|
||||
// Mocking only the required subset of Prisma Asset properties for the test
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
] as any)
|
||||
|
||||
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
||||
const res = await app.request('/files/download-links', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ ids: ['asset1', 'asset2'] }),
|
||||
})
|
||||
|
||||
expect(res.status).toBe(400)
|
||||
const json = await res.json()
|
||||
expect(json.error).toBe('All selected items must belong to the same project')
|
||||
|
||||
mockFindMany.mockRestore()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -15,6 +15,7 @@ import {
|
||||
updateAssetOrderRequestSchema,
|
||||
updateFileRequestSchema,
|
||||
uploadFileRequestSchema,
|
||||
getDownloadLinksRequestSchema,
|
||||
} from '@shumai/dtos'
|
||||
import { transcodeService } from '@shumai/transcode/src/transcode'
|
||||
import fs from 'fs'
|
||||
@@ -264,5 +265,67 @@ const route = new Hono<{ Variables: { user: User } }>()
|
||||
|
||||
return c.json({ key })
|
||||
})
|
||||
.post('/files/download-links', zValidator('json', getDownloadLinksRequestSchema), async (c) => {
|
||||
const user = c.get('user')
|
||||
const req = c.req.valid('json')
|
||||
|
||||
if (req.ids.length === 0) {
|
||||
return c.json({ files: [] })
|
||||
}
|
||||
|
||||
// 1. Fetch input assets to verify project grouping
|
||||
const assets = await prisma.asset.findMany({
|
||||
where: { id: { in: req.ids }, isDeleted: false },
|
||||
select: {
|
||||
id: true,
|
||||
projectId: true,
|
||||
type: true,
|
||||
targetId: true,
|
||||
target: { select: { projectId: true } },
|
||||
},
|
||||
})
|
||||
|
||||
if (assets.length === 0) {
|
||||
return c.json({ files: [] })
|
||||
}
|
||||
|
||||
const projectIds = new Set<string>()
|
||||
for (const asset of assets) {
|
||||
let projId = asset.projectId
|
||||
if (asset.type === 'symlink' && asset.target?.projectId) {
|
||||
projId = asset.target.projectId
|
||||
}
|
||||
if (projId) {
|
||||
projectIds.add(projId)
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Validate same-project constraint
|
||||
if (projectIds.size !== 1) {
|
||||
return c.json({ error: 'All selected items must belong to the same project' }, 400)
|
||||
}
|
||||
|
||||
const projectId = Array.from(projectIds)[0]
|
||||
|
||||
// 3. Verify user has READ permission on this project
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Read,
|
||||
type: ResourceType.Project,
|
||||
id: projectId,
|
||||
})
|
||||
|
||||
// 4. Resolve starting IDs (dereference top-level symlinks)
|
||||
const startingIds = assets.map((asset) => {
|
||||
if (asset.type === 'symlink' && asset.targetId) {
|
||||
return asset.targetId
|
||||
}
|
||||
return asset.id
|
||||
})
|
||||
|
||||
// 5. Generate download links
|
||||
const files = await assetService.getDownloadLinks(startingIds)
|
||||
return c.json({ files })
|
||||
})
|
||||
|
||||
export default route
|
||||
|
||||
Reference in New Issue
Block a user