updateBatchProgress 更新批量上传进度
(task *UploadTask, status string, progress float64)
| 479 | |
| 480 | // updateBatchProgress 更新批量上传进度 |
| 481 | func (fp *FileProcessor) updateBatchProgress(task *UploadTask, status string, progress float64) { |
| 482 | if task.BatchID == "" || task.FileID == "" { |
| 483 | return |
| 484 | } |
| 485 | |
| 486 | manager := GetBatchUploadManagerInstance() |
| 487 | if manager.IsBatchCancelled(task.BatchID) { |
| 488 | return |
| 489 | } |
| 490 | if batch, err := manager.GetBatch(task.BatchID); err == nil { |
| 491 | // Fix: Use GetFileUpload for thread-safe access to the map |
| 492 | if fileUploadPtr, exists := batch.GetFileUpload(task.FileID); exists { |
| 493 | // Create a copy to modify, avoiding race conditions on struct fields |
| 494 | fileUpload := *fileUploadPtr |
| 495 | |
| 496 | fileUpload.Status = status |
| 497 | fileUpload.Progress = progress |
| 498 | if status == "failed" { |
| 499 | // 从数据库获取错误信息,如果没有则使用通用错误 |
| 500 | if task.DatabaseID != 0 { |
| 501 | if dbFile, err := model.GetUploadFileByID(task.DatabaseID); err == nil && dbFile != nil && dbFile.Error != "" { |
| 502 | fileUpload.Error = dbFile.Error |
| 503 | } else { |
| 504 | fileUpload.Error = "处理失败" |
| 505 | } |
| 506 | } else { |
| 507 | fileUpload.Error = "处理失败" |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | // 如果task中有DatabaseID和FileID,则使用它们 |
| 512 | if task.DatabaseID != 0 { |
| 513 | fileUpload.DatabaseID = task.DatabaseID |
| 514 | } |
| 515 | if task.FileIDRef != 0 { |
| 516 | fileUpload.FileID = task.FileIDRef |
| 517 | } |
| 518 | |
| 519 | // Pass the address of the modified copy |
| 520 | manager.updateFileProgress(task.BatchID, task.FileID, &fileUpload) |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | // cleanupCancelledUpload 回收取消时已经创建的上传/文件记录 |
| 526 | func (fp *FileProcessor) cleanupCancelledUpload(task *UploadTask, uploadFile *model.UploadFile, fileID int64) { |
no test coverage detected