feat(asset): decouple logical assets from physical storage with StorageKey and implement O(1) copy

This commit is contained in:
yiling
2026-05-25 00:52:45 +08:00
parent c5fba746be
commit 5dc3bdb38e
33 changed files with 2203 additions and 273 deletions
+32 -12
View File
@@ -101,10 +101,15 @@ describe('UploadService', () => {
data: {
name: 'video.mp4',
type: AssetType.file,
projectId,
parentId,
project: { connect: { id: projectId } },
parent: { connect: { id: parentId } },
status: AssetStatus.uploading,
key: 'test-key',
storageKey: {
connectOrCreate: {
where: { key: 'test-key' },
create: { key: 'test-key' },
},
},
mediaType: 'video/mp4',
},
})
@@ -134,10 +139,15 @@ describe('UploadService', () => {
data: {
name: 'image.png',
type: AssetType.file,
projectId,
parentId,
project: { connect: { id: projectId } },
parent: { connect: { id: parentId } },
status: AssetStatus.uploading,
key: 'test-key',
storageKey: {
connectOrCreate: {
where: { key: 'test-key' },
create: { key: 'test-key' },
},
},
mediaType: 'image/png',
},
})
@@ -162,10 +172,15 @@ describe('UploadService', () => {
data: {
name: 'file.txt',
type: AssetType.file,
projectId,
parentId,
project: { connect: { id: projectId } },
parent: { connect: { id: parentId } },
status: AssetStatus.uploading,
key: 'test-key',
storageKey: {
connectOrCreate: {
where: { key: 'test-key' },
create: { key: 'test-key' },
},
},
mediaType: 'text/plain',
},
})
@@ -259,10 +274,15 @@ describe('UploadService', () => {
data: {
name: 'v2.txt',
type: AssetType.file,
projectId,
parentId: stack.id,
project: { connect: { id: projectId } },
parent: { connect: { id: stack.id } },
status: AssetStatus.uploading,
key: 'v2-key',
storageKey: {
connectOrCreate: {
where: { key: 'v2-key' },
create: { key: 'v2-key' },
},
},
sizeByte: 2000,
},
})
+12 -8
View File
@@ -127,15 +127,15 @@ export class UploadService {
data: {
name: file.name,
type: assetType,
key,
storageKey: key ? { create: { key } } : undefined,
sortIndex: newSortIndex,
mediaType: file.mediaType,
status: AssetStatus.uploading,
sizeByte: file.size,
creatorId: userId,
parentId,
projectId,
taskId,
creator: userId ? { connect: { id: userId } } : undefined,
parent: parentId ? { connect: { id: parentId } } : undefined,
project: { connect: { id: projectId } },
task: { connect: { id: taskId } },
},
})
createdIds.push(newAsset.id)
@@ -168,11 +168,15 @@ export class UploadService {
): Promise<void> {
const asset = await this.prismaClient.asset.findUnique({
where: { id: req.fileId },
include: { project: { include: { team: true } } },
include: {
storageKey: true,
project: { include: { team: true } },
},
})
if (!asset) throw new Error('Asset not found')
if (!asset.project) throw new Error('Project not found for asset')
if (!asset.key) throw new Error('Asset has no key')
const key = asset.storageKey?.key
if (!key) throw new Error('Asset has no key')
if (req.errorMessage) {
await this.prismaClient.asset.delete({ where: { id: asset.id } })
@@ -192,7 +196,7 @@ export class UploadService {
return
}
const size = await s3Service.getObjectSize(process.env.S3_BUCKET || 'shumai', asset.key)
const size = await s3Service.getObjectSize(process.env.S3_BUCKET || 'shumai', key)
const project = asset.project