mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-23 13:37:40 +00:00
refactor(core): move starting symlink resolution to getDownloadLinks and simplify getProjectIds
This commit is contained in:
@@ -455,12 +455,9 @@ describe('file api', () => {
|
||||
})
|
||||
|
||||
it('POST /files/download-links generates links if assets belong to the same project', async () => {
|
||||
const mockGetProjectIdsAndStartingIds = vi
|
||||
.spyOn(assetService, 'getProjectIdsAndStartingIds')
|
||||
.mockResolvedValue({
|
||||
projectIds: ['project-id-1'],
|
||||
startingIds: ['asset1', 'asset2'],
|
||||
})
|
||||
const mockGetProjectIds = vi
|
||||
.spyOn(assetService, 'getProjectIds')
|
||||
.mockResolvedValue(['project-id-1'])
|
||||
|
||||
const mockGetDownloadLinks = vi.spyOn(assetService, 'getDownloadLinks').mockResolvedValue([
|
||||
{ id: 'file1', name: 'file1.txt', url: 'http://link1' },
|
||||
@@ -488,17 +485,14 @@ describe('file api', () => {
|
||||
|
||||
expect(assetService.getDownloadLinks).toHaveBeenCalledWith(['asset1', 'asset2'])
|
||||
|
||||
mockGetProjectIdsAndStartingIds.mockRestore()
|
||||
mockGetProjectIds.mockRestore()
|
||||
mockGetDownloadLinks.mockRestore()
|
||||
})
|
||||
|
||||
it('POST /files/download-links rejects if assets belong to different projects', async () => {
|
||||
const mockGetProjectIdsAndStartingIds = vi
|
||||
.spyOn(assetService, 'getProjectIdsAndStartingIds')
|
||||
.mockResolvedValue({
|
||||
projectIds: ['project-id-1', 'project-id-2'],
|
||||
startingIds: ['asset1', 'asset2'],
|
||||
})
|
||||
const mockGetProjectIds = vi
|
||||
.spyOn(assetService, 'getProjectIds')
|
||||
.mockResolvedValue(['project-id-1', 'project-id-2'])
|
||||
|
||||
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
||||
const res = await app.request('/files/download-links', {
|
||||
@@ -511,6 +505,6 @@ describe('file api', () => {
|
||||
const json = await res.json()
|
||||
expect(json.error).toBe('All selected items must belong to the same project')
|
||||
|
||||
mockGetProjectIdsAndStartingIds.mockRestore()
|
||||
mockGetProjectIds.mockRestore()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -273,10 +273,10 @@ const route = new Hono<{ Variables: { user: User } }>()
|
||||
return c.json({ files: [] })
|
||||
}
|
||||
|
||||
// 1. Fetch project IDs and starting IDs from the service layer
|
||||
const { projectIds, startingIds } = await assetService.getProjectIdsAndStartingIds(req.ids)
|
||||
// 1. Fetch project IDs from the service layer
|
||||
const projectIds = await assetService.getProjectIds(req.ids)
|
||||
|
||||
if (startingIds.length === 0) {
|
||||
if (projectIds.length === 0) {
|
||||
return c.json({ files: [] })
|
||||
}
|
||||
|
||||
@@ -296,7 +296,7 @@ const route = new Hono<{ Variables: { user: User } }>()
|
||||
})
|
||||
|
||||
// 4. Generate download links
|
||||
const files = await assetService.getDownloadLinks(startingIds)
|
||||
const files = await assetService.getDownloadLinks(req.ids)
|
||||
return c.json({ files })
|
||||
})
|
||||
|
||||
|
||||
@@ -1803,16 +1803,52 @@ describe('AssetService — natural sort by name', () => {
|
||||
expect(item.url).toBe('http://mock-s3-url')
|
||||
}
|
||||
})
|
||||
|
||||
it('resolves top-level symlinks to their targets when passed as direct starting IDs', async () => {
|
||||
const team = await prisma.team.create({ data: { name: 'Test Team' } })
|
||||
const project = await prisma.project.create({
|
||||
data: { name: 'Test Project', teamId: team.id },
|
||||
})
|
||||
const user = await prisma.user.create({
|
||||
data: { name: 'Test User', email: `test-${Date.now()}@example.com` },
|
||||
})
|
||||
|
||||
const file1 = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'file1.txt',
|
||||
type: AssetType.file,
|
||||
project: { connect: { id: project.id } },
|
||||
creator: { connect: { id: user.id } },
|
||||
status: AssetStatus.uploaded,
|
||||
storageKey: { create: { key: 'keys/file1' } },
|
||||
},
|
||||
})
|
||||
|
||||
const symlink = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'symlinkToFile',
|
||||
type: AssetType.symlink,
|
||||
project: { connect: { id: project.id } },
|
||||
creator: { connect: { id: user.id } },
|
||||
status: AssetStatus.uploaded,
|
||||
target: { connect: { id: file1.id } },
|
||||
},
|
||||
})
|
||||
|
||||
const result = await assetService.getDownloadLinks([symlink.id])
|
||||
expect(result).toHaveLength(1)
|
||||
expect(result[0].name).toBe('file1.txt')
|
||||
expect(result[0].id).toBe(file1.id)
|
||||
})
|
||||
})
|
||||
|
||||
describe('getProjectIdsAndStartingIds', () => {
|
||||
describe('getProjectIds', () => {
|
||||
it('returns empty lists for empty inputs', async () => {
|
||||
const result = await assetService.getProjectIdsAndStartingIds([])
|
||||
expect(result.projectIds).toEqual([])
|
||||
expect(result.startingIds).toEqual([])
|
||||
const result = await assetService.getProjectIds([])
|
||||
expect(result).toEqual([])
|
||||
})
|
||||
|
||||
it('resolves project IDs and starting IDs, including symlinks', async () => {
|
||||
it('resolves project IDs, including symlinks', async () => {
|
||||
const team = await prisma.team.create({ data: { name: 'Test Team' } })
|
||||
const project = await prisma.project.create({
|
||||
data: { name: 'Test Project', teamId: team.id },
|
||||
@@ -1842,13 +1878,9 @@ describe('AssetService — natural sort by name', () => {
|
||||
},
|
||||
})
|
||||
|
||||
const result = await assetService.getProjectIdsAndStartingIds([file1.id, symlink.id])
|
||||
expect(result.projectIds).toContain(project.id)
|
||||
expect(result.projectIds).toHaveLength(1)
|
||||
expect(result.startingIds).toHaveLength(2)
|
||||
// Since it's a symlink, its starting ID should be resolved to the target file1.id
|
||||
expect(result.startingIds[0]).toBe(file1.id)
|
||||
expect(result.startingIds[1]).toBe(file1.id)
|
||||
const result = await assetService.getProjectIds([file1.id, symlink.id])
|
||||
expect(result).toContain(project.id)
|
||||
expect(result).toHaveLength(1)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1649,28 +1649,21 @@ export class AssetService {
|
||||
)
|
||||
}
|
||||
|
||||
async getProjectIdsAndStartingIds(assetIds: string[]): Promise<{
|
||||
projectIds: string[]
|
||||
startingIds: string[]
|
||||
}> {
|
||||
async getProjectIds(assetIds: string[]): Promise<string[]> {
|
||||
if (assetIds.length === 0) {
|
||||
return { projectIds: [], startingIds: [] }
|
||||
return []
|
||||
}
|
||||
|
||||
const assets = await this.prismaClient.asset.findMany({
|
||||
where: { id: { in: assetIds }, isDeleted: false },
|
||||
select: {
|
||||
id: true,
|
||||
projectId: true,
|
||||
type: true,
|
||||
targetId: true,
|
||||
target: { select: { projectId: true } },
|
||||
},
|
||||
})
|
||||
|
||||
const projectIds = new Set<string>()
|
||||
const startingIds: string[] = []
|
||||
|
||||
for (const asset of assets) {
|
||||
let projId = asset.projectId
|
||||
if (asset.type === 'symlink' && asset.target?.projectId) {
|
||||
@@ -1679,23 +1672,31 @@ export class AssetService {
|
||||
if (projId) {
|
||||
projectIds.add(projId)
|
||||
}
|
||||
|
||||
if (asset.type === 'symlink' && asset.targetId) {
|
||||
startingIds.push(asset.targetId)
|
||||
} else {
|
||||
startingIds.push(asset.id)
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
projectIds: Array.from(projectIds),
|
||||
startingIds,
|
||||
}
|
||||
return Array.from(projectIds)
|
||||
}
|
||||
|
||||
async getDownloadLinks(
|
||||
startingIds: string[],
|
||||
): Promise<Array<{ id: string; name: string; url: string }>> {
|
||||
async getDownloadLinks(ids: string[]): Promise<Array<{ id: string; name: string; url: string }>> {
|
||||
if (ids.length === 0) return []
|
||||
|
||||
// Resolve starting IDs (dereference top-level symlinks)
|
||||
const assets = await this.prismaClient.asset.findMany({
|
||||
where: { id: { in: ids }, isDeleted: false },
|
||||
select: {
|
||||
id: true,
|
||||
type: true,
|
||||
targetId: true,
|
||||
},
|
||||
})
|
||||
|
||||
const startingIds = assets.map((asset) => {
|
||||
if (asset.type === 'symlink' && asset.targetId) {
|
||||
return asset.targetId
|
||||
}
|
||||
return asset.id
|
||||
})
|
||||
|
||||
if (startingIds.length === 0) return []
|
||||
|
||||
const descendants = await this.prismaClient.$queryRaw<
|
||||
|
||||
Reference in New Issue
Block a user