mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-02 22:14:50 +00:00
refactor(core): fix fileCount and createAssetsRecursively transaction
This commit is contained in:
@@ -559,6 +559,63 @@ describe('AssetService', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('fileCount verification', () => {
|
||||||
|
it('createAsset should increment parent fileCount', async () => {
|
||||||
|
const { user, assets, project } = await setupBasicAssets()
|
||||||
|
|
||||||
|
const parent = assets.folderA
|
||||||
|
const initialCount = parent.fileCount // should be 2 from setup (fileA1, fileA2)
|
||||||
|
|
||||||
|
// Create a new folder inside folderA
|
||||||
|
await assetService.createAsset({
|
||||||
|
name: 'newFolder',
|
||||||
|
type: 'folder',
|
||||||
|
parentId: parent.id,
|
||||||
|
projectId: project.id,
|
||||||
|
creatorId: user.id,
|
||||||
|
})
|
||||||
|
|
||||||
|
const updatedParent = await prisma.asset.findUnique({ where: { id: parent.id } })
|
||||||
|
expect(updatedParent?.fileCount).toBe(initialCount + 1)
|
||||||
|
|
||||||
|
// Create a new file inside folderA
|
||||||
|
await assetService.createAsset({
|
||||||
|
name: 'newFile',
|
||||||
|
type: 'file',
|
||||||
|
parentId: parent.id,
|
||||||
|
projectId: project.id,
|
||||||
|
creatorId: user.id,
|
||||||
|
})
|
||||||
|
|
||||||
|
const updatedParent2 = await prisma.asset.findUnique({ where: { id: parent.id } })
|
||||||
|
expect(updatedParent2?.fileCount).toBe(initialCount + 2)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('reparentAssets should update fileCount when moving folders', async () => {
|
||||||
|
const { user, assets } = await setupBasicAssets()
|
||||||
|
|
||||||
|
const sourceParent = assets.root
|
||||||
|
const targetParent = assets.folderB
|
||||||
|
const folderToMove = assets.folderA
|
||||||
|
|
||||||
|
// folderA is a child of root.
|
||||||
|
// root children: folderA, folderB, fileRoot1. count=3.
|
||||||
|
// folderB children: stackB, fileB2. count=2.
|
||||||
|
|
||||||
|
await assetService.reparentAssets({
|
||||||
|
assetIds: [folderToMove.id],
|
||||||
|
newParentId: targetParent.id,
|
||||||
|
creatorId: user.id,
|
||||||
|
})
|
||||||
|
|
||||||
|
const updatedSourceParent = await prisma.asset.findUnique({ where: { id: sourceParent.id } })
|
||||||
|
expect(updatedSourceParent?.fileCount).toBe(2)
|
||||||
|
|
||||||
|
const updatedTargetParent = await prisma.asset.findUnique({ where: { id: targetParent.id } })
|
||||||
|
expect(updatedTargetParent?.fileCount).toBe(3)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
it('handles reparenting - Version Stack: dissolve with 1 remaining', async () => {
|
it('handles reparenting - Version Stack: dissolve with 1 remaining', async () => {
|
||||||
const { user, assets } = await setupBasicAssets()
|
const { user, assets } = await setupBasicAssets()
|
||||||
await assetService.reparentAssets({
|
await assetService.reparentAssets({
|
||||||
|
|||||||
@@ -172,8 +172,8 @@ export class AssetService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async reparentAssets(req: ReparentAssetsRequest): Promise<void> {
|
async reparentAssets(req: ReparentAssetsRequest, tx?: Prisma.TransactionClient): Promise<void> {
|
||||||
await this.prismaClient.$transaction(async (tx) => {
|
const runInTx = async (tx: Prisma.TransactionClient) => {
|
||||||
const newParent = await tx.asset.findUnique({
|
const newParent = await tx.asset.findUnique({
|
||||||
where: { id: req.newParentId },
|
where: { id: req.newParentId },
|
||||||
include: { project: { include: { team: true } } },
|
include: { project: { include: { team: true } } },
|
||||||
@@ -269,7 +269,13 @@ export class AssetService {
|
|||||||
if (oldParent) {
|
if (oldParent) {
|
||||||
await this.dissolveStackIfEmpty(tx, oldParent)
|
await this.dissolveStackIfEmpty(tx, oldParent)
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
|
||||||
|
if (tx) {
|
||||||
|
await runInTx(tx)
|
||||||
|
} else {
|
||||||
|
await this.prismaClient.$transaction(runInTx)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async copyAssets(req: CopyAssetsRequest): Promise<void> {
|
async copyAssets(req: CopyAssetsRequest): Promise<void> {
|
||||||
@@ -722,30 +728,41 @@ export class AssetService {
|
|||||||
if (req.contentType) data.mediaType = req.contentType
|
if (req.contentType) data.mediaType = req.contentType
|
||||||
if (req.creatorId) data.creator = { connect: { id: req.creatorId } }
|
if (req.creatorId) data.creator = { connect: { id: req.creatorId } }
|
||||||
|
|
||||||
const asset = await this.prismaClient.asset.create({
|
const asset = await this.prismaClient.$transaction(async (tx) => {
|
||||||
data,
|
const createdAsset = await tx.asset.create({
|
||||||
include: {
|
data,
|
||||||
creator: true,
|
include: {
|
||||||
metadataValues: true,
|
creator: true,
|
||||||
storageKey: true,
|
metadataValues: true,
|
||||||
target: {
|
storageKey: true,
|
||||||
include: {
|
target: {
|
||||||
creator: true,
|
include: {
|
||||||
metadataValues: true,
|
creator: true,
|
||||||
storageKey: true,
|
metadataValues: true,
|
||||||
children: {
|
storageKey: true,
|
||||||
include: { creator: true, metadataValues: true, storageKey: true },
|
children: {
|
||||||
take: 3,
|
include: { creator: true, metadataValues: true, storageKey: true },
|
||||||
orderBy: { sortIndex: 'asc' },
|
take: 3,
|
||||||
|
orderBy: { sortIndex: 'asc' },
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
children: {
|
||||||
|
include: { creator: true, metadataValues: true, storageKey: true },
|
||||||
|
take: 3,
|
||||||
|
orderBy: { sortIndex: 'asc' },
|
||||||
|
},
|
||||||
},
|
},
|
||||||
children: {
|
})
|
||||||
include: { creator: true, metadataValues: true, storageKey: true },
|
|
||||||
take: 3,
|
if (parent) {
|
||||||
orderBy: { sortIndex: 'asc' },
|
await tx.asset.update({
|
||||||
},
|
where: { id: parent.id },
|
||||||
},
|
data: { fileCount: { increment: 1 } },
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return createdAsset
|
||||||
})
|
})
|
||||||
|
|
||||||
const infos = await this.toAssetInfos([asset])
|
const infos = await this.toAssetInfos([asset])
|
||||||
|
|||||||
@@ -93,6 +93,67 @@ describe('UploadService', () => {
|
|||||||
expect(hidden).toBeNull()
|
expect(hidden).toBeNull()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should correctly increment fileCount for parent folders when uploading folders', async () => {
|
||||||
|
const req = {
|
||||||
|
parentId: parentId,
|
||||||
|
files: [
|
||||||
|
{
|
||||||
|
name: 'folder1',
|
||||||
|
id: '1',
|
||||||
|
type: 'folder' as const,
|
||||||
|
size: 0,
|
||||||
|
children: [
|
||||||
|
{
|
||||||
|
name: 'folder2',
|
||||||
|
id: '2',
|
||||||
|
type: 'folder' as const,
|
||||||
|
size: 0,
|
||||||
|
children: [],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'file1.txt',
|
||||||
|
id: '3',
|
||||||
|
type: 'file' as const,
|
||||||
|
size: 100,
|
||||||
|
mediaType: 'text/plain',
|
||||||
|
children: [],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
|
||||||
|
await uploadService.createUploadTask(userId, req)
|
||||||
|
|
||||||
|
// parentId folder should have folder1 as a direct child
|
||||||
|
const parent = await prisma.asset.findUnique({ where: { id: parentId } })
|
||||||
|
expect(parent?.fileCount).toBe(1)
|
||||||
|
|
||||||
|
// folder1 should have folder2 and file1.txt as direct children
|
||||||
|
// folder2 is a folder, so it is counted immediately.
|
||||||
|
// file1.txt is a file, so it is counted after confirmFileUpload.
|
||||||
|
const folder1 = await prisma.asset.findFirst({ where: { name: 'folder1' } })
|
||||||
|
expect(folder1?.parentId).toBe(parentId)
|
||||||
|
expect(folder1?.fileCount).toBe(1)
|
||||||
|
|
||||||
|
const folder2 = await prisma.asset.findFirst({ where: { name: 'folder2' } })
|
||||||
|
expect(folder2?.parentId).toBe(folder1?.id)
|
||||||
|
expect(folder2?.fileCount).toBe(0)
|
||||||
|
|
||||||
|
const file1 = await prisma.asset.findFirst({ where: { name: 'file1.txt' } })
|
||||||
|
expect(file1?.parentId).toBe(folder1?.id)
|
||||||
|
expect(file1?.status).toBe(AssetStatus.uploading)
|
||||||
|
|
||||||
|
// Confirm upload for file1.txt
|
||||||
|
const task = await prisma.task.findFirst({ where: { total: 1 } })
|
||||||
|
await uploadService.confirmFileUpload(userId, task!.id, {
|
||||||
|
fileId: file1!.id,
|
||||||
|
})
|
||||||
|
|
||||||
|
const folder1After = await prisma.asset.findUnique({ where: { id: folder1!.id } })
|
||||||
|
expect(folder1After?.fileCount).toBe(2)
|
||||||
|
})
|
||||||
|
|
||||||
it('should confirm file upload and create transcode tasks for video', async () => {
|
it('should confirm file upload and create transcode tasks for video', async () => {
|
||||||
const task = await prisma.task.create({
|
const task = await prisma.task.create({
|
||||||
data: { creatorId: userId, total: 1, uploaded: 0, type: 'upload' },
|
data: { creatorId: userId, total: 1, uploaded: 0, type: 'upload' },
|
||||||
|
|||||||
@@ -2,21 +2,21 @@ import { assetService } from '@shumai/core/src/asset/asset'
|
|||||||
import { PaginatedData, PaginationParams, paginateQuery } from '@shumai/core/src/pagination'
|
import { PaginatedData, PaginationParams, paginateQuery } from '@shumai/core/src/pagination'
|
||||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||||
import {
|
import {
|
||||||
AssetStatus,
|
AssetStatus,
|
||||||
AssetType,
|
AssetType,
|
||||||
Prisma,
|
Prisma,
|
||||||
TaskStatus,
|
TaskStatus,
|
||||||
WorkflowTaskStatus,
|
WorkflowTaskStatus,
|
||||||
WorkflowTaskType,
|
WorkflowTaskType,
|
||||||
prisma,
|
prisma,
|
||||||
} from '@shumai/db'
|
} from '@shumai/db'
|
||||||
import {
|
import {
|
||||||
ConfirmFileUploadRequest,
|
ConfirmFileUploadRequest,
|
||||||
CreateUploadTaskRequest,
|
CreateUploadTaskRequest,
|
||||||
CreateUploadTaskResponse,
|
CreateUploadTaskResponse,
|
||||||
FileNode,
|
FileNode,
|
||||||
PresignedUrl,
|
PresignedUrl,
|
||||||
TaskInfo,
|
TaskInfo,
|
||||||
} from '@shumai/dtos'
|
} from '@shumai/dtos'
|
||||||
import { ImageTranscoder, VideoTranscoder } from '@shumai/transcode'
|
import { ImageTranscoder, VideoTranscoder } from '@shumai/transcode'
|
||||||
import { generateKeyBetween } from 'jittered-fractional-indexing'
|
import { generateKeyBetween } from 'jittered-fractional-indexing'
|
||||||
@@ -67,24 +67,32 @@ export class UploadService {
|
|||||||
const isParentFile = parentAsset.type === AssetType.file
|
const isParentFile = parentAsset.type === AssetType.file
|
||||||
const targetParentId = isParentFile ? parentAsset.parentId! : parentAsset.id
|
const targetParentId = isParentFile ? parentAsset.parentId! : parentAsset.id
|
||||||
|
|
||||||
const createdAssetIds = await this.createAssetsRecursively(
|
await this.prismaClient.$transaction(async (tx) => {
|
||||||
userId,
|
const ids = await this.createAssetsRecursively(
|
||||||
task.id,
|
tx,
|
||||||
parentAsset.projectId,
|
userId,
|
||||||
targetParentId,
|
task.id,
|
||||||
req.files,
|
parentAsset.projectId!,
|
||||||
presignedUrls,
|
targetParentId,
|
||||||
)
|
req.files,
|
||||||
|
presignedUrls,
|
||||||
|
)
|
||||||
|
|
||||||
if (isParentFile && createdAssetIds.length > 0) {
|
if (isParentFile && ids.length > 0) {
|
||||||
for (const assetId of createdAssetIds) {
|
for (const assetId of ids) {
|
||||||
await assetService.reparentAssets({
|
await assetService.reparentAssets(
|
||||||
assetIds: [assetId],
|
{
|
||||||
newParentId: parentAsset.id,
|
assetIds: [assetId],
|
||||||
creatorId: userId,
|
newParentId: parentAsset.id,
|
||||||
})
|
creatorId: userId,
|
||||||
|
},
|
||||||
|
tx,
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
return ids
|
||||||
|
})
|
||||||
|
|
||||||
return {
|
return {
|
||||||
taskId: task.id,
|
taskId: task.id,
|
||||||
@@ -93,6 +101,7 @@ export class UploadService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async createAssetsRecursively(
|
private async createAssetsRecursively(
|
||||||
|
tx: Prisma.TransactionClient,
|
||||||
userId: string,
|
userId: string,
|
||||||
taskId: string,
|
taskId: string,
|
||||||
projectId: string,
|
projectId: string,
|
||||||
@@ -100,7 +109,7 @@ export class UploadService {
|
|||||||
files: FileNode[],
|
files: FileNode[],
|
||||||
presignedUrls: PresignedUrl[],
|
presignedUrls: PresignedUrl[],
|
||||||
): Promise<string[]> {
|
): Promise<string[]> {
|
||||||
const firstFile = await this.prismaClient.asset.findFirst({
|
const firstFile = await tx.asset.findFirst({
|
||||||
where: { parentId },
|
where: { parentId },
|
||||||
orderBy: { sortIndex: 'asc' },
|
orderBy: { sortIndex: 'asc' },
|
||||||
})
|
})
|
||||||
@@ -119,14 +128,14 @@ export class UploadService {
|
|||||||
key = `files/${ulid()}/raw`
|
key = `files/${ulid()}/raw`
|
||||||
}
|
}
|
||||||
|
|
||||||
const newAsset = await this.prismaClient.asset.create({
|
const newAsset = await tx.asset.create({
|
||||||
data: {
|
data: {
|
||||||
name: file.name,
|
name: file.name,
|
||||||
type: assetType,
|
type: assetType,
|
||||||
storageKey: key ? { create: { key } } : undefined,
|
storageKey: key ? { create: { key } } : undefined,
|
||||||
sortIndex: newSortIndex,
|
sortIndex: newSortIndex,
|
||||||
mediaType: file.mediaType,
|
mediaType: file.mediaType,
|
||||||
status: AssetStatus.uploading,
|
status: assetType === AssetType.folder ? AssetStatus.uploaded : AssetStatus.uploading,
|
||||||
sizeByte: file.size,
|
sizeByte: file.size,
|
||||||
creator: userId ? { connect: { id: userId } } : undefined,
|
creator: userId ? { connect: { id: userId } } : undefined,
|
||||||
parent: parentId ? { connect: { id: parentId } } : undefined,
|
parent: parentId ? { connect: { id: parentId } } : undefined,
|
||||||
@@ -136,8 +145,16 @@ export class UploadService {
|
|||||||
})
|
})
|
||||||
createdIds.push(newAsset.id)
|
createdIds.push(newAsset.id)
|
||||||
|
|
||||||
|
if (assetType === AssetType.folder && parentId) {
|
||||||
|
await tx.asset.update({
|
||||||
|
where: { id: parentId },
|
||||||
|
data: { fileCount: { increment: 1 } },
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
if (file.children && file.children.length > 0) {
|
if (file.children && file.children.length > 0) {
|
||||||
await this.createAssetsRecursively(
|
await this.createAssetsRecursively(
|
||||||
|
tx,
|
||||||
userId,
|
userId,
|
||||||
taskId,
|
taskId,
|
||||||
projectId,
|
projectId,
|
||||||
@@ -212,9 +229,8 @@ export class UploadService {
|
|||||||
where: { id: asset.parentId },
|
where: { id: asset.parentId },
|
||||||
data: { fileCount: { increment: 1 } },
|
data: { fileCount: { increment: 1 } },
|
||||||
})
|
})
|
||||||
// The transaction context is compatible with the asset service's requirements
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
await assetService.updateAncestorsSize(tx, asset.parentId, updatedAsset.sizeByte)
|
||||||
await assetService.updateAncestorsSize(tx as any, asset.parentId, updatedAsset.sizeByte)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update task
|
// Update task
|
||||||
|
|||||||
Reference in New Issue
Block a user