fix(asset): delete entire S3 directory for composite assets to clean up transcodes

This commit is contained in:
yiling
2026-05-24 09:18:58 +08:00
parent 823d915613
commit b6c2c59801
4 changed files with 67 additions and 2 deletions
+35
View File
@@ -10,6 +10,7 @@ vi.mock('@/services/s3/s3', () => ({
presign: vi.fn().mockResolvedValue('http://mock-s3-url'),
putObject: vi.fn().mockResolvedValue(undefined),
deleteObject: vi.fn().mockResolvedValue(undefined),
deletePrefix: vi.fn().mockResolvedValue(undefined),
},
}))
@@ -1228,5 +1229,39 @@ describe('AssetService', () => {
const remainingChildren = await prisma.asset.count({ where: { parentId: folderA.id } })
expect(remainingChildren).toBe(0)
})
it('should delete the entire directory prefix for assets with complex keys (e.g., file/ULID/raw)', async () => {
const { project } = await setupBasicAssets()
const { s3Service } = await import('@/services/s3/s3')
// Create an asset with a complex key
const complexFile = await prisma.asset.create({
data: {
name: 'Complex File',
type: AssetType.file,
status: AssetStatus.pending_purge,
isDeleted: true,
projectId: project.id,
key: 'file/01KSBRJVY3DPK111S2MECFKDQ4/raw',
},
})
// Trigger the purge job
// eslint-disable-next-line @typescript-eslint/no-explicit-any
await (assetService as any).purgePendingAssets()
// Verify deletePrefix was called with the correct prefix instead of just deleteObject
expect(s3Service.deletePrefix).toHaveBeenCalledWith(
expect.any(String),
'file/01KSBRJVY3DPK111S2MECFKDQ4/',
)
expect(s3Service.deleteObject).not.toHaveBeenCalledWith(
expect.any(String),
'file/01KSBRJVY3DPK111S2MECFKDQ4/raw',
)
// Verify record is gone
expect(await prisma.asset.findUnique({ where: { id: complexFile.id } })).toBeNull()
})
})
})