refactor(uploader): 优化上传器代码结构和上下文取消处理

- 在进度更新循环中添加上下文取消检查点
- 在文件复制操作前添加上下文取消检查点
- 重构代码缩进和括号位置以提高可读性
- 优化 goroutine 中的上下文取消处理逻辑
- 统一代码块的括号格式和缩进风格
This commit is contained in:
2026-05-01 22:47:39 +08:00
parent 993814cdfa
commit d426c16104
+33 -10
View File
@@ -57,6 +57,8 @@ func StartUpload(ctx context.Context, logChan *chan string) {
case <-ctx.Done():
return
default:
}
var pg []Progress
progress.Range(func(_, value any) bool {
pg = append(pg, value.(Progress))
@@ -66,12 +68,12 @@ func StartUpload(ctx context.Context, logChan *chan string) {
time.Sleep(250 * time.Millisecond)
}
}
}()
//开启上传程序
for {
uploadData(ctx, logChan)
select {
case <-ctx.Done():
return
@@ -120,6 +122,12 @@ func uploadData(ctx context.Context, logChan *chan string) {
if config.APPConfig.ClearFilesNoPrompt {
//不用提示直接复制文件到tmp
for _, p := range f {
select {
case <-ctx.Done():
return
default:
}
err := copyFile(p, "./tmp/"+filepath.Base(p))
if err != nil {
AddLog(logChan, "复制文件失败:"+err.Error())
@@ -143,6 +151,12 @@ func uploadData(ctx context.Context, logChan *chan string) {
if <-confirm {
for _, p := range f {
select {
case <-ctx.Done():
return
default:
}
err := copyFile(p, "./tmp/"+filepath.Base(p))
if err != nil {
AddLog(logChan, "复制文件失败:"+err.Error())
@@ -173,6 +187,8 @@ func uploadData(ctx context.Context, logChan *chan string) {
case <-ctx.Done():
return
default:
}
file, err := os.Open(filePath)
if err != nil {
AddLog(logChan, "打开文件失败:"+err.Error())
@@ -198,7 +214,6 @@ func uploadData(ctx context.Context, logChan *chan string) {
AddLog(logChan, fmt.Sprintf("%s 文件行数:%v", filepath.Base(filePath), lineCount))
}
}
if isAllEmpty {
AddLog(logChan, "所有文件都为空,不进行上传")
@@ -228,11 +243,15 @@ func uploadData(ctx context.Context, logChan *chan string) {
case <-egctx.Done():
return
default:
}
g.Go(func() error {
select {
case <-egctx.Done():
return egctx.Err()
default:
}
AddLog(logChan, "正在上传文件:"+fileName)
processFile(egctx, logChan, info.FilePath, info.FileLines)
@@ -241,29 +260,29 @@ func uploadData(ctx context.Context, logChan *chan string) {
case <-egctx.Done():
return egctx.Err()
default:
}
//上传完成,删除缓存文件
err := os.Remove(info.FilePath)
if err != nil {
AddLog(logChan, "删除缓存文件失败:"+err.Error())
}
return nil
}
}
})
}
}
select {
case <-ctx.Done():
return
default:
}
// 等待所有任务完成
g.Wait()
AddLog(logChan, "所有任务执行完成!")
AddLog(logChan, fmt.Sprintf("上传完成,耗时:%s", time.Since(start).String()))
}
}
// copyFile 快速拷贝文件 src -> dst
func copyFile(src, dst string) error {
@@ -336,11 +355,12 @@ func processFile(ctx context.Context, logChan *chan string, filePath string, fil
close(lines)
return
default:
}
go func() {
processLines(ctx, logChan, &lines, i, filePath, &countLine)
}()
}
}
// 读取文件并发送到通道
scanner := bufio.NewScanner(file)
@@ -356,8 +376,9 @@ func processFile(ctx context.Context, logChan *chan string, filePath string, fil
case <-ctx.Done():
return
default:
lines <- scanner.Text()
}
lines <- scanner.Text()
}
}()
@@ -368,6 +389,8 @@ func processFile(ctx context.Context, logChan *chan string, filePath string, fil
close(lines) //关闭processLines中的上传线程
return
default:
}
progress.Store(filepath.Base(filePath),
Progress{
FileName: filepath.Base(filePath),
@@ -377,7 +400,6 @@ func processFile(ctx context.Context, logChan *chan string, filePath string, fil
},
)
}
}
//上传完成,进度设为100
progress.Store(filepath.Base(filePath),
Progress{
@@ -404,6 +426,8 @@ func processLines(ctx context.Context, logChan *chan string, lines *chan string,
case <-ctx.Done():
return
default:
}
// 跳过空行
if strings.TrimSpace(line) == "" {
continue
@@ -415,7 +439,6 @@ func processLines(ctx context.Context, logChan *chan string, lines *chan string,
atomic.AddInt32(countLine, 1)
}
}
}
// AddLog 添加日志
func AddLog(logChan *chan string, message string) {