mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-29 04:14:30 +00:00
feat(version-stack): add manage versions dialog and version removal (#311)
* feat(version-stack): add manage versions dialog and version removal * fix(webui): remove ellipsis and add pointer cursor to manage versions button * fix(version-stack): handle symlink deduplication and name sync for share links * refactor(version-stack): extract shared symlink dedup helper The 'repoint first symlink per share folder, delete duplicates with fileCount decrement' logic was triplicated across createVersionStack, reparentAssets, and reparentFileToFile with subtle variations. Extract it into dedupeSymlinksToTarget() in packages/core/src/asset/symlink.ts and call it from all three sites (behavior unchanged), with a dedicated test suite covering the edge cases.
This commit is contained in:
@@ -3030,4 +3030,218 @@ describe('AssetService — natural sort by name', () => {
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('reparentAssets into version_stack with sharelink symlinks', () => {
|
||||
it('deduplicates symlinks when both Stack A and File B are in the sharelink and File B is moved into Stack A', async () => {
|
||||
const team = await prisma.team.create({ data: { name: 'Test Team ' + Date.now() } })
|
||||
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 rootFolder = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Root',
|
||||
type: AssetType.folder,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
},
|
||||
})
|
||||
const shareRoot = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'ShareRoot',
|
||||
type: AssetType.share_root,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
fileCount: 2,
|
||||
},
|
||||
})
|
||||
|
||||
const fileA1 = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'fileA1.txt',
|
||||
type: AssetType.file,
|
||||
parentId: rootFolder.id,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
sizeByte: 100,
|
||||
sortIndex: 'a0',
|
||||
},
|
||||
})
|
||||
const fileA2 = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'fileA2.txt',
|
||||
type: AssetType.file,
|
||||
parentId: rootFolder.id,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
sizeByte: 100,
|
||||
sortIndex: 'a1',
|
||||
},
|
||||
})
|
||||
const fileB = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'fileB.txt',
|
||||
type: AssetType.file,
|
||||
parentId: rootFolder.id,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
sizeByte: 200,
|
||||
sortIndex: 'a2',
|
||||
},
|
||||
})
|
||||
|
||||
// Create Stack A from fileA1 and fileA2
|
||||
const stackA = await prisma.asset.create({
|
||||
data: {
|
||||
name: '',
|
||||
type: AssetType.version_stack,
|
||||
parentId: rootFolder.id,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
fileCount: 2,
|
||||
sizeByte: 200,
|
||||
},
|
||||
})
|
||||
await prisma.asset.update({ where: { id: fileA1.id }, data: { parentId: stackA.id } })
|
||||
await prisma.asset.update({ where: { id: fileA2.id }, data: { parentId: stackA.id } })
|
||||
|
||||
// Create symlinks in shareRoot for both Stack A and File B
|
||||
const symlinkStackA = await prisma.asset.create({
|
||||
data: {
|
||||
name: '',
|
||||
type: AssetType.symlink,
|
||||
parentId: shareRoot.id,
|
||||
targetId: stackA.id,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
},
|
||||
})
|
||||
const symlinkFileB = await prisma.asset.create({
|
||||
data: {
|
||||
name: fileB.name,
|
||||
type: AssetType.symlink,
|
||||
parentId: shareRoot.id,
|
||||
targetId: fileB.id,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
},
|
||||
})
|
||||
|
||||
// Move File B into Stack A
|
||||
await assetService.reparentAssets({
|
||||
newParentId: stackA.id,
|
||||
assetIds: [fileB.id],
|
||||
creatorId: user.id,
|
||||
})
|
||||
|
||||
// Verify: File B's symlink was deleted, only Stack A's symlink remains
|
||||
expect(await prisma.asset.findUnique({ where: { id: symlinkFileB.id } })).toBeNull()
|
||||
const remainingSymlinks = await prisma.asset.findMany({
|
||||
where: { parentId: shareRoot.id, type: AssetType.symlink },
|
||||
})
|
||||
expect(remainingSymlinks).toHaveLength(1)
|
||||
expect(remainingSymlinks[0].id).toBe(symlinkStackA.id)
|
||||
expect(remainingSymlinks[0].targetId).toBe(stackA.id)
|
||||
|
||||
// ShareRoot fileCount was decremented from 2 to 1
|
||||
const updatedShareRoot = await prisma.asset.findUnique({ where: { id: shareRoot.id } })
|
||||
expect(updatedShareRoot?.fileCount).toBe(1)
|
||||
})
|
||||
|
||||
it('repoints symlink to Stack A when only File B is in sharelink and File B is moved into Stack A', async () => {
|
||||
const team = await prisma.team.create({ data: { name: 'Test Team ' + Date.now() } })
|
||||
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 rootFolder = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Root',
|
||||
type: AssetType.folder,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
},
|
||||
})
|
||||
const shareRoot = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'ShareRoot',
|
||||
type: AssetType.share_root,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
fileCount: 1,
|
||||
},
|
||||
})
|
||||
|
||||
const fileA1 = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'fileA1.txt',
|
||||
type: AssetType.file,
|
||||
parentId: rootFolder.id,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
sizeByte: 100,
|
||||
sortIndex: 'a0',
|
||||
},
|
||||
})
|
||||
const stackA = await prisma.asset.create({
|
||||
data: {
|
||||
name: '',
|
||||
type: AssetType.version_stack,
|
||||
parentId: rootFolder.id,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
fileCount: 1,
|
||||
sizeByte: 100,
|
||||
},
|
||||
})
|
||||
await prisma.asset.update({ where: { id: fileA1.id }, data: { parentId: stackA.id } })
|
||||
|
||||
const fileB = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'fileB.txt',
|
||||
type: AssetType.file,
|
||||
parentId: rootFolder.id,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
sizeByte: 200,
|
||||
sortIndex: 'a1',
|
||||
},
|
||||
})
|
||||
|
||||
// Create symlink for File B only
|
||||
const symlinkFileB = await prisma.asset.create({
|
||||
data: {
|
||||
name: fileB.name,
|
||||
type: AssetType.symlink,
|
||||
parentId: shareRoot.id,
|
||||
targetId: fileB.id,
|
||||
projectId: project.id,
|
||||
status: AssetStatus.uploaded,
|
||||
},
|
||||
})
|
||||
|
||||
// Move File B into Stack A
|
||||
await assetService.reparentAssets({
|
||||
newParentId: stackA.id,
|
||||
assetIds: [fileB.id],
|
||||
creatorId: user.id,
|
||||
})
|
||||
|
||||
// Verify: symlinkFileB now points to stackA.id
|
||||
const updatedSymlink = await prisma.asset.findUnique({ where: { id: symlinkFileB.id } })
|
||||
expect(updatedSymlink?.targetId).toBe(stackA.id)
|
||||
|
||||
// Listing assets for shareRoot returns Stack A with latest version name
|
||||
const result = await assetService.listChildren({
|
||||
assetId: shareRoot.id,
|
||||
assetType: AssetType.file,
|
||||
})
|
||||
expect(result.data).toHaveLength(1)
|
||||
expect(result.data[0].name).toBe(fileB.name) // fileB is the latest version
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -22,6 +22,7 @@ import { HTTPException } from 'hono/http-exception'
|
||||
import { logger } from '@shumai/core/src/logger'
|
||||
import { PaginatedData, paginateQuery, PaginationParams } from '@shumai/core/src/pagination'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { dedupeSymlinksToTarget } from './symlink'
|
||||
import { watermarkService } from '@shumai/core/src/watermark/watermark'
|
||||
import { generateKeyBetween } from 'jittered-fractional-indexing'
|
||||
import { getAvatarUrl } from '@shumai/core/src/user/avatar'
|
||||
@@ -160,6 +161,9 @@ export class AssetService {
|
||||
const count = await tx.asset.count({ where: { parentId: stack.id } })
|
||||
|
||||
if (count === 0) {
|
||||
await tx.asset.deleteMany({
|
||||
where: { targetId: stack.id, type: AssetType.symlink },
|
||||
})
|
||||
if (stack.parentId) {
|
||||
await tx.asset.update({
|
||||
where: { id: stack.parentId },
|
||||
@@ -174,9 +178,14 @@ export class AssetService {
|
||||
if (!lastChild) return
|
||||
if (!stack.parentId) throw new Error('Stack has no parent')
|
||||
|
||||
await tx.asset.updateMany({
|
||||
where: { targetId: stack.id, type: AssetType.symlink },
|
||||
data: { targetId: lastChild.id, name: lastChild.name },
|
||||
})
|
||||
|
||||
await tx.asset.update({
|
||||
where: { id: lastChild.id },
|
||||
data: { parentId: stack.parentId, sortIndex: null },
|
||||
data: { parentId: stack.parentId, sortIndex: stack.sortIndex },
|
||||
})
|
||||
await tx.asset.delete({ where: { id: stack.id } })
|
||||
}
|
||||
@@ -273,6 +282,14 @@ export class AssetService {
|
||||
firstSortIndex = newSortIndex
|
||||
}
|
||||
|
||||
if (newParent.type === AssetType.version_stack) {
|
||||
await dedupeSymlinksToTarget(tx, {
|
||||
targetIds: req.assetIds,
|
||||
newTargetId: newParent.id,
|
||||
name: '',
|
||||
})
|
||||
}
|
||||
|
||||
const oldParent = await tx.asset.findUnique({
|
||||
where: { id: oldParentId },
|
||||
})
|
||||
@@ -579,6 +596,12 @@ export class AssetService {
|
||||
},
|
||||
})
|
||||
|
||||
// Update existing symlinks pointing to destFile or sourceAsset
|
||||
await dedupeSymlinksToTarget(tx, {
|
||||
targetIds: [destFile.id, sourceAsset.id],
|
||||
newTargetId: stack.id,
|
||||
})
|
||||
|
||||
const oldParent = await tx.asset.findUnique({ where: { id: oldParentId } })
|
||||
if (oldParent) {
|
||||
await this.dissolveStackIfEmpty(tx, oldParent)
|
||||
@@ -1629,6 +1652,7 @@ export class AssetService {
|
||||
id: v.id,
|
||||
name: v.name,
|
||||
previewUrl: preview?.thumbnailUrl || null,
|
||||
createdAt: v.createdAt.toISOString(),
|
||||
creator: v.creator
|
||||
? {
|
||||
id: v.creator.id,
|
||||
@@ -1744,9 +1768,12 @@ export class AssetService {
|
||||
result.push({
|
||||
id: a.id,
|
||||
name:
|
||||
a.type === AssetType.version_stack && (a.name === '' || !a.name)
|
||||
a.type === AssetType.version_stack ||
|
||||
(a.type === AssetType.symlink && a.target?.type === AssetType.version_stack)
|
||||
? latestVersion.name
|
||||
: a.name,
|
||||
: a.type === AssetType.symlink
|
||||
? a.name || latestVersion.name || a.target?.name || ''
|
||||
: a.name,
|
||||
sizeByte: Number(latestVersion.sizeByte),
|
||||
fileCount: latestVersion.fileCount,
|
||||
type: a.type,
|
||||
@@ -1776,7 +1803,8 @@ export class AssetService {
|
||||
version: number
|
||||
name: string
|
||||
previewUrl: string | null
|
||||
creator: { id: string; name: string | null } | null
|
||||
createdAt: string
|
||||
creator: { id: string; name: string | null; image?: string | null } | null
|
||||
}>
|
||||
> {
|
||||
const versions = await this.prismaClient.asset.findMany({
|
||||
@@ -1797,6 +1825,7 @@ export class AssetService {
|
||||
version: versions.length - i,
|
||||
name: v.name,
|
||||
previewUrl: preview?.thumbnailUrl || null,
|
||||
createdAt: v.createdAt.toISOString(),
|
||||
creator: v.creator
|
||||
? {
|
||||
id: v.creator.id,
|
||||
|
||||
@@ -0,0 +1,249 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { prisma } from '@shumai/db'
|
||||
import { setupTestDbHooks } from '@shumai/db/test'
|
||||
import { AssetType } from '@shumai/db'
|
||||
import { dedupeSymlinksToTarget } from './symlink'
|
||||
|
||||
async function setupShareFolder(projectId: string, fileCount: number) {
|
||||
const shareRoot = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'share-root',
|
||||
type: AssetType.share_root,
|
||||
projectId,
|
||||
status: 'uploaded',
|
||||
fileCount,
|
||||
},
|
||||
})
|
||||
return shareRoot
|
||||
}
|
||||
|
||||
describe('dedupeSymlinksToTarget', () => {
|
||||
setupTestDbHooks()
|
||||
|
||||
async function setupProject() {
|
||||
const team = await prisma.team.create({ data: { name: 'Team ' + Date.now() } })
|
||||
const project = await prisma.project.create({
|
||||
data: { name: 'Project ' + Date.now(), teamId: team.id },
|
||||
})
|
||||
const root = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'root',
|
||||
type: AssetType.folder,
|
||||
projectId: project.id,
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
const fileA = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'a.txt',
|
||||
type: AssetType.file,
|
||||
parentId: root.id,
|
||||
projectId: project.id,
|
||||
status: 'uploaded',
|
||||
sizeByte: 10,
|
||||
},
|
||||
})
|
||||
const fileB = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'b.txt',
|
||||
type: AssetType.file,
|
||||
parentId: root.id,
|
||||
projectId: project.id,
|
||||
status: 'uploaded',
|
||||
sizeByte: 20,
|
||||
},
|
||||
})
|
||||
return { project, root, fileA, fileB }
|
||||
}
|
||||
|
||||
it('repoints one symlink per parent and deletes duplicates, decrementing fileCount once', async () => {
|
||||
const { project, root, fileA, fileB } = await setupProject()
|
||||
const shareRoot = await setupShareFolder(project.id, 2)
|
||||
|
||||
const symA = await prisma.asset.create({
|
||||
data: {
|
||||
name: fileA.name,
|
||||
type: AssetType.symlink,
|
||||
parentId: shareRoot.id,
|
||||
targetId: fileA.id,
|
||||
projectId: project.id,
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
const symB = await prisma.asset.create({
|
||||
data: {
|
||||
name: fileB.name,
|
||||
type: AssetType.symlink,
|
||||
parentId: shareRoot.id,
|
||||
targetId: fileB.id,
|
||||
projectId: project.id,
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
|
||||
const stack = await prisma.asset.create({
|
||||
data: {
|
||||
name: '',
|
||||
type: AssetType.version_stack,
|
||||
parentId: root.id,
|
||||
projectId: project.id,
|
||||
status: 'uploaded',
|
||||
fileCount: 2,
|
||||
sizeByte: 30,
|
||||
},
|
||||
})
|
||||
|
||||
await prisma.$transaction((tx) =>
|
||||
dedupeSymlinksToTarget(tx, { targetIds: [fileA.id, fileB.id], newTargetId: stack.id }),
|
||||
)
|
||||
|
||||
// First symlink repointed to the stack, second deleted
|
||||
const repointed = await prisma.asset.findUnique({ where: { id: symA.id } })
|
||||
expect(repointed?.targetId).toBe(stack.id)
|
||||
expect(await prisma.asset.findUnique({ where: { id: symB.id } })).toBeNull()
|
||||
|
||||
// fileCount decremented exactly once (2 -> 1)
|
||||
const updatedShareRoot = await prisma.asset.findUnique({ where: { id: shareRoot.id } })
|
||||
expect(updatedShareRoot?.fileCount).toBe(1)
|
||||
})
|
||||
|
||||
it('deletes a file symlink when the parent already shows the stack', async () => {
|
||||
const { project, root, fileA } = await setupProject()
|
||||
const shareRoot = await setupShareFolder(project.id, 2)
|
||||
|
||||
const stack = await prisma.asset.create({
|
||||
data: {
|
||||
name: '',
|
||||
type: AssetType.version_stack,
|
||||
parentId: root.id,
|
||||
projectId: project.id,
|
||||
status: 'uploaded',
|
||||
fileCount: 1,
|
||||
sizeByte: 10,
|
||||
},
|
||||
})
|
||||
// Share already contains a symlink to the stack
|
||||
const stackSymlink = await prisma.asset.create({
|
||||
data: {
|
||||
name: '',
|
||||
type: AssetType.symlink,
|
||||
parentId: shareRoot.id,
|
||||
targetId: stack.id,
|
||||
projectId: project.id,
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
// And a symlink to the file being moved into the stack
|
||||
const fileSymlink = await prisma.asset.create({
|
||||
data: {
|
||||
name: fileA.name,
|
||||
type: AssetType.symlink,
|
||||
parentId: shareRoot.id,
|
||||
targetId: fileA.id,
|
||||
projectId: project.id,
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
|
||||
await prisma.$transaction((tx) =>
|
||||
dedupeSymlinksToTarget(tx, { targetIds: [fileA.id], newTargetId: stack.id }),
|
||||
)
|
||||
|
||||
// File symlink deleted, stack symlink untouched
|
||||
expect(await prisma.asset.findUnique({ where: { id: fileSymlink.id } })).toBeNull()
|
||||
const updatedStackSymlink = await prisma.asset.findUnique({ where: { id: stackSymlink.id } })
|
||||
expect(updatedStackSymlink?.targetId).toBe(stack.id)
|
||||
|
||||
// fileCount decremented once (2 -> 1)
|
||||
const updatedShareRoot = await prisma.asset.findUnique({ where: { id: shareRoot.id } })
|
||||
expect(updatedShareRoot?.fileCount).toBe(1)
|
||||
})
|
||||
|
||||
it('repoints symlinks in different share folders without changing fileCounts', async () => {
|
||||
const { project, root, fileA, fileB } = await setupProject()
|
||||
const share1 = await setupShareFolder(project.id, 1)
|
||||
const share2 = await setupShareFolder(project.id, 1)
|
||||
|
||||
const sym1 = await prisma.asset.create({
|
||||
data: {
|
||||
name: fileA.name,
|
||||
type: AssetType.symlink,
|
||||
parentId: share1.id,
|
||||
targetId: fileA.id,
|
||||
projectId: project.id,
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
const sym2 = await prisma.asset.create({
|
||||
data: {
|
||||
name: fileB.name,
|
||||
type: AssetType.symlink,
|
||||
parentId: share2.id,
|
||||
targetId: fileB.id,
|
||||
projectId: project.id,
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
|
||||
const stack = await prisma.asset.create({
|
||||
data: {
|
||||
name: '',
|
||||
type: AssetType.version_stack,
|
||||
parentId: root.id,
|
||||
projectId: project.id,
|
||||
status: 'uploaded',
|
||||
fileCount: 2,
|
||||
sizeByte: 30,
|
||||
},
|
||||
})
|
||||
|
||||
await prisma.$transaction((tx) =>
|
||||
dedupeSymlinksToTarget(tx, { targetIds: [fileA.id, fileB.id], newTargetId: stack.id }),
|
||||
)
|
||||
|
||||
expect((await prisma.asset.findUnique({ where: { id: sym1.id } }))?.targetId).toBe(stack.id)
|
||||
expect((await prisma.asset.findUnique({ where: { id: sym2.id } }))?.targetId).toBe(stack.id)
|
||||
expect((await prisma.asset.findUnique({ where: { id: share1.id } }))?.fileCount).toBe(1)
|
||||
expect((await prisma.asset.findUnique({ where: { id: share2.id } }))?.fileCount).toBe(1)
|
||||
})
|
||||
|
||||
it('sets the provided name on repointed symlinks', async () => {
|
||||
const { project, root, fileA } = await setupProject()
|
||||
const shareRoot = await setupShareFolder(project.id, 1)
|
||||
|
||||
const sym = await prisma.asset.create({
|
||||
data: {
|
||||
name: fileA.name,
|
||||
type: AssetType.symlink,
|
||||
parentId: shareRoot.id,
|
||||
targetId: fileA.id,
|
||||
projectId: project.id,
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
|
||||
const stack = await prisma.asset.create({
|
||||
data: {
|
||||
name: '',
|
||||
type: AssetType.version_stack,
|
||||
parentId: root.id,
|
||||
projectId: project.id,
|
||||
status: 'uploaded',
|
||||
fileCount: 1,
|
||||
sizeByte: 10,
|
||||
},
|
||||
})
|
||||
|
||||
await prisma.$transaction((tx) =>
|
||||
dedupeSymlinksToTarget(tx, {
|
||||
targetIds: [fileA.id],
|
||||
newTargetId: stack.id,
|
||||
name: '',
|
||||
}),
|
||||
)
|
||||
|
||||
const updatedSym = await prisma.asset.findUnique({ where: { id: sym.id } })
|
||||
expect(updatedSym?.targetId).toBe(stack.id)
|
||||
expect(updatedSym?.name).toBe('')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,58 @@
|
||||
import { Prisma } from '@shumai/db'
|
||||
import { AssetType } from '@shumai/db'
|
||||
|
||||
interface DedupeSymlinksParams {
|
||||
/** Files being merged into a version stack — symlinks pointing at them are the candidates. */
|
||||
targetIds: string[]
|
||||
/** The version stack the files are moving into. */
|
||||
newTargetId: string
|
||||
/** Optional name to set on repointed symlinks ('' for stacks — display name comes from the latest version). */
|
||||
name?: string
|
||||
}
|
||||
|
||||
/**
|
||||
* When files are merged into a version stack, symlinks in share folders that
|
||||
* pointed at those files must either be repointed to the stack (one per parent)
|
||||
* or deleted as duplicates. Per parent folder: if the parent already shows the
|
||||
* stack (either before this call or after the first repoint), delete the
|
||||
* incoming symlink and decrement the parent's fileCount; otherwise repoint the
|
||||
* first symlink to the stack.
|
||||
*/
|
||||
export async function dedupeSymlinksToTarget(
|
||||
tx: Prisma.TransactionClient,
|
||||
{ targetIds, newTargetId, name }: DedupeSymlinksParams,
|
||||
): Promise<void> {
|
||||
const existingSymlinks = await tx.asset.findMany({
|
||||
where: { targetId: { in: targetIds }, type: AssetType.symlink },
|
||||
})
|
||||
if (existingSymlinks.length === 0) return
|
||||
|
||||
// Parents that already display the stack (or will after the first repoint).
|
||||
const stackSymlinkParents = new Set(
|
||||
(
|
||||
await tx.asset.findMany({
|
||||
where: { targetId: newTargetId, type: AssetType.symlink },
|
||||
select: { parentId: true },
|
||||
})
|
||||
)
|
||||
.map((s) => s.parentId)
|
||||
.filter((id): id is string => !!id),
|
||||
)
|
||||
|
||||
for (const symlink of existingSymlinks) {
|
||||
if (!symlink.parentId) continue
|
||||
if (stackSymlinkParents.has(symlink.parentId)) {
|
||||
await tx.asset.delete({ where: { id: symlink.id } })
|
||||
await tx.asset.update({
|
||||
where: { id: symlink.parentId },
|
||||
data: { fileCount: { decrement: 1 } },
|
||||
})
|
||||
} else {
|
||||
stackSymlinkParents.add(symlink.parentId)
|
||||
await tx.asset.update({
|
||||
where: { id: symlink.id },
|
||||
data: { targetId: newTargetId, ...(name !== undefined ? { name } : {}) },
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ import { describe, it, expect, beforeEach } from 'vitest'
|
||||
import { prisma } from '@shumai/db'
|
||||
import { setupTestDbHooks } from '@shumai/db/test'
|
||||
import { VersionStackService } from './versionStack'
|
||||
import { assetService } from '../asset/asset'
|
||||
import { AssetType } from '@shumai/db'
|
||||
|
||||
describe('VersionStackService', () => {
|
||||
@@ -207,4 +208,299 @@ describe('VersionStackService', () => {
|
||||
expect(children.map((c) => c.id).includes(file3.id)).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('removeVersionFromStack', () => {
|
||||
it('removes a version from a 3-version stack and turns it into a standalone file adjacent to the stack', async () => {
|
||||
const team = await prisma.team.create({ data: { name: 'Test Team' } })
|
||||
const proj = 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 root = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'root',
|
||||
type: AssetType.folder,
|
||||
status: 'uploaded',
|
||||
projectId: proj.id,
|
||||
creatorId: user.id,
|
||||
},
|
||||
})
|
||||
const parent = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Parent Folder',
|
||||
type: AssetType.folder,
|
||||
projectId: proj.id,
|
||||
parentId: root.id,
|
||||
creatorId: user.id,
|
||||
status: 'uploaded',
|
||||
fileCount: 3,
|
||||
},
|
||||
})
|
||||
|
||||
const file1 = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'file1.txt',
|
||||
type: AssetType.file,
|
||||
parentId: parent.id,
|
||||
projectId: proj.id,
|
||||
creatorId: user.id,
|
||||
status: 'uploaded',
|
||||
sizeByte: 100,
|
||||
sortIndex: 'a0',
|
||||
},
|
||||
})
|
||||
const file2 = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'file2.txt',
|
||||
type: AssetType.file,
|
||||
parentId: parent.id,
|
||||
projectId: proj.id,
|
||||
creatorId: user.id,
|
||||
status: 'uploaded',
|
||||
sizeByte: 200,
|
||||
},
|
||||
})
|
||||
const file3 = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'file3.txt',
|
||||
type: AssetType.file,
|
||||
parentId: parent.id,
|
||||
projectId: proj.id,
|
||||
creatorId: user.id,
|
||||
status: 'uploaded',
|
||||
sizeByte: 300,
|
||||
},
|
||||
})
|
||||
|
||||
const stack = await service.createVersionStack({
|
||||
fileIds: [file1.id, file2.id, file3.id],
|
||||
projectId: proj.id,
|
||||
creatorId: user.id,
|
||||
})
|
||||
|
||||
// Stack should have 3 versions
|
||||
expect(stack.fileCount).toBe(3)
|
||||
expect(Number(stack.sizeByte)).toBe(600)
|
||||
|
||||
// Parent fileCount should be 1 (the stack)
|
||||
const parentBefore = await prisma.asset.findUnique({ where: { id: parent.id } })
|
||||
expect(parentBefore?.fileCount).toBe(1)
|
||||
|
||||
// Remove file2 from stack
|
||||
await service.removeVersionFromStack({
|
||||
stackId: stack.id,
|
||||
fileId: file2.id,
|
||||
})
|
||||
|
||||
// File2 should now be in parent folder
|
||||
const updatedFile2 = await prisma.asset.findUnique({ where: { id: file2.id } })
|
||||
expect(updatedFile2?.parentId).toBe(parent.id)
|
||||
expect(updatedFile2?.sortIndex).toBeDefined()
|
||||
expect(updatedFile2!.sortIndex! > stack.sortIndex!).toBe(true)
|
||||
|
||||
// Parent fileCount should be 2 (stack + file2)
|
||||
const updatedParent = await prisma.asset.findUnique({ where: { id: parent.id } })
|
||||
expect(updatedParent?.fileCount).toBe(2)
|
||||
|
||||
// Stack should still exist with 2 versions (file1 and file3)
|
||||
const updatedStack = await prisma.asset.findUnique({ where: { id: stack.id } })
|
||||
expect(updatedStack).toBeDefined()
|
||||
expect(updatedStack?.fileCount).toBe(2)
|
||||
expect(Number(updatedStack?.sizeByte)).toBe(400)
|
||||
|
||||
const remainingChildren = await prisma.asset.findMany({
|
||||
where: { parentId: stack.id },
|
||||
})
|
||||
expect(remainingChildren).toHaveLength(2)
|
||||
expect(remainingChildren.map((c) => c.id).sort()).toEqual([file1.id, file3.id].sort())
|
||||
})
|
||||
|
||||
it('dissolves the version stack when removing down to 1 remaining version', async () => {
|
||||
const team = await prisma.team.create({ data: { name: 'Test Team' } })
|
||||
const proj = 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 root = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'root',
|
||||
type: AssetType.folder,
|
||||
status: 'uploaded',
|
||||
projectId: proj.id,
|
||||
creatorId: user.id,
|
||||
},
|
||||
})
|
||||
const parent = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Parent Folder',
|
||||
type: AssetType.folder,
|
||||
projectId: proj.id,
|
||||
parentId: root.id,
|
||||
creatorId: user.id,
|
||||
status: 'uploaded',
|
||||
fileCount: 2,
|
||||
},
|
||||
})
|
||||
|
||||
const file1 = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'file1.txt',
|
||||
type: AssetType.file,
|
||||
parentId: parent.id,
|
||||
projectId: proj.id,
|
||||
creatorId: user.id,
|
||||
status: 'uploaded',
|
||||
sizeByte: 100,
|
||||
sortIndex: 'a0',
|
||||
},
|
||||
})
|
||||
const file2 = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'file2.txt',
|
||||
type: AssetType.file,
|
||||
parentId: parent.id,
|
||||
projectId: proj.id,
|
||||
creatorId: user.id,
|
||||
status: 'uploaded',
|
||||
sizeByte: 200,
|
||||
},
|
||||
})
|
||||
|
||||
const stack = await service.createVersionStack({
|
||||
fileIds: [file1.id, file2.id],
|
||||
projectId: proj.id,
|
||||
creatorId: user.id,
|
||||
})
|
||||
|
||||
// Remove file2 from stack
|
||||
await service.removeVersionFromStack({
|
||||
stackId: stack.id,
|
||||
fileId: file2.id,
|
||||
})
|
||||
|
||||
// Stack asset should be deleted
|
||||
const deletedStack = await prisma.asset.findUnique({ where: { id: stack.id } })
|
||||
expect(deletedStack).toBeNull()
|
||||
|
||||
// File1 should now be in parent folder and inherited stack's sortIndex
|
||||
const updatedFile1 = await prisma.asset.findUnique({ where: { id: file1.id } })
|
||||
expect(updatedFile1?.parentId).toBe(parent.id)
|
||||
expect(updatedFile1?.sortIndex).toBe('a0')
|
||||
|
||||
// File2 should also be in parent folder
|
||||
const updatedFile2 = await prisma.asset.findUnique({ where: { id: file2.id } })
|
||||
expect(updatedFile2?.parentId).toBe(parent.id)
|
||||
|
||||
// Parent fileCount should be 2
|
||||
const updatedParent = await prisma.asset.findUnique({ where: { id: parent.id } })
|
||||
expect(updatedParent?.fileCount).toBe(2)
|
||||
})
|
||||
|
||||
it('updates share link symlinks when normal asset becomes version stack and when stack dissolves back to normal asset', async () => {
|
||||
const team = await prisma.team.create({ data: { name: 'Test Team' } })
|
||||
const proj = 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 root = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'root',
|
||||
type: AssetType.folder,
|
||||
status: 'uploaded',
|
||||
projectId: proj.id,
|
||||
creatorId: user.id,
|
||||
},
|
||||
})
|
||||
const shareRoot = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'share-root',
|
||||
type: AssetType.share_root,
|
||||
status: 'uploaded',
|
||||
projectId: proj.id,
|
||||
creatorId: user.id,
|
||||
fileCount: 0,
|
||||
},
|
||||
})
|
||||
const shareLink = await prisma.shareLink.create({
|
||||
data: {
|
||||
name: 'My Share',
|
||||
projectId: proj.id,
|
||||
rootFolderId: shareRoot.id,
|
||||
creatorId: user.id,
|
||||
},
|
||||
})
|
||||
|
||||
const file1 = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'file1.txt',
|
||||
type: AssetType.file,
|
||||
parentId: root.id,
|
||||
projectId: proj.id,
|
||||
creatorId: user.id,
|
||||
status: 'uploaded',
|
||||
sizeByte: 100,
|
||||
},
|
||||
})
|
||||
const file2 = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'file2.txt',
|
||||
type: AssetType.file,
|
||||
parentId: root.id,
|
||||
projectId: proj.id,
|
||||
creatorId: user.id,
|
||||
status: 'uploaded',
|
||||
sizeByte: 200,
|
||||
},
|
||||
})
|
||||
|
||||
// Add file1 to share link (create symlink)
|
||||
const symlink = await prisma.asset.create({
|
||||
data: {
|
||||
name: file1.name,
|
||||
type: AssetType.symlink,
|
||||
parentId: shareLink.rootFolderId,
|
||||
targetId: file1.id,
|
||||
projectId: proj.id,
|
||||
creatorId: user.id,
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
|
||||
// 1. Group file1 and file2 into a version stack
|
||||
const stack = await service.createVersionStack({
|
||||
fileIds: [file1.id, file2.id],
|
||||
projectId: proj.id,
|
||||
creatorId: user.id,
|
||||
})
|
||||
|
||||
// Symlink in share link should now point to stack.id
|
||||
const symlinkAfterStack = await prisma.asset.findUnique({ where: { id: symlink.id } })
|
||||
expect(symlinkAfterStack?.targetId).toBe(stack.id)
|
||||
|
||||
// 2. Remove file2 from stack -> stack dissolves -> only file1 remains
|
||||
await service.removeVersionFromStack({
|
||||
stackId: stack.id,
|
||||
fileId: file2.id,
|
||||
})
|
||||
|
||||
// Symlink in share link should now point directly to file1.id and have file1's name
|
||||
const symlinkAfterDissolve = await prisma.asset.findUnique({ where: { id: symlink.id } })
|
||||
expect(symlinkAfterDissolve?.targetId).toBe(file1.id)
|
||||
expect(symlinkAfterDissolve?.name).toBe(file1.name)
|
||||
|
||||
// Listing share link files returns file1 with correct name
|
||||
const shareFiles = await assetService.listChildren({
|
||||
assetId: shareLink.rootFolderId,
|
||||
assetType: AssetType.file,
|
||||
})
|
||||
expect(shareFiles.data).toHaveLength(1)
|
||||
expect(shareFiles.data[0].name).toBe(file1.name)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
import { generateKeyBetween } from 'jittered-fractional-indexing'
|
||||
import { prisma } from '@shumai/db'
|
||||
import { type Asset, AssetType } from '@shumai/db'
|
||||
import { ChangeStackFileVersionParams, CreateVersionStackParams } from '@shumai/dtos'
|
||||
import {
|
||||
ChangeStackFileVersionParams,
|
||||
CreateVersionStackParams,
|
||||
RemoveStackVersionParams,
|
||||
} from '@shumai/dtos'
|
||||
import { dedupeSymlinksToTarget } from '../asset/symlink'
|
||||
|
||||
function generateSortIndex(previous?: string | null): string {
|
||||
if (!previous) return generateKeyBetween(null, null)
|
||||
@@ -95,6 +100,9 @@ export class VersionStackService {
|
||||
},
|
||||
})
|
||||
|
||||
// Update existing symlinks pointing to any of the stacked files
|
||||
await dedupeSymlinksToTarget(tx, { targetIds: fileIds, newTargetId: stack.id })
|
||||
|
||||
return stack
|
||||
})
|
||||
}
|
||||
@@ -127,7 +135,7 @@ export class VersionStackService {
|
||||
|
||||
if (beforeId === '-1') {
|
||||
const lastFile = await tx.asset.findFirst({
|
||||
where: { parentId: stackId },
|
||||
where: { parentId: stackId, id: { not: fileId } },
|
||||
orderBy: { sortIndex: 'desc' },
|
||||
})
|
||||
newSortIndex = generateKeyBetween(lastFile?.sortIndex || null, null)
|
||||
@@ -145,6 +153,7 @@ export class VersionStackService {
|
||||
const prevFile = await tx.asset.findFirst({
|
||||
where: {
|
||||
parentId: stackId,
|
||||
id: { not: fileId },
|
||||
sortIndex: {
|
||||
lt: beforeFile.sortIndex!,
|
||||
},
|
||||
@@ -168,6 +177,123 @@ export class VersionStackService {
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
async removeVersionFromStack(params: RemoveStackVersionParams): Promise<void> {
|
||||
const { stackId, fileId } = params
|
||||
|
||||
await this.prismaClient.$transaction(async (tx) => {
|
||||
const stack = await tx.asset.findUnique({
|
||||
where: { id: stackId },
|
||||
})
|
||||
if (!stack) {
|
||||
throw new Error('stack not found')
|
||||
}
|
||||
if (stack.type !== AssetType.version_stack) {
|
||||
throw new Error('asset is not a version stack')
|
||||
}
|
||||
if (!stack.parentId) {
|
||||
throw new Error('stack has no parent')
|
||||
}
|
||||
|
||||
const fileToRemove = await tx.asset.findUnique({
|
||||
where: { id: fileId },
|
||||
})
|
||||
if (!fileToRemove) {
|
||||
throw new Error('file not found')
|
||||
}
|
||||
if (fileToRemove.parentId !== stackId) {
|
||||
throw new Error('file is not in the stack')
|
||||
}
|
||||
|
||||
// Find next sibling in parent folder after the stack to insert the removed file adjacent to the stack
|
||||
const nextSibling = await tx.asset.findFirst({
|
||||
where: {
|
||||
parentId: stack.parentId,
|
||||
id: { not: stack.id },
|
||||
sortIndex: {
|
||||
gt: stack.sortIndex || '',
|
||||
},
|
||||
},
|
||||
orderBy: { sortIndex: 'asc' },
|
||||
})
|
||||
|
||||
let newSortIndex: string
|
||||
try {
|
||||
newSortIndex = generateKeyBetween(stack.sortIndex || null, nextSibling?.sortIndex || null)
|
||||
} catch {
|
||||
newSortIndex = generateKeyBetween(null, null)
|
||||
}
|
||||
|
||||
await tx.asset.update({
|
||||
where: { id: fileId },
|
||||
data: {
|
||||
parentId: stack.parentId,
|
||||
sortIndex: newSortIndex,
|
||||
},
|
||||
})
|
||||
|
||||
// Increment parent folder fileCount for the newly standalone file
|
||||
await tx.asset.update({
|
||||
where: { id: stack.parentId },
|
||||
data: {
|
||||
fileCount: { increment: 1 },
|
||||
},
|
||||
})
|
||||
|
||||
// Remaining files in stack
|
||||
const remainingFiles = await tx.asset.findMany({
|
||||
where: { parentId: stackId, isDeleted: false, id: { not: fileId } },
|
||||
orderBy: { sortIndex: 'asc' },
|
||||
})
|
||||
|
||||
if (remainingFiles.length > 1) {
|
||||
let totalSize = 0
|
||||
for (const f of remainingFiles) {
|
||||
totalSize += Number(f.sizeByte)
|
||||
}
|
||||
await tx.asset.update({
|
||||
where: { id: stack.id },
|
||||
data: {
|
||||
fileCount: remainingFiles.length,
|
||||
sizeByte: totalSize,
|
||||
},
|
||||
})
|
||||
} else if (remainingFiles.length === 1) {
|
||||
const lastChild = remainingFiles[0]
|
||||
|
||||
// Update symlinks pointing to stack.id to point to the single remaining child
|
||||
await tx.asset.updateMany({
|
||||
where: { targetId: stack.id, type: AssetType.symlink },
|
||||
data: { targetId: lastChild.id, name: lastChild.name },
|
||||
})
|
||||
|
||||
// Reparent the single remaining child to stack.parentId, keeping stack's sortIndex
|
||||
await tx.asset.update({
|
||||
where: { id: lastChild.id },
|
||||
data: {
|
||||
parentId: stack.parentId,
|
||||
sortIndex: stack.sortIndex,
|
||||
},
|
||||
})
|
||||
|
||||
// Delete the stack asset (parent folder file count is unchanged: -1 stack, +1 remaining child)
|
||||
await tx.asset.delete({
|
||||
where: { id: stack.id },
|
||||
})
|
||||
} else {
|
||||
await tx.asset.deleteMany({
|
||||
where: { targetId: stack.id, type: AssetType.symlink },
|
||||
})
|
||||
await tx.asset.delete({
|
||||
where: { id: stack.id },
|
||||
})
|
||||
await tx.asset.update({
|
||||
where: { id: stack.parentId },
|
||||
data: { fileCount: { decrement: 1 } },
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export const versionStackService = new VersionStackService()
|
||||
|
||||
Reference in New Issue
Block a user