hejianhao
2026-08-20 2f5f94f57693c8e056ee89c505bb5d784ecddc1d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.tcm.training.admin.upload;
 
import com.obs.services.model.ProgressStatus;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
 
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
 
@Service
public class UploadProgressService {
 
    private final ConcurrentMap<String, UploadProgressInfo> progressMap = new ConcurrentHashMap<>();
 
    public void init(String taskId, long totalBytes) {
        if (!StringUtils.hasText(taskId)) {
            return;
        }
        UploadProgressInfo info = new UploadProgressInfo();
        info.setTaskId(taskId);
        info.setStatus(UploadProgressInfo.STATUS_UPLOADING);
        info.setPercent(0);
        info.setTransferredBytes(0L);
        info.setTotalBytes(Math.max(totalBytes, 0L));
        progressMap.put(taskId, info);
    }
 
    public void update(String taskId, ProgressStatus status) {
        if (!StringUtils.hasText(taskId) || status == null) {
            return;
        }
        UploadProgressInfo info = progressMap.computeIfAbsent(taskId, this::createUploadingInfo);
        info.setStatus(UploadProgressInfo.STATUS_UPLOADING);
        info.setPercent(status.getTransferPercentage());
        info.setTransferredBytes(status.getTransferredBytes());
        if (status.getTotalBytes() > 0) {
            info.setTotalBytes(status.getTotalBytes());
        }
    }
 
    public void markSuccess(String taskId, UploadFileResult result, long totalBytes) {
        if (!StringUtils.hasText(taskId)) {
            return;
        }
        UploadProgressInfo info = progressMap.computeIfAbsent(taskId, this::createUploadingInfo);
        info.setStatus(UploadProgressInfo.STATUS_SUCCESS);
        info.setPercent(100);
        info.setTotalBytes(Math.max(totalBytes, 0L));
        info.setTransferredBytes(Math.max(totalBytes, 0L));
        info.setUrl(result.getUrl());
        info.setFileName(result.getFileName());
    }
 
    public void markFailed(String taskId, String errorMessage) {
        if (!StringUtils.hasText(taskId)) {
            return;
        }
        UploadProgressInfo info = progressMap.computeIfAbsent(taskId, this::createUploadingInfo);
        info.setStatus(UploadProgressInfo.STATUS_FAILED);
        info.setErrorMessage(errorMessage);
    }
 
    public UploadProgressInfo get(String taskId) {
        if (!StringUtils.hasText(taskId)) {
            throw new IllegalArgumentException("上传任务ID不能为空");
        }
        return progressMap.getOrDefault(taskId, createUploadingInfo(taskId));
    }
 
    private UploadProgressInfo createUploadingInfo(String taskId) {
        UploadProgressInfo info = new UploadProgressInfo();
        info.setTaskId(taskId);
        info.setStatus(UploadProgressInfo.STATUS_UPLOADING);
        info.setPercent(0);
        info.setTransferredBytes(0L);
        info.setTotalBytes(0L);
        return info;
    }
}