mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-19 14:16:29 +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:
@@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
|
||||
|
||||
### Added
|
||||
|
||||
- **upload**: Allow users to cancel running upload tasks directly from the upload tasks sidebar with a confirmation dialog, preserving already completed files while discarding incomplete assets
|
||||
- **upload**: Improve upload resilience during network drops, device sleep, and transient gateway errors by adding state-based exponential backoff retries and extending upload link expiration to 12 hours
|
||||
|
||||
### Fixed
|
||||
|
||||
@@ -220,4 +220,27 @@ describe('Upload API', () => {
|
||||
key: 'files/test.mp4',
|
||||
})
|
||||
})
|
||||
|
||||
it('POST /teams/:teamId/upload/tasks/:taskId/cancel', async () => {
|
||||
vi.spyOn(uploadService, 'cancelUploadTask').mockResolvedValue({
|
||||
success: true,
|
||||
})
|
||||
|
||||
const res = await app.request('/teams/team1/upload/tasks/task1/cancel', {
|
||||
method: 'POST',
|
||||
headers: { Authorization: 'Bearer test' },
|
||||
})
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const json = await res.json()
|
||||
expect(json).toEqual({ success: true })
|
||||
expect(authzService.hasPermission).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
type: ResourceType.Team,
|
||||
id: 'team1',
|
||||
permission: Permission.Edit,
|
||||
}),
|
||||
)
|
||||
expect(uploadService.cancelUploadTask).toHaveBeenCalledWith('team1', 'user1', 'task1')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -163,5 +163,20 @@ const route = new Hono<{ Variables: { user: User } }>()
|
||||
return c.json(resp)
|
||||
},
|
||||
)
|
||||
.post('/teams/:teamId/upload/tasks/:taskId/cancel', async (c) => {
|
||||
const teamId = c.req.param('teamId')
|
||||
const taskId = c.req.param('taskId')
|
||||
const user = c.get('user')
|
||||
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Edit,
|
||||
type: ResourceType.Team,
|
||||
id: teamId,
|
||||
})
|
||||
|
||||
const resp = await uploadService.cancelUploadTask(teamId, user.id, taskId)
|
||||
return c.json(resp)
|
||||
})
|
||||
|
||||
export default route
|
||||
|
||||
@@ -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()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -5,6 +5,7 @@ import { AssetStatus, AssetType, Prisma, TaskStatus, prisma } from '@shumai/db'
|
||||
import {
|
||||
AbortUploadRequest,
|
||||
AbortUploadResponse,
|
||||
CancelUploadTaskResponse,
|
||||
ConfirmFileUploadRequest,
|
||||
CreateUploadTaskRequest,
|
||||
CreateUploadTaskResponse,
|
||||
@@ -210,30 +211,7 @@ export class UploadService {
|
||||
if (!key) throw new Error('Asset has no key')
|
||||
|
||||
if (req.errorMessage) {
|
||||
const bucket = process.env.S3_BUCKET || 'shumai'
|
||||
if (asset.uploadId && key) {
|
||||
try {
|
||||
await s3Service.abortMultipartUpload(bucket, key, asset.uploadId)
|
||||
} catch (err) {
|
||||
logger.warn(
|
||||
{ err, key, uploadId: asset.uploadId },
|
||||
'Failed to abort multipart upload on confirm error',
|
||||
)
|
||||
}
|
||||
} else if (key) {
|
||||
try {
|
||||
await s3Service.deleteObject(bucket, key)
|
||||
} catch (err) {
|
||||
logger.warn({ err, key }, 'Failed to delete storage object on confirm error')
|
||||
}
|
||||
}
|
||||
|
||||
await this.prismaClient.asset.delete({ where: { id: asset.id } }).catch(() => {})
|
||||
if (asset.storageKeyId) {
|
||||
await this.prismaClient.storageKey
|
||||
.delete({ where: { id: asset.storageKeyId } })
|
||||
.catch(() => {})
|
||||
}
|
||||
await this.discardUploadingAsset(asset)
|
||||
const t = await this.prismaClient.task.findUnique({ where: { id: taskId } })
|
||||
if (t) {
|
||||
const updatedTask = await this.prismaClient.task.update({
|
||||
@@ -250,6 +228,10 @@ export class UploadService {
|
||||
return
|
||||
}
|
||||
|
||||
if (asset.status !== AssetStatus.uploading) {
|
||||
throw new Error('Asset is not in uploading status')
|
||||
}
|
||||
|
||||
const size = await s3Service.getObjectSize(process.env.S3_BUCKET || 'shumai', key)
|
||||
|
||||
const project = asset.project
|
||||
@@ -425,6 +407,7 @@ export class UploadService {
|
||||
total: t.total || 0,
|
||||
uploaded: t.uploaded,
|
||||
createdAt: t.createdAt.toISOString(),
|
||||
status: t.status,
|
||||
}))
|
||||
|
||||
return { data: infos, pageInfo }
|
||||
@@ -490,7 +473,6 @@ export class UploadService {
|
||||
taskId: string,
|
||||
req: AbortUploadRequest,
|
||||
): Promise<AbortUploadResponse> {
|
||||
const bucket = process.env.S3_BUCKET || 'shumai'
|
||||
const asset = await this.prismaClient.asset.findUnique({
|
||||
where: { id: req.fileId },
|
||||
include: {
|
||||
@@ -520,33 +502,11 @@ export class UploadService {
|
||||
throw new Error('Provided key does not match asset storage key')
|
||||
}
|
||||
|
||||
const uploadId = asset.uploadId || req.uploadId
|
||||
if (asset.uploadId && req.uploadId && asset.uploadId !== req.uploadId) {
|
||||
throw new Error('Provided uploadId does not match asset uploadId')
|
||||
}
|
||||
|
||||
if (uploadId && key) {
|
||||
try {
|
||||
await s3Service.abortMultipartUpload(bucket, key, uploadId)
|
||||
} catch (err) {
|
||||
logger.warn({ err, key, uploadId }, 'Failed to abort multipart upload in S3')
|
||||
}
|
||||
} else if (key) {
|
||||
try {
|
||||
await s3Service.deleteObject(bucket, key)
|
||||
} catch (err) {
|
||||
logger.warn({ err, key }, 'Failed to delete object from storage')
|
||||
}
|
||||
}
|
||||
|
||||
await this.prismaClient.asset.delete({ where: { id: asset.id } }).catch((err) => {
|
||||
logger.warn({ err, assetId: asset.id }, 'Failed to delete asset on upload abort')
|
||||
})
|
||||
if (asset.storageKeyId) {
|
||||
await this.prismaClient.storageKey
|
||||
.delete({ where: { id: asset.storageKeyId } })
|
||||
.catch(() => {})
|
||||
}
|
||||
await this.discardUploadingAsset(asset, req.uploadId)
|
||||
|
||||
if (taskId) {
|
||||
const remaining = await this.prismaClient.asset.count({
|
||||
@@ -564,6 +524,104 @@ export class UploadService {
|
||||
|
||||
return { success: true }
|
||||
}
|
||||
|
||||
private async discardUploadingAsset(
|
||||
asset: {
|
||||
id: string
|
||||
uploadId: string | null
|
||||
storageKeyId: string | null
|
||||
storageKey: { key: string } | null
|
||||
},
|
||||
requestedUploadId?: string,
|
||||
): Promise<void> {
|
||||
const deleteResult = await this.prismaClient.asset.deleteMany({
|
||||
where: {
|
||||
id: asset.id,
|
||||
status: AssetStatus.uploading,
|
||||
},
|
||||
})
|
||||
|
||||
if (deleteResult.count === 0) {
|
||||
return
|
||||
}
|
||||
|
||||
const bucket = process.env.S3_BUCKET || 'shumai'
|
||||
const key = asset.storageKey?.key
|
||||
const uploadId = asset.uploadId || requestedUploadId
|
||||
|
||||
if (uploadId && key) {
|
||||
try {
|
||||
await s3Service.abortMultipartUpload(bucket, key, uploadId)
|
||||
} catch (err) {
|
||||
logger.warn({ err, key, uploadId }, 'Failed to abort multipart upload in S3')
|
||||
}
|
||||
} else if (key) {
|
||||
try {
|
||||
await s3Service.deleteObject(bucket, key)
|
||||
} catch (err) {
|
||||
logger.warn({ err, key }, 'Failed to delete object from storage')
|
||||
}
|
||||
}
|
||||
|
||||
if (asset.storageKeyId) {
|
||||
await this.prismaClient.storageKey
|
||||
.delete({ where: { id: asset.storageKeyId } })
|
||||
.catch(() => {})
|
||||
}
|
||||
}
|
||||
|
||||
async cancelUploadTask(
|
||||
teamId: string,
|
||||
userId: string,
|
||||
taskId: string,
|
||||
): Promise<CancelUploadTaskResponse> {
|
||||
const task = await this.prismaClient.task.findUnique({
|
||||
where: { id: taskId },
|
||||
include: {
|
||||
assets: {
|
||||
include: {
|
||||
storageKey: true,
|
||||
project: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
if (!task) {
|
||||
throw new Error('Upload task not found')
|
||||
}
|
||||
|
||||
if (task.creatorId !== userId) {
|
||||
throw new Error('Unauthorized to cancel this upload task')
|
||||
}
|
||||
|
||||
if (task.status === TaskStatus.completed) {
|
||||
throw new Error('Cannot cancel a completed upload task')
|
||||
}
|
||||
|
||||
const invalidTeamAsset = task.assets.find(
|
||||
(a) => a.project?.teamId && a.project.teamId !== teamId,
|
||||
)
|
||||
if (invalidTeamAsset) {
|
||||
throw new Error('Task does not belong to specified team')
|
||||
}
|
||||
|
||||
const uploadingAssets = task.assets.filter((a) => a.status === AssetStatus.uploading)
|
||||
|
||||
for (const asset of uploadingAssets) {
|
||||
await this.discardUploadingAsset(asset)
|
||||
}
|
||||
|
||||
await this.prismaClient.task.updateMany({
|
||||
where: {
|
||||
id: taskId,
|
||||
status: { not: TaskStatus.completed },
|
||||
},
|
||||
data: { status: TaskStatus.failed },
|
||||
})
|
||||
|
||||
return { success: true }
|
||||
}
|
||||
}
|
||||
|
||||
export const uploadService = new UploadService()
|
||||
|
||||
@@ -75,6 +75,11 @@ export const abortUploadResponseSchema = z.object({
|
||||
})
|
||||
export type AbortUploadResponse = z.infer<typeof abortUploadResponseSchema>
|
||||
|
||||
export const cancelUploadTaskResponseSchema = z.object({
|
||||
success: z.boolean(),
|
||||
})
|
||||
export type CancelUploadTaskResponse = z.infer<typeof cancelUploadTaskResponseSchema>
|
||||
|
||||
export const confirmFileUploadRequestSchema = z.object({
|
||||
fileId: z.string(),
|
||||
errorMessage: z.string().optional(),
|
||||
@@ -87,6 +92,7 @@ export const taskInfoSchema = z.object({
|
||||
total: z.number(),
|
||||
uploaded: z.number(),
|
||||
createdAt: z.string(),
|
||||
status: z.enum(['pending', 'uploading', 'completed', 'failed']).optional(),
|
||||
})
|
||||
export type TaskInfo = z.infer<typeof taskInfoSchema>
|
||||
|
||||
|
||||
@@ -250,12 +250,15 @@ export function FileBrowser({
|
||||
}
|
||||
|
||||
const [localUploadingFiles, setLocalUploadingFiles] = useState<AssetInfo[]>([])
|
||||
const fileProgress = useUploadStore((state) => state.fileProgress)
|
||||
|
||||
const displayedFiles = useMemo(() => {
|
||||
const existingIds = new Set(files.map((f) => f.id))
|
||||
const filteredLocal = localUploadingFiles.filter((f) => !existingIds.has(f.id))
|
||||
const filteredLocal = localUploadingFiles
|
||||
.filter((f) => !existingIds.has(f.id))
|
||||
.filter((f) => fileProgress[f.id!]?.status !== 'failed')
|
||||
return [...filteredLocal, ...files]
|
||||
}, [files, localUploadingFiles])
|
||||
}, [files, localUploadingFiles, fileProgress])
|
||||
|
||||
const activePreviewItem = useMemo(() => {
|
||||
if (selectedIds.size === 0) return null
|
||||
@@ -575,6 +578,7 @@ export function FileBrowser({
|
||||
startTask(data.taskId, taskName, filesProgressInfo)
|
||||
}
|
||||
|
||||
let batchIds: Set<string> | null = null
|
||||
if (isCurrentFolderUpload) {
|
||||
const newLocalFiles: AssetInfo[] = currentFiles
|
||||
.filter((f) => {
|
||||
@@ -615,6 +619,7 @@ export function FileBrowser({
|
||||
updatedAt: new Date().toISOString(),
|
||||
}
|
||||
})
|
||||
batchIds = new Set(newLocalFiles.map((f) => f.id!))
|
||||
setLocalUploadingFiles((prev) => [...newLocalFiles, ...prev])
|
||||
}
|
||||
|
||||
@@ -626,26 +631,32 @@ export function FileBrowser({
|
||||
queryKey: ['teams', teamId, 'upload', 'tasks'],
|
||||
})
|
||||
|
||||
await uploadFilesWithUppy({
|
||||
files: currentFiles,
|
||||
taskId: data.taskId!,
|
||||
teamId: teamId!,
|
||||
storageBackend: (data.storageBackend as 's3' | 'local') || 'local',
|
||||
createdAssets: data.createdAssets,
|
||||
presignedUrls: data.presignedUrls,
|
||||
onFileFinished: async (fileId) => {
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: ['search', teamId, assetId],
|
||||
})
|
||||
setLocalUploadingFiles((prev) => prev.filter((f) => f.id !== fileId))
|
||||
},
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ['search', teamId, assetId],
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ['teams', teamId, 'upload', 'tasks'],
|
||||
})
|
||||
try {
|
||||
await uploadFilesWithUppy({
|
||||
files: currentFiles,
|
||||
taskId: data.taskId!,
|
||||
teamId: teamId!,
|
||||
storageBackend: (data.storageBackend as 's3' | 'local') || 'local',
|
||||
createdAssets: data.createdAssets,
|
||||
presignedUrls: data.presignedUrls,
|
||||
onFileFinished: async (fileId) => {
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: ['search', teamId, assetId],
|
||||
})
|
||||
setLocalUploadingFiles((prev) => prev.filter((f) => f.id !== fileId))
|
||||
},
|
||||
})
|
||||
} finally {
|
||||
if (batchIds) {
|
||||
setLocalUploadingFiles((prev) => prev.filter((f) => !batchIds!.has(f.id!)))
|
||||
}
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ['search', teamId, assetId],
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ['teams', teamId, 'upload', 'tasks'],
|
||||
})
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
@@ -0,0 +1,172 @@
|
||||
// @vitest-environment happy-dom
|
||||
import { cleanup, render, screen, fireEvent, waitFor } from '@testing-library/react'
|
||||
import React from 'react'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import { UploadTasks } from './upload-tasks'
|
||||
import { useUploadStore } from '@/ui/stores/upload'
|
||||
import * as uploaderLib from '@/ui/lib/uploader'
|
||||
import { toast } from 'sonner'
|
||||
|
||||
const mockGetTasks = vi.fn()
|
||||
const mockCancelTask = vi.fn()
|
||||
|
||||
vi.mock('@/ui/api/client', () => ({
|
||||
client: {
|
||||
api: {
|
||||
teams: {
|
||||
':teamId': {
|
||||
upload: {
|
||||
tasks: Object.assign(
|
||||
{
|
||||
$get: () => mockGetTasks(),
|
||||
},
|
||||
{
|
||||
':taskId': {
|
||||
cancel: {
|
||||
$post: (args: unknown) => mockCancelTask(args),
|
||||
},
|
||||
},
|
||||
},
|
||||
),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock('@/ui/hooks/use-team-id', () => ({
|
||||
useTeamId: () => 'team-1',
|
||||
}))
|
||||
|
||||
vi.mock('sonner', () => ({
|
||||
toast: {
|
||||
success: vi.fn(),
|
||||
error: vi.fn(),
|
||||
},
|
||||
}))
|
||||
|
||||
describe('UploadTasks', () => {
|
||||
let queryClient: QueryClient
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
queryClient = new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: { retry: false },
|
||||
},
|
||||
})
|
||||
useUploadStore.setState({
|
||||
uploading: 0,
|
||||
tasks: {},
|
||||
fileProgress: {},
|
||||
})
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
cleanup()
|
||||
})
|
||||
|
||||
it('renders completed and failed tasks without a cancel button', async () => {
|
||||
mockGetTasks.mockResolvedValue({
|
||||
ok: true,
|
||||
json: async () => ({
|
||||
data: [
|
||||
{
|
||||
id: 'task-done',
|
||||
name: 'Completed Task',
|
||||
total: 2,
|
||||
uploaded: 2,
|
||||
status: 'completed',
|
||||
createdAt: new Date().toISOString(),
|
||||
},
|
||||
{
|
||||
id: 'task-failed',
|
||||
name: 'Failed Task',
|
||||
total: 2,
|
||||
uploaded: 1,
|
||||
status: 'failed',
|
||||
createdAt: new Date().toISOString(),
|
||||
},
|
||||
],
|
||||
pageInfo: {},
|
||||
}),
|
||||
})
|
||||
|
||||
render(
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<UploadTasks />
|
||||
</QueryClientProvider>,
|
||||
)
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Completed Task')).toBeDefined()
|
||||
expect(screen.getByText('Failed Task')).toBeDefined()
|
||||
})
|
||||
|
||||
// No cancel button should be rendered for completed or failed tasks
|
||||
expect(screen.queryByRole('button', { name: /cancel/i })).toBeNull()
|
||||
})
|
||||
|
||||
it('renders cancel button for running tasks and executes cancel flow on confirmation', async () => {
|
||||
const cancelActiveSpy = vi.spyOn(uploaderLib, 'cancelActiveUploadTask')
|
||||
|
||||
mockGetTasks.mockResolvedValue({
|
||||
ok: true,
|
||||
json: async () => ({
|
||||
data: [
|
||||
{
|
||||
id: 'task-running',
|
||||
name: 'Running Task',
|
||||
total: 3,
|
||||
uploaded: 1,
|
||||
status: 'uploading',
|
||||
createdAt: new Date().toISOString(),
|
||||
},
|
||||
],
|
||||
pageInfo: {},
|
||||
}),
|
||||
})
|
||||
|
||||
mockCancelTask.mockResolvedValue({
|
||||
ok: true,
|
||||
json: async () => ({ success: true }),
|
||||
})
|
||||
|
||||
render(
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<UploadTasks />
|
||||
</QueryClientProvider>,
|
||||
)
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Running Task')).toBeDefined()
|
||||
})
|
||||
|
||||
// Cancel button should be present
|
||||
const cancelButton = screen.getByRole('button', { name: /cancel/i })
|
||||
expect(cancelButton).toBeDefined()
|
||||
|
||||
// Click Cancel to open confirmation dialog
|
||||
fireEvent.click(cancelButton)
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText(/cancel upload task\?/i)).toBeDefined()
|
||||
})
|
||||
|
||||
// Click confirm in the dialog
|
||||
const confirmButtons = screen.getAllByRole('button', { name: /cancel upload/i })
|
||||
fireEvent.click(confirmButtons[confirmButtons.length - 1])
|
||||
|
||||
await waitFor(() => {
|
||||
expect(cancelActiveSpy).toHaveBeenCalledWith('task-running')
|
||||
expect(mockCancelTask).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
param: { teamId: 'team-1', taskId: 'task-running' },
|
||||
}),
|
||||
)
|
||||
expect(toast.success).toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,13 +1,26 @@
|
||||
import { client } from '@/ui/api/client'
|
||||
import type { TaskInfo } from '@shumai/dtos'
|
||||
import { useTeamId } from '@/ui/hooks/use-team-id'
|
||||
import { useInfiniteQuery } from '@tanstack/react-query'
|
||||
import { useInfiniteQuery, useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { useUploadStore } from '@/ui/stores/upload'
|
||||
import { Loader2, CheckCircle2, Clock, UploadCloud } from 'lucide-react'
|
||||
import { useEffect, useMemo } from 'react'
|
||||
import { cancelActiveUploadTask } from '@/ui/lib/uploader'
|
||||
import { Loader2, CheckCircle2, Clock, UploadCloud, AlertCircle } from 'lucide-react'
|
||||
import { useEffect, useMemo, useState } from 'react'
|
||||
import { useInView } from 'react-intersection-observer'
|
||||
import { format, parseISO, isToday, isYesterday } from 'date-fns'
|
||||
import { Progress } from '@/ui/components/ui/progress'
|
||||
import { Button } from '@/ui/components/ui/button'
|
||||
import {
|
||||
AlertDialog,
|
||||
AlertDialogAction,
|
||||
AlertDialogCancel,
|
||||
AlertDialogContent,
|
||||
AlertDialogDescription,
|
||||
AlertDialogFooter,
|
||||
AlertDialogHeader,
|
||||
AlertDialogTitle,
|
||||
} from '@/ui/components/ui/alert-dialog'
|
||||
import { toast } from 'sonner'
|
||||
import { formatTimeAgo } from '@/ui/lib/time'
|
||||
import { m } from '@/ui/paraglide/messages.js'
|
||||
|
||||
@@ -33,20 +46,29 @@ function formatBytes(bytes: number): string {
|
||||
return parseFloat((bytes / Math.pow(k, i)).toFixed(1)) + ' ' + sizes[i]
|
||||
}
|
||||
|
||||
function UploadTaskItem({ task }: { task: TaskInfo }) {
|
||||
function UploadTaskItem({ task, teamId }: { task: TaskInfo; teamId?: string }) {
|
||||
const [isCancelConfirmOpen, setIsCancelConfirmOpen] = useState(false)
|
||||
const clientProgress = useUploadStore((state) => state.tasks[task.id])
|
||||
const cancelTaskInStore = useUploadStore((state) => state.cancelTask)
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
const isCompleted = clientProgress
|
||||
? clientProgress.loaded === clientProgress.total && task.uploaded === task.total
|
||||
: task.uploaded === task.total
|
||||
|
||||
const percent = clientProgress
|
||||
? clientProgress.total > 0
|
||||
? (clientProgress.loaded / clientProgress.total) * 100
|
||||
: 0
|
||||
: task.total > 0
|
||||
? (task.uploaded / task.total) * 100
|
||||
: 0
|
||||
const isFailed = task.status === 'failed'
|
||||
const isDone = task.status === 'completed' || isCompleted
|
||||
const isRunning = !isDone && !isFailed
|
||||
|
||||
const percent = isDone
|
||||
? 100
|
||||
: clientProgress
|
||||
? clientProgress.total > 0
|
||||
? (clientProgress.loaded / clientProgress.total) * 100
|
||||
: 0
|
||||
: task.total > 0
|
||||
? (task.uploaded / task.total) * 100
|
||||
: 0
|
||||
|
||||
let formattedTime: string
|
||||
try {
|
||||
@@ -55,6 +77,37 @@ function UploadTaskItem({ task }: { task: TaskInfo }) {
|
||||
formattedTime = m.unknown()
|
||||
}
|
||||
|
||||
const { mutate: cancelMutation, isPending: isCanceling } = useMutation({
|
||||
mutationFn: async () => {
|
||||
cancelActiveUploadTask(task.id)
|
||||
cancelTaskInStore(task.id)
|
||||
|
||||
if (teamId) {
|
||||
const res = await client.api.teams[':teamId'].upload.tasks[':taskId'].cancel.$post({
|
||||
param: { teamId, taskId: task.id },
|
||||
})
|
||||
if (!res.ok) {
|
||||
throw new Error('Failed to cancel upload task')
|
||||
}
|
||||
return await res.json()
|
||||
}
|
||||
},
|
||||
onSuccess: () => {
|
||||
setIsCancelConfirmOpen(false)
|
||||
toast.success(m.upload_task_canceled())
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ['teams', teamId, 'upload', 'tasks'],
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ['search', teamId],
|
||||
})
|
||||
},
|
||||
onError: (err) => {
|
||||
console.error('Failed to cancel upload task:', err)
|
||||
toast.error(m.failed_to_cancel_upload_task())
|
||||
},
|
||||
})
|
||||
|
||||
return (
|
||||
<div className="group relative flex flex-col p-4 rounded-xl border border-sidebar-border bg-sidebar-accent/30 text-sidebar-foreground shadow-xs transition-all duration-200 hover:shadow-md hover:border-sidebar-foreground/20 hover:bg-sidebar-accent/50">
|
||||
{/* Top row: Name & Status Badge */}
|
||||
@@ -68,11 +121,16 @@ function UploadTaskItem({ task }: { task: TaskInfo }) {
|
||||
</span>
|
||||
</div>
|
||||
<div className="flex-shrink-0">
|
||||
{isCompleted ? (
|
||||
{isDone ? (
|
||||
<span className="inline-flex items-center gap-1 text-xs font-semibold text-muted-foreground bg-muted px-2 py-0.5 rounded-full transition-colors">
|
||||
<CheckCircle2 className="w-3.5 h-3.5" />
|
||||
{m.done()}
|
||||
</span>
|
||||
) : isFailed ? (
|
||||
<span className="inline-flex items-center gap-1 text-xs font-semibold text-destructive bg-destructive/10 px-2 py-0.5 rounded-full">
|
||||
<AlertCircle className="w-3.5 h-3.5" />
|
||||
{m.status_failed()}
|
||||
</span>
|
||||
) : (
|
||||
<span className="inline-flex items-center gap-1 text-xs font-semibold text-sidebar-primary bg-sidebar-primary/10 px-2 py-0.5 rounded-full animate-pulse">
|
||||
<Loader2 className="w-3.5 h-3.5 animate-spin" />
|
||||
@@ -88,7 +146,7 @@ function UploadTaskItem({ task }: { task: TaskInfo }) {
|
||||
<span>{formattedTime}</span>
|
||||
</div>
|
||||
|
||||
{/* Bottom row: Progress Bar and Fraction */}
|
||||
{/* Bottom row: Progress Bar, Fraction & Cancel Button */}
|
||||
<div className="space-y-1.5 mt-auto">
|
||||
<div className="flex items-center justify-between text-xs font-semibold">
|
||||
<span className="text-muted-foreground/80">
|
||||
@@ -96,12 +154,56 @@ function UploadTaskItem({ task }: { task: TaskInfo }) {
|
||||
? `${formatBytes(clientProgress.loaded)} / ${formatBytes(clientProgress.total)} (${m.files_progress({ uploaded: task.uploaded, total: task.total })})`
|
||||
: m.files_progress({ uploaded: task.uploaded, total: task.total })}
|
||||
</span>
|
||||
{!isCompleted && (
|
||||
<span className="text-sidebar-primary font-mono font-bold">{Math.round(percent)}%</span>
|
||||
)}
|
||||
<div className="flex items-center gap-2">
|
||||
{!isDone && !isFailed && (
|
||||
<span className="text-sidebar-primary font-mono font-bold">
|
||||
{Math.round(percent)}%
|
||||
</span>
|
||||
)}
|
||||
{isRunning && (
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="h-6 px-2 text-xs font-medium text-muted-foreground hover:text-destructive hover:bg-destructive/10"
|
||||
disabled={isCanceling}
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
setIsCancelConfirmOpen(true)
|
||||
}}
|
||||
>
|
||||
{isCanceling ? <Loader2 className="w-3 h-3 animate-spin mr-1" /> : null}
|
||||
{m.cancel()}
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<Progress value={percent} className="h-1.5 w-full bg-muted" />
|
||||
</div>
|
||||
|
||||
<AlertDialog open={isCancelConfirmOpen} onOpenChange={setIsCancelConfirmOpen}>
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>{m.cancel_upload_task_confirm_title()}</AlertDialogTitle>
|
||||
<AlertDialogDescription>
|
||||
{m.cancel_upload_task_confirm_description()}
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel disabled={isCanceling}>{m.cancel()}</AlertDialogCancel>
|
||||
<AlertDialogAction
|
||||
disabled={isCanceling}
|
||||
onClick={(e) => {
|
||||
e.preventDefault()
|
||||
cancelMutation()
|
||||
}}
|
||||
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
|
||||
>
|
||||
{isCanceling ? <Loader2 className="w-4 h-4 animate-spin mr-1" /> : null}
|
||||
{m.cancel_upload_task()}
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -218,7 +320,7 @@ export function UploadTasks() {
|
||||
{/* Tasks in Day */}
|
||||
<div className="mt-3 space-y-3">
|
||||
{group.tasks.map((task) => (
|
||||
<UploadTaskItem key={task.id} task={task} />
|
||||
<UploadTaskItem key={task.id} task={task} teamId={teamId} />
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import {
|
||||
activeUploads,
|
||||
cancelActiveUploadTask,
|
||||
uploadFilesWithUppy,
|
||||
isNetworkIssue,
|
||||
isLocalhostStorageMismatch,
|
||||
@@ -856,4 +857,33 @@ describe('Uploader', () => {
|
||||
|
||||
addFileSpy.mockRestore()
|
||||
})
|
||||
|
||||
it('cancelActiveUploadTask cancels in-flight uppy upload and cleans up activeUploads', async () => {
|
||||
useUploadStore
|
||||
.getState()
|
||||
.startTask('task-cancel', 'Task Cancel', [
|
||||
{ fileId: 'file-c1', name: 'cancel.mp4', size: 1000 },
|
||||
])
|
||||
|
||||
const uploadPromise = uploadFilesWithUppy({
|
||||
files: [
|
||||
{
|
||||
id: 'temp-c1',
|
||||
file: new File(['data'], 'cancel.mp4', { type: 'video/mp4' }),
|
||||
},
|
||||
],
|
||||
taskId: 'task-cancel',
|
||||
teamId: 'team-1',
|
||||
storageBackend: 's3',
|
||||
createdAssets: [{ tempId: 'temp-c1', assetId: 'file-c1', key: 'files/cancel.mp4' }],
|
||||
})
|
||||
|
||||
await new Promise((r) => setTimeout(r, 10))
|
||||
expect(activeUploads.has('file-c1')).toBe(true)
|
||||
|
||||
cancelActiveUploadTask('task-cancel')
|
||||
|
||||
expect(activeUploads.has('file-c1')).toBe(false)
|
||||
await expect(uploadPromise).resolves.toBeUndefined()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -23,6 +23,24 @@ export interface ActiveUploadFile {
|
||||
}
|
||||
|
||||
export const activeUploads = new Map<string, ActiveUploadFile>()
|
||||
export const activeUppyByTaskId = new Map<string, Uppy>()
|
||||
|
||||
export function cancelActiveUploadTask(taskId: string): void {
|
||||
const uppy = activeUppyByTaskId.get(taskId)
|
||||
if (uppy) {
|
||||
try {
|
||||
uppy.cancelAll()
|
||||
} catch (err) {
|
||||
console.warn('Failed to cancel uppy uploads:', err)
|
||||
}
|
||||
}
|
||||
|
||||
for (const [fileId, item] of activeUploads.entries()) {
|
||||
if (item.taskId === taskId) {
|
||||
activeUploads.delete(fileId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Setup beforeunload and pagehide listeners once
|
||||
if (typeof window !== 'undefined') {
|
||||
@@ -237,7 +255,7 @@ export async function uploadFilesWithUppy({
|
||||
await acquireWakeLock()
|
||||
|
||||
const store = useUploadStore.getState()
|
||||
const { increment, decrement, updateFileProgress, completeFile, failFile } = store
|
||||
const { increment, updateFileProgress, completeFile, failFile } = store
|
||||
|
||||
// Build lookup maps
|
||||
const assetByTempId = new Map<string, { assetId: string; key?: string }>()
|
||||
@@ -258,6 +276,7 @@ export async function uploadFilesWithUppy({
|
||||
autoProceed: true,
|
||||
allowMultipleUploadBatches: false,
|
||||
})
|
||||
activeUppyByTaskId.set(taskId, uppy)
|
||||
|
||||
if (storageBackend === 's3') {
|
||||
uppy.use(AwsS3, {
|
||||
@@ -417,12 +436,10 @@ export async function uploadFilesWithUppy({
|
||||
throw new Error(`Failed to confirm upload: ${res.statusText}`)
|
||||
}
|
||||
completeFile(taskId, fileId)
|
||||
decrement()
|
||||
await onFileFinished?.(fileId)
|
||||
} catch (err) {
|
||||
console.error('Failed to confirm upload:', err)
|
||||
failFile(taskId, fileId)
|
||||
decrement()
|
||||
toast.error(m.failed_confirm_upload_with_name({ name: file.name }))
|
||||
await onFileFinished?.(fileId)
|
||||
throw err
|
||||
@@ -443,7 +460,6 @@ export async function uploadFilesWithUppy({
|
||||
|
||||
activeUploads.delete(fileId)
|
||||
failFile(taskId, fileId)
|
||||
decrement()
|
||||
|
||||
if (isLocalhostStorageMismatch(uploadUrl)) {
|
||||
toast.error(m.upload_failed_s3_endpoint_localhost(), {
|
||||
@@ -600,6 +616,7 @@ export async function uploadFilesWithUppy({
|
||||
|
||||
await Promise.allSettled(Array.from(confirmationPromises.values()))
|
||||
} finally {
|
||||
activeUppyByTaskId.delete(taskId)
|
||||
if (retryTimeout) {
|
||||
clearTimeout(retryTimeout)
|
||||
}
|
||||
|
||||
@@ -1607,5 +1607,10 @@
|
||||
"deny": "Deny",
|
||||
"denying": "Denying...",
|
||||
"consent_failed": "Failed to process authorization",
|
||||
"mcp_access_scope_desc": "Full user-level access to Shumai workspace and MCP tools"
|
||||
"mcp_access_scope_desc": "Full user-level access to Shumai workspace and MCP tools",
|
||||
"cancel_upload_task": "Cancel upload",
|
||||
"cancel_upload_task_confirm_title": "Cancel upload task?",
|
||||
"cancel_upload_task_confirm_description": "Files that have already finished uploading will be kept in your project. Any incomplete or pending uploads will be canceled and removed.",
|
||||
"upload_task_canceled": "Upload task canceled",
|
||||
"failed_to_cancel_upload_task": "Failed to cancel upload task"
|
||||
}
|
||||
|
||||
@@ -1607,5 +1607,10 @@
|
||||
"deny": "拒绝",
|
||||
"denying": "拒绝中...",
|
||||
"consent_failed": "处理授权失败",
|
||||
"mcp_access_scope_desc": "以当前用户权限完全访问 Shumai 工作空间及 MCP 工具"
|
||||
"mcp_access_scope_desc": "以当前用户权限完全访问 Shumai 工作空间及 MCP 工具",
|
||||
"cancel_upload_task": "取消上传",
|
||||
"cancel_upload_task_confirm_title": "取消上传任务?",
|
||||
"cancel_upload_task_confirm_description": "已完成上传的文件将保留在项目中。任何未完成或等待中的上传将被取消并移除。",
|
||||
"upload_task_canceled": "上传任务已取消",
|
||||
"failed_to_cancel_upload_task": "取消上传任务失败"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
import { describe, it, expect, beforeEach } from 'vitest'
|
||||
import { useUploadStore } from './upload'
|
||||
|
||||
describe('useUploadStore', () => {
|
||||
beforeEach(() => {
|
||||
useUploadStore.setState({
|
||||
uploading: 0,
|
||||
tasks: {},
|
||||
fileProgress: {},
|
||||
})
|
||||
})
|
||||
|
||||
it('starts task and tracks initial progress', () => {
|
||||
useUploadStore.getState().startTask('task-1', 'My Task', [
|
||||
{ fileId: 'f1', name: 'video1.mp4', size: 1000 },
|
||||
{ fileId: 'f2', name: 'video2.mp4', size: 2000 },
|
||||
])
|
||||
|
||||
const state = useUploadStore.getState()
|
||||
expect(state.tasks['task-1']).toBeDefined()
|
||||
expect(state.tasks['task-1'].name).toBe('My Task')
|
||||
expect(state.tasks['task-1'].total).toBe(3000)
|
||||
expect(state.tasks['task-1'].loaded).toBe(0)
|
||||
expect(state.tasks['task-1'].files['f1'].status).toBe('uploading')
|
||||
expect(state.tasks['task-1'].files['f2'].status).toBe('uploading')
|
||||
})
|
||||
|
||||
it('updates file progress and task loaded bytes', () => {
|
||||
useUploadStore.getState().startTask('task-1', 'My Task', [
|
||||
{ fileId: 'f1', name: 'video1.mp4', size: 1000 },
|
||||
{ fileId: 'f2', name: 'video2.mp4', size: 2000 },
|
||||
])
|
||||
|
||||
useUploadStore.getState().updateFileProgress('task-1', 'f1', 500)
|
||||
expect(useUploadStore.getState().tasks['task-1'].loaded).toBe(500)
|
||||
expect(useUploadStore.getState().fileProgress['f1'].loaded).toBe(500)
|
||||
|
||||
useUploadStore.getState().completeFile('task-1', 'f1')
|
||||
expect(useUploadStore.getState().tasks['task-1'].files['f1'].status).toBe('completed')
|
||||
expect(useUploadStore.getState().tasks['task-1'].files['f1'].loaded).toBe(1000)
|
||||
expect(useUploadStore.getState().tasks['task-1'].loaded).toBe(1000)
|
||||
})
|
||||
|
||||
it('cancelTask marks incomplete files as failed and decrements uploading count', () => {
|
||||
useUploadStore.setState({ uploading: 3 })
|
||||
useUploadStore.getState().startTask('task-1', 'My Task', [
|
||||
{ fileId: 'f1', name: 'done.mp4', size: 1000 },
|
||||
{ fileId: 'f2', name: 'stuck1.mp4', size: 2000 },
|
||||
{ fileId: 'f3', name: 'stuck2.mp4', size: 3000 },
|
||||
])
|
||||
|
||||
// f1 completes (automatically decrements uploading from 3 to 2)
|
||||
useUploadStore.getState().completeFile('task-1', 'f1')
|
||||
expect(useUploadStore.getState().uploading).toBe(2)
|
||||
|
||||
// cancelTask is called
|
||||
useUploadStore.getState().cancelTask('task-1')
|
||||
|
||||
const task = useUploadStore.getState().tasks['task-1']
|
||||
expect(task.files['f1'].status).toBe('completed')
|
||||
expect(task.files['f2'].status).toBe('failed')
|
||||
expect(task.files['f3'].status).toBe('failed')
|
||||
expect(useUploadStore.getState().fileProgress['f2'].status).toBe('failed')
|
||||
expect(useUploadStore.getState().fileProgress['f3'].status).toBe('failed')
|
||||
expect(useUploadStore.getState().uploading).toBe(0)
|
||||
})
|
||||
|
||||
it('does not decrement uploading count twice if completeFile or failFile is called after cancelTask', () => {
|
||||
useUploadStore.setState({ uploading: 2 })
|
||||
useUploadStore.getState().startTask('task-1', 'My Task', [
|
||||
{ fileId: 'f1', name: 'in-flight.mp4', size: 1000 },
|
||||
{ fileId: 'f2', name: 'waiting.mp4', size: 2000 },
|
||||
])
|
||||
|
||||
// Cancel task while f1 confirmation was in-flight
|
||||
useUploadStore.getState().cancelTask('task-1')
|
||||
expect(useUploadStore.getState().uploading).toBe(0)
|
||||
expect(useUploadStore.getState().tasks['task-1'].files['f1'].status).toBe('failed')
|
||||
|
||||
// In-flight confirmation settles late
|
||||
useUploadStore.getState().completeFile('task-1', 'f1')
|
||||
expect(useUploadStore.getState().uploading).toBe(0)
|
||||
|
||||
// Another late error settles
|
||||
useUploadStore.getState().failFile('task-1', 'f2')
|
||||
expect(useUploadStore.getState().uploading).toBe(0)
|
||||
})
|
||||
|
||||
it('cancelTask gracefully handles non-existent task', () => {
|
||||
useUploadStore.setState({ uploading: 1 })
|
||||
useUploadStore.getState().cancelTask('non-existent')
|
||||
expect(useUploadStore.getState().uploading).toBe(1)
|
||||
})
|
||||
})
|
||||
@@ -30,6 +30,7 @@ type UploadStore = {
|
||||
updateFileProgress: (taskId: string, fileId: string, loaded: number) => void
|
||||
completeFile: (taskId: string, fileId: string) => void
|
||||
failFile: (taskId: string, fileId: string) => void
|
||||
cancelTask: (taskId: string) => void
|
||||
}
|
||||
|
||||
export const useUploadStore = create<UploadStore>((set) => ({
|
||||
@@ -114,12 +115,14 @@ export const useUploadStore = create<UploadStore>((set) => ({
|
||||
const file = task.files[fileId]
|
||||
if (!file) return {}
|
||||
|
||||
const wasUploading = file.status === 'uploading'
|
||||
const updatedFile: FileUploadState = { ...file, status: 'completed', loaded: file.total }
|
||||
const updatedFiles = { ...task.files, [fileId]: updatedFile }
|
||||
|
||||
const loadedBytes = Object.values(updatedFiles).reduce((sum, f) => sum + f.loaded, 0)
|
||||
|
||||
return {
|
||||
uploading: wasUploading ? Math.max(0, state.uploading - 1) : state.uploading,
|
||||
tasks: {
|
||||
...state.tasks,
|
||||
[taskId]: {
|
||||
@@ -143,12 +146,14 @@ export const useUploadStore = create<UploadStore>((set) => ({
|
||||
const file = task.files[fileId]
|
||||
if (!file) return {}
|
||||
|
||||
const wasUploading = file.status === 'uploading'
|
||||
const updatedFile: FileUploadState = { ...file, status: 'failed' }
|
||||
const updatedFiles = { ...task.files, [fileId]: updatedFile }
|
||||
|
||||
const loadedBytes = Object.values(updatedFiles).reduce((sum, f) => sum + f.loaded, 0)
|
||||
|
||||
return {
|
||||
uploading: wasUploading ? Math.max(0, state.uploading - 1) : state.uploading,
|
||||
tasks: {
|
||||
...state.tasks,
|
||||
[taskId]: {
|
||||
@@ -163,4 +168,34 @@ export const useUploadStore = create<UploadStore>((set) => ({
|
||||
},
|
||||
}
|
||||
}),
|
||||
|
||||
cancelTask: (taskId) =>
|
||||
set((state) => {
|
||||
const task = state.tasks[taskId]
|
||||
if (!task) return {}
|
||||
|
||||
let decrements = 0
|
||||
const updatedFiles = { ...task.files }
|
||||
const updatedFileProgress = { ...state.fileProgress }
|
||||
|
||||
for (const [fileId, file] of Object.entries(task.files)) {
|
||||
if (file.status === 'uploading') {
|
||||
updatedFiles[fileId] = { ...file, status: 'failed' }
|
||||
updatedFileProgress[fileId] = { ...file, status: 'failed' }
|
||||
decrements++
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
uploading: Math.max(0, state.uploading - decrements),
|
||||
tasks: {
|
||||
...state.tasks,
|
||||
[taskId]: {
|
||||
...task,
|
||||
files: updatedFiles,
|
||||
},
|
||||
},
|
||||
fileProgress: updatedFileProgress,
|
||||
}
|
||||
}),
|
||||
}))
|
||||
|
||||
Reference in New Issue
Block a user