Compare commits
3 Commits
8ffa0531d6
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
| b100fc9b32 | |||
| d426c16104 | |||
| 993814cdfa |
@@ -40,9 +40,9 @@ const handleConfirm = () => {
|
||||
align-center
|
||||
>
|
||||
<template #header>
|
||||
是否确认清空上传文件
|
||||
是否确认清空需要上传文件
|
||||
</template>
|
||||
<div class="hint-text">以下文件将会被清空并移动到tmp文件夹进行上传,您是否确认</div>
|
||||
<div class="hint-text">以下文件将会被清空并移动到 tmp 文件夹进行上传,您是否确认</div>
|
||||
<div class="dialog-content">
|
||||
<div class="file-list">
|
||||
<div v-for="(file, index) in fileList" :key="index" class="file-item">
|
||||
|
||||
+110
-86
@@ -57,21 +57,23 @@ 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))
|
||||
return true
|
||||
})
|
||||
wailsruntime.EventsEmit(ctx, "progress", pg)
|
||||
|
||||
time.Sleep(250 * time.Millisecond)
|
||||
}
|
||||
|
||||
var pg []Progress
|
||||
progress.Range(func(_, value any) bool {
|
||||
pg = append(pg, value.(Progress))
|
||||
return true
|
||||
})
|
||||
wailsruntime.EventsEmit(ctx, "progress", pg)
|
||||
|
||||
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())
|
||||
@@ -163,41 +177,42 @@ func uploadData(ctx context.Context, logChan *chan string) {
|
||||
|
||||
//检测到文件
|
||||
//统计文件行数
|
||||
var fInfo = make(map[string]fileInfo)
|
||||
|
||||
AddLog(logChan, fmt.Sprintf("正在统计 %v 个文件行数", len(files)))
|
||||
var filesInfo = make(map[string]fileInfo)
|
||||
|
||||
isAllEmpty := true
|
||||
|
||||
AddLog(logChan, fmt.Sprintf("正在统计 %v 个文件行数", len(files)))
|
||||
for _, filePath := range files {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
default:
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
AddLog(logChan, "打开文件失败:"+err.Error())
|
||||
}
|
||||
|
||||
// 使用 bufio.Scanner 逐行读取
|
||||
scanner := bufio.NewScanner(file)
|
||||
lineCount := 0
|
||||
for scanner.Scan() {
|
||||
lineCount++
|
||||
}
|
||||
err = file.Close()
|
||||
|
||||
if lineCount == 0 {
|
||||
continue
|
||||
}
|
||||
fInfo[filepath.Base(filePath)] = fileInfo{
|
||||
FilePath: filePath,
|
||||
FileLines: lineCount,
|
||||
}
|
||||
|
||||
isAllEmpty = false
|
||||
|
||||
AddLog(logChan, fmt.Sprintf("%s 文件行数:%v", filepath.Base(filePath), lineCount))
|
||||
}
|
||||
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
AddLog(logChan, "打开文件失败:"+err.Error())
|
||||
}
|
||||
|
||||
// 使用 bufio.Scanner 逐行读取
|
||||
scanner := bufio.NewScanner(file)
|
||||
lineCount := 0
|
||||
for scanner.Scan() {
|
||||
lineCount++
|
||||
}
|
||||
err = file.Close()
|
||||
|
||||
if lineCount == 0 {
|
||||
continue
|
||||
}
|
||||
filesInfo[filepath.Base(filePath)] = fileInfo{
|
||||
FilePath: filePath,
|
||||
FileLines: lineCount,
|
||||
}
|
||||
|
||||
isAllEmpty = false
|
||||
|
||||
AddLog(logChan, fmt.Sprintf("%s 文件行数:%v", filepath.Base(filePath), lineCount))
|
||||
}
|
||||
|
||||
if isAllEmpty {
|
||||
@@ -207,7 +222,7 @@ func uploadData(ctx context.Context, logChan *chan string) {
|
||||
|
||||
//刷新文件上传进度
|
||||
progress.Clear()
|
||||
for fileName, info := range fInfo {
|
||||
for fileName, info := range filesInfo {
|
||||
progress.Store(fileName,
|
||||
Progress{
|
||||
FileName: fileName,
|
||||
@@ -220,48 +235,53 @@ func uploadData(ctx context.Context, logChan *chan string) {
|
||||
|
||||
// 使用 errgroup 控制同时处理的文件数,并开始上传文件任务
|
||||
g, egctx := errgroup.WithContext(ctx)
|
||||
g.SetLimit(config.APPConfig.HandleFileCount) // 设置同时处理文件数
|
||||
// 设置同时处理文件数
|
||||
g.SetLimit(config.APPConfig.HandleFileCount)
|
||||
// 执行文件上传任务参数(文件路径,文件行数)
|
||||
for fileName, info := range fInfo {
|
||||
for fileName, info := range filesInfo {
|
||||
select {
|
||||
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)
|
||||
|
||||
select {
|
||||
case <-egctx.Done():
|
||||
return egctx.Err()
|
||||
default:
|
||||
//上传完成,删除缓存文件
|
||||
err := os.Remove(info.FilePath)
|
||||
if err != nil {
|
||||
AddLog(logChan, "删除缓存文件失败:"+err.Error())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
g.Go(func() error {
|
||||
select {
|
||||
case <-egctx.Done():
|
||||
return egctx.Err()
|
||||
default:
|
||||
}
|
||||
|
||||
AddLog(logChan, "正在上传文件:"+fileName)
|
||||
|
||||
processFile(egctx, logChan, info.FilePath, info.FileLines)
|
||||
|
||||
select {
|
||||
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()))
|
||||
}
|
||||
|
||||
// 等待所有任务完成
|
||||
g.Wait()
|
||||
|
||||
AddLog(logChan, "所有任务执行完成!")
|
||||
AddLog(logChan, fmt.Sprintf("上传完成,耗时:%s", time.Since(start).String()))
|
||||
}
|
||||
|
||||
// copyFile 快速拷贝文件 src -> dst
|
||||
@@ -335,10 +355,11 @@ func processFile(ctx context.Context, logChan *chan string, filePath string, fil
|
||||
close(lines)
|
||||
return
|
||||
default:
|
||||
go func() {
|
||||
processLines(ctx, logChan, &lines, i, filePath, &countLine)
|
||||
}()
|
||||
}
|
||||
|
||||
go func() {
|
||||
processLines(ctx, logChan, &lines, i, filePath, &countLine)
|
||||
}()
|
||||
}
|
||||
|
||||
// 读取文件并发送到通道
|
||||
@@ -355,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()
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -367,15 +389,16 @@ 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),
|
||||
Total: fileLines,
|
||||
Uploaded: int(countLine),
|
||||
Percentage: int(float64(countLine) / float64(fileLines) * 100),
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
progress.Store(filepath.Base(filePath),
|
||||
Progress{
|
||||
FileName: filepath.Base(filePath),
|
||||
Total: fileLines,
|
||||
Uploaded: int(countLine),
|
||||
Percentage: int(float64(countLine) / float64(fileLines) * 100),
|
||||
},
|
||||
)
|
||||
}
|
||||
//上传完成,进度设为100
|
||||
progress.Store(filepath.Base(filePath),
|
||||
@@ -403,16 +426,17 @@ func processLines(ctx context.Context, logChan *chan string, lines *chan string,
|
||||
case <-ctx.Done():
|
||||
return
|
||||
default:
|
||||
// 跳过空行
|
||||
if strings.TrimSpace(line) == "" {
|
||||
continue
|
||||
}
|
||||
// 上传数据
|
||||
if err := api.UploadDataToServer(ctx, line); err != nil {
|
||||
AddLog(logChan, fmt.Sprintf("Worker %d (文件 %s): 上传失败: %v", workerID, filepath.Base(filePath), err))
|
||||
}
|
||||
atomic.AddInt32(countLine, 1)
|
||||
}
|
||||
|
||||
// 跳过空行
|
||||
if strings.TrimSpace(line) == "" {
|
||||
continue
|
||||
}
|
||||
// 上传数据
|
||||
if err := api.UploadDataToServer(ctx, line); err != nil {
|
||||
AddLog(logChan, fmt.Sprintf("Worker %d (文件 %s): 上传失败: %v", workerID, filepath.Base(filePath), err))
|
||||
}
|
||||
atomic.AddInt32(countLine, 1)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user