mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-01 13:34:33 +00:00
feat(upload): allow canceling in-progress upload tasks from sidebar (#511)
* feat(upload): allow canceling in-progress upload tasks from sidebar * fix(upload): resolve concurrent confirmation race and store counter issues on cancellation * fix(webui): immediately remove cancelled upload files from file list view
This commit is contained in:
@@ -797,6 +797,7 @@ describe('UploadService', () => {
|
||||
expect(result.data).toHaveLength(1)
|
||||
expect(result.data[0].id).toBe(task.id)
|
||||
expect(result.data[0].name).toBe('test-upload-task')
|
||||
expect(result.data[0].status).toBe(TaskStatus.pending)
|
||||
})
|
||||
|
||||
it('should correct the mediaType for .wma files that browser incorrectly reports as video', async () => {
|
||||
@@ -1120,4 +1121,262 @@ describe('UploadService', () => {
|
||||
expect(deletedAsset).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe('cancelUploadTask', () => {
|
||||
it('cancels uploading assets, leaves uploaded assets and folders intact, and marks task failed', async () => {
|
||||
const task = await prisma.task.create({
|
||||
data: {
|
||||
creatorId: userId,
|
||||
type: 'upload',
|
||||
name: 'multi-file-task',
|
||||
total: 3,
|
||||
uploaded: 1,
|
||||
status: TaskStatus.pending,
|
||||
},
|
||||
})
|
||||
|
||||
// 1. A folder created by the task
|
||||
const folder = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'subfolder',
|
||||
type: AssetType.folder,
|
||||
projectId,
|
||||
parentId,
|
||||
taskId: task.id,
|
||||
status: AssetStatus.uploaded,
|
||||
},
|
||||
})
|
||||
|
||||
// 2. An asset already finished uploading
|
||||
const finishedKey = await prisma.storageKey.create({ data: { key: 'files/test/done.mp4' } })
|
||||
const finishedAsset = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'done.mp4',
|
||||
type: AssetType.file,
|
||||
projectId,
|
||||
parentId: folder.id,
|
||||
storageKeyId: finishedKey.id,
|
||||
taskId: task.id,
|
||||
status: AssetStatus.uploaded,
|
||||
},
|
||||
})
|
||||
|
||||
// 3. An incomplete multipart asset
|
||||
const multipartKey = await prisma.storageKey.create({
|
||||
data: { key: 'files/test/part.mp4' },
|
||||
})
|
||||
const multipartAsset = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'part.mp4',
|
||||
type: AssetType.file,
|
||||
projectId,
|
||||
parentId: folder.id,
|
||||
storageKeyId: multipartKey.id,
|
||||
taskId: task.id,
|
||||
uploadId: 'upload-multipart-cancel',
|
||||
status: AssetStatus.uploading,
|
||||
},
|
||||
})
|
||||
|
||||
// 4. An incomplete single PUT asset
|
||||
const singleKey = await prisma.storageKey.create({
|
||||
data: { key: 'files/test/single.mp4' },
|
||||
})
|
||||
const singleAsset = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'single.mp4',
|
||||
type: AssetType.file,
|
||||
projectId,
|
||||
parentId: folder.id,
|
||||
storageKeyId: singleKey.id,
|
||||
taskId: task.id,
|
||||
status: AssetStatus.uploading,
|
||||
},
|
||||
})
|
||||
|
||||
const res = await uploadService.cancelUploadTask(teamId, userId, task.id)
|
||||
expect(res).toEqual({ success: true })
|
||||
|
||||
// Incomplete assets deleted
|
||||
expect(await prisma.asset.findUnique({ where: { id: multipartAsset.id } })).toBeNull()
|
||||
expect(await prisma.storageKey.findUnique({ where: { id: multipartKey.id } })).toBeNull()
|
||||
expect(await prisma.asset.findUnique({ where: { id: singleAsset.id } })).toBeNull()
|
||||
expect(await prisma.storageKey.findUnique({ where: { id: singleKey.id } })).toBeNull()
|
||||
|
||||
// S3 cleanup called
|
||||
expect(s3Service.abortMultipartUpload).toHaveBeenCalledWith(
|
||||
expect.anything(),
|
||||
'files/test/part.mp4',
|
||||
'upload-multipart-cancel',
|
||||
)
|
||||
expect(s3Service.deleteObject).toHaveBeenCalledWith(
|
||||
expect.anything(),
|
||||
'files/test/single.mp4',
|
||||
)
|
||||
|
||||
// Completed asset and folder remain intact
|
||||
expect(await prisma.asset.findUnique({ where: { id: finishedAsset.id } })).not.toBeNull()
|
||||
expect(await prisma.storageKey.findUnique({ where: { id: finishedKey.id } })).not.toBeNull()
|
||||
expect(await prisma.asset.findUnique({ where: { id: folder.id } })).not.toBeNull()
|
||||
|
||||
// Task status updated to failed
|
||||
const updatedTask = await prisma.task.findUnique({ where: { id: task.id } })
|
||||
expect(updatedTask?.status).toBe(TaskStatus.failed)
|
||||
expect(updatedTask?.uploaded).toBe(1)
|
||||
})
|
||||
|
||||
it('rejects cancellation if the caller is not the task creator', async () => {
|
||||
const otherUser = await prisma.user.create({
|
||||
data: {
|
||||
email: 'other@example.com',
|
||||
name: 'Other User',
|
||||
},
|
||||
})
|
||||
|
||||
const task = await prisma.task.create({
|
||||
data: {
|
||||
creatorId: otherUser.id,
|
||||
type: 'upload',
|
||||
name: 'other-task',
|
||||
total: 1,
|
||||
status: TaskStatus.pending,
|
||||
},
|
||||
})
|
||||
|
||||
await expect(uploadService.cancelUploadTask(teamId, userId, task.id)).rejects.toThrow(
|
||||
'Unauthorized to cancel this upload task',
|
||||
)
|
||||
})
|
||||
|
||||
it('throws when task does not exist', async () => {
|
||||
await expect(
|
||||
uploadService.cancelUploadTask(teamId, userId, 'non-existent-task'),
|
||||
).rejects.toThrow('Upload task not found')
|
||||
})
|
||||
|
||||
it('throws when task assets belong to a different team', async () => {
|
||||
const otherTeam = await prisma.team.create({
|
||||
data: { name: 'Other Team' },
|
||||
})
|
||||
const otherProject = await prisma.project.create({
|
||||
data: { name: 'Other Project', teamId: otherTeam.id },
|
||||
})
|
||||
|
||||
const task = await prisma.task.create({
|
||||
data: {
|
||||
creatorId: userId,
|
||||
type: 'upload',
|
||||
name: 'wrong-team-task',
|
||||
total: 1,
|
||||
status: TaskStatus.pending,
|
||||
},
|
||||
})
|
||||
|
||||
await prisma.asset.create({
|
||||
data: {
|
||||
name: 'wrong-team-file.mp4',
|
||||
type: AssetType.file,
|
||||
projectId: otherProject.id,
|
||||
taskId: task.id,
|
||||
status: AssetStatus.uploading,
|
||||
},
|
||||
})
|
||||
|
||||
await expect(uploadService.cancelUploadTask(teamId, userId, task.id)).rejects.toThrow(
|
||||
'Task does not belong to specified team',
|
||||
)
|
||||
})
|
||||
|
||||
it('throws when task is already completed and preserves completed status', async () => {
|
||||
const task = await prisma.task.create({
|
||||
data: {
|
||||
creatorId: userId,
|
||||
type: 'upload',
|
||||
name: 'completed-task',
|
||||
total: 1,
|
||||
uploaded: 1,
|
||||
status: TaskStatus.completed,
|
||||
},
|
||||
})
|
||||
|
||||
await expect(uploadService.cancelUploadTask(teamId, userId, task.id)).rejects.toThrow(
|
||||
'Cannot cancel a completed upload task',
|
||||
)
|
||||
|
||||
const refreshed = await prisma.task.findUnique({ where: { id: task.id } })
|
||||
expect(refreshed?.status).toBe(TaskStatus.completed)
|
||||
})
|
||||
|
||||
it('preserves an asset and its storage if it was confirmed uploaded concurrently', async () => {
|
||||
const task = await prisma.task.create({
|
||||
data: {
|
||||
creatorId: userId,
|
||||
type: 'upload',
|
||||
name: 'race-task',
|
||||
total: 2,
|
||||
uploaded: 0,
|
||||
status: TaskStatus.uploading,
|
||||
},
|
||||
})
|
||||
|
||||
const confirmedAsset = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'race-confirmed.mp4',
|
||||
type: AssetType.file,
|
||||
project: { connect: { id: projectId } },
|
||||
task: { connect: { id: task.id } },
|
||||
status: AssetStatus.uploading,
|
||||
storageKey: {
|
||||
create: { key: 'files/test/race-confirmed.mp4' },
|
||||
},
|
||||
},
|
||||
include: { storageKey: true },
|
||||
})
|
||||
|
||||
const cancelledAsset = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'race-cancelled.mp4',
|
||||
type: AssetType.file,
|
||||
project: { connect: { id: projectId } },
|
||||
task: { connect: { id: task.id } },
|
||||
status: AssetStatus.uploading,
|
||||
storageKey: {
|
||||
create: { key: 'files/test/race-cancelled.mp4' },
|
||||
},
|
||||
},
|
||||
include: { storageKey: true },
|
||||
})
|
||||
|
||||
// Simulate concurrent confirm: update status to uploaded
|
||||
await prisma.asset.update({
|
||||
where: { id: confirmedAsset.id },
|
||||
data: { status: AssetStatus.uploaded },
|
||||
})
|
||||
|
||||
const deleteObjectSpy = vi.spyOn(s3Service, 'deleteObject').mockResolvedValue(1)
|
||||
|
||||
await uploadService.cancelUploadTask(teamId, userId, task.id)
|
||||
|
||||
// Confirmed asset must still exist in DB
|
||||
const dbConfirmed = await prisma.asset.findUnique({ where: { id: confirmedAsset.id } })
|
||||
expect(dbConfirmed).not.toBeNull()
|
||||
expect(dbConfirmed?.status).toBe(AssetStatus.uploaded)
|
||||
|
||||
// Cancelled asset must be deleted
|
||||
const dbCancelled = await prisma.asset.findUnique({ where: { id: cancelledAsset.id } })
|
||||
expect(dbCancelled).toBeNull()
|
||||
|
||||
// S3 deleteObject was called for the cancelled asset, but NOT for the confirmed asset
|
||||
expect(deleteObjectSpy).toHaveBeenCalledWith(
|
||||
expect.anything(),
|
||||
'files/test/race-cancelled.mp4',
|
||||
)
|
||||
expect(deleteObjectSpy).not.toHaveBeenCalledWith(
|
||||
expect.anything(),
|
||||
'files/test/race-confirmed.mp4',
|
||||
)
|
||||
|
||||
deleteObjectSpy.mockRestore()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user