Compare commits
5 Commits
03e4e6f45b
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| b100fc9b32 | |||
| d426c16104 | |||
| 993814cdfa | |||
| 8ffa0531d6 | |||
| 3f6e999783 |
@@ -11,6 +11,7 @@
|
|||||||
"type-check": "vue-tsc --build"
|
"type-check": "vue-tsc --build"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@element-plus/icons-vue": "^2.3.2",
|
||||||
"element-plus": "^2.13.7",
|
"element-plus": "^2.13.7",
|
||||||
"vue": "^3.5.32"
|
"vue": "^3.5.32"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
aa914e6b4676ee4621ced7ad6d81c58c
|
05225657934ff66d822c925754c951bf
|
||||||
Generated
+3
@@ -8,6 +8,9 @@ importers:
|
|||||||
|
|
||||||
.:
|
.:
|
||||||
dependencies:
|
dependencies:
|
||||||
|
'@element-plus/icons-vue':
|
||||||
|
specifier: ^2.3.2
|
||||||
|
version: 2.3.2(vue@3.5.33(typescript@6.0.3))
|
||||||
element-plus:
|
element-plus:
|
||||||
specifier: ^2.13.7
|
specifier: ^2.13.7
|
||||||
version: 2.13.7(typescript@6.0.3)(vue@3.5.33(typescript@6.0.3))
|
version: 2.13.7(typescript@6.0.3)(vue@3.5.33(typescript@6.0.3))
|
||||||
|
|||||||
+34
-11
@@ -1,11 +1,12 @@
|
|||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import {ref, nextTick, computed} from 'vue'
|
import {computed, nextTick, ref} from 'vue'
|
||||||
import {ElMessage} from 'element-plus'
|
import {ElMessage, ElMessageBox} from 'element-plus'
|
||||||
import {ElMessageBox} from 'element-plus'
|
import ConfirmClearDialog from './components/ConfirmClearDialog.vue'
|
||||||
import {SelectPath, GetConfig, WriteConfig, StartUpload, StopUpload} from '../wailsjs/go/main/App';
|
import {GetConfig, SelectPath, StartUpload, StopUpload, WriteConfig} from '../wailsjs/go/main/App';
|
||||||
import {config} from "../wailsjs/go/models.ts";
|
import {config} from "../wailsjs/go/models.ts";
|
||||||
import Config = config.Config;
|
|
||||||
import {EventsOn, LogPrint} from "../wailsjs/runtime";
|
import {EventsOn, LogPrint} from "../wailsjs/runtime";
|
||||||
|
import {configModel} from "@/model.ts";
|
||||||
|
import Config = config.Config;
|
||||||
|
|
||||||
// const serverUrl = ref('')
|
// const serverUrl = ref('')
|
||||||
const token = ref('')
|
const token = ref('')
|
||||||
@@ -19,6 +20,10 @@ const logOutput = ref<string[]>([])
|
|||||||
const logContentRef = ref<HTMLElement>()
|
const logContentRef = ref<HTMLElement>()
|
||||||
const logRoll = ref(true)
|
const logRoll = ref(true)
|
||||||
|
|
||||||
|
const clearDialogVisible = ref(false)
|
||||||
|
const filesToClear = ref<string[]>([])
|
||||||
|
const noPromptClear = ref(false)
|
||||||
|
|
||||||
interface FileProgress {
|
interface FileProgress {
|
||||||
name: string
|
name: string
|
||||||
uploaded: number
|
uploaded: number
|
||||||
@@ -100,16 +105,16 @@ const clearLog = () => {
|
|||||||
// WriteConfig("url", serverUrl.value)
|
// WriteConfig("url", serverUrl.value)
|
||||||
// }
|
// }
|
||||||
const writeToken = () => {
|
const writeToken = () => {
|
||||||
WriteConfig("token", token.value)
|
WriteConfig(configModel.Token, token.value)
|
||||||
}
|
}
|
||||||
const writeCheckDir = () => {
|
const writeCheckDir = () => {
|
||||||
WriteConfig("check-dir", checkDir.value)
|
WriteConfig(configModel.CheckDir, checkDir.value)
|
||||||
}
|
}
|
||||||
const writeConcurrentFiles = () => {
|
const writeConcurrentFiles = () => {
|
||||||
WriteConfig("handle-file-count", concurrentFiles.value)
|
WriteConfig(configModel.HandleFileCount, concurrentFiles.value)
|
||||||
}
|
}
|
||||||
const writeUploadThreads = () => {
|
const writeUploadThreads = () => {
|
||||||
WriteConfig("thread-count", uploadThreads.value)
|
WriteConfig(configModel.ThreadCount, uploadThreads.value)
|
||||||
}
|
}
|
||||||
// const writeAutoStart = () => {
|
// const writeAutoStart = () => {
|
||||||
// WriteConfig("is-run-on-start", autoStart.value)
|
// WriteConfig("is-run-on-start", autoStart.value)
|
||||||
@@ -124,6 +129,7 @@ try {
|
|||||||
concurrentFiles.value = config.handle_file_count
|
concurrentFiles.value = config.handle_file_count
|
||||||
uploadThreads.value = config.thread_count
|
uploadThreads.value = config.thread_count
|
||||||
// autoStart.value = config.is_run_on_start
|
// autoStart.value = config.is_run_on_start
|
||||||
|
noPromptClear.value = config.clear_files_no_prompt
|
||||||
|
|
||||||
LogPrint(`[${new Date().toLocaleString()}] 配置已加载`)
|
LogPrint(`[${new Date().toLocaleString()}] 配置已加载`)
|
||||||
})
|
})
|
||||||
@@ -131,6 +137,7 @@ try {
|
|||||||
console.log(e)
|
console.log(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
EventsOn("is-run", (run) => {
|
EventsOn("is-run", (run) => {
|
||||||
isRunning.value = run
|
isRunning.value = run
|
||||||
})
|
})
|
||||||
@@ -140,6 +147,15 @@ EventsOn("progress", (progress) => {
|
|||||||
EventsOn("log", (msg) => {
|
EventsOn("log", (msg) => {
|
||||||
addLog(msg)
|
addLog(msg)
|
||||||
})
|
})
|
||||||
|
EventsOn("clear-files", (files) => {
|
||||||
|
filesToClear.value = files
|
||||||
|
if (!noPromptClear.value) {
|
||||||
|
clearDialogVisible.value = true
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} catch (e) {
|
||||||
|
console.log(e)
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
@@ -165,12 +181,14 @@ EventsOn("log", (msg) => {
|
|||||||
|
|
||||||
<div class="form-item">
|
<div class="form-item">
|
||||||
<label>同时处理文件数</label>
|
<label>同时处理文件数</label>
|
||||||
<el-input-number v-model="concurrentFiles" :min="1" :max="100" :disabled="isRunning" @change="writeConcurrentFiles()"/>
|
<el-input-number v-model="concurrentFiles" :min="1" :max="100" :disabled="isRunning"
|
||||||
|
@change="writeConcurrentFiles()"/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-item">
|
<div class="form-item">
|
||||||
<label>单文件上传线程</label>
|
<label>单文件上传线程</label>
|
||||||
<el-input-number v-model="uploadThreads" :min="1" :max="100" :disabled="isRunning" @change="writeUploadThreads()"/>
|
<el-input-number v-model="uploadThreads" :min="1" :max="100" :disabled="isRunning"
|
||||||
|
@change="writeUploadThreads()"/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- <div class="form-item">-->
|
<!-- <div class="form-item">-->
|
||||||
@@ -205,6 +223,11 @@ EventsOn("log", (msg) => {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<ConfirmClearDialog
|
||||||
|
v-model:visible="clearDialogVisible"
|
||||||
|
:file-list="filesToClear"
|
||||||
|
/>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<style scoped>
|
<style scoped>
|
||||||
|
|||||||
@@ -0,0 +1,124 @@
|
|||||||
|
<script lang="ts" setup>
|
||||||
|
import {ref} from 'vue'
|
||||||
|
import {WriteConfig} from "../../wailsjs/go/main/App";
|
||||||
|
import {configModel} from "@/model.ts";
|
||||||
|
import {EventsEmit} from "../../wailsjs/runtime";
|
||||||
|
import {InfoFilled} from '@element-plus/icons-vue'
|
||||||
|
|
||||||
|
const props = defineProps<{
|
||||||
|
visible: boolean
|
||||||
|
fileList: string[]
|
||||||
|
}>()
|
||||||
|
|
||||||
|
const emit = defineEmits<{
|
||||||
|
(e: 'update:visible', value: boolean): void
|
||||||
|
// (e: 'confirm', dontShowAgain: boolean): void
|
||||||
|
}>()
|
||||||
|
|
||||||
|
const dontShowAgain = ref(false)
|
||||||
|
|
||||||
|
const handleClose = () => {
|
||||||
|
emit('update:visible', false)
|
||||||
|
EventsEmit('confirm-clear-files', false)
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleConfirm = () => {
|
||||||
|
// emit('confirm', dontShowAgain.value)
|
||||||
|
emit('update:visible', false)
|
||||||
|
WriteConfig(configModel.ClearFilesNoPrompt, dontShowAgain.value)
|
||||||
|
EventsEmit('confirm-clear-files', true)
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<el-dialog
|
||||||
|
:model-value="visible"
|
||||||
|
width="600px"
|
||||||
|
:close-on-click-modal="false"
|
||||||
|
@update:model-value="(val: boolean) => emit('update:visible', val)"
|
||||||
|
@close="handleClose"
|
||||||
|
align-center
|
||||||
|
>
|
||||||
|
<template #header>
|
||||||
|
是否确认清空需要上传文件
|
||||||
|
</template>
|
||||||
|
<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">
|
||||||
|
{{ file }}
|
||||||
|
</div>
|
||||||
|
<div v-if="fileList.length === 0" class="empty-text">暂无文件</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<template #footer>
|
||||||
|
<div class="dialog-footer">
|
||||||
|
<div class="dialog-footer-checkbox-container">
|
||||||
|
<el-checkbox v-model="dontShowAgain" label="下次清空文件不再弹出此弹窗确认"/>
|
||||||
|
<el-tooltip content="此弹窗会在首次运行时弹出,您可以选择下次不再弹出此弹窗">
|
||||||
|
<el-icon>
|
||||||
|
<InfoFilled/>
|
||||||
|
</el-icon>
|
||||||
|
</el-tooltip>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<el-button @click="handleClose">取消</el-button>
|
||||||
|
<el-button type="primary" @click="handleConfirm">确认</el-button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
</el-dialog>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.dialog-footer-checkbox-container {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.dialog-footer-checkbox-container .el-icon {
|
||||||
|
color: #909399;
|
||||||
|
}
|
||||||
|
|
||||||
|
.dialog-content {
|
||||||
|
padding: 10px 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.hint-text {
|
||||||
|
font-size: 12px;
|
||||||
|
color: #909399;
|
||||||
|
}
|
||||||
|
|
||||||
|
.file-list {
|
||||||
|
max-height: 350px;
|
||||||
|
overflow-y: auto;
|
||||||
|
border: 1px solid #e4e7ed;
|
||||||
|
border-radius: 4px;
|
||||||
|
padding: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.file-item {
|
||||||
|
padding: 6px 8px;
|
||||||
|
font-size: 14px;
|
||||||
|
color: #606266;
|
||||||
|
border-bottom: 1px solid #f0f0f0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.file-item:last-child {
|
||||||
|
border-bottom: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.empty-text {
|
||||||
|
text-align: center;
|
||||||
|
color: #909399;
|
||||||
|
padding: 20px 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.dialog-footer {
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
export enum configModel {
|
||||||
|
Url = "url",
|
||||||
|
Token = "token",
|
||||||
|
ThreadCount = "thread-count",
|
||||||
|
HandleFileCount = "handle-file-count",
|
||||||
|
IsRunOnStart = "is-run-on-start",
|
||||||
|
CheckDir = "check-dir",
|
||||||
|
ClearFilesNoPrompt = "clear-files-no-prompt",
|
||||||
|
}
|
||||||
@@ -7,6 +7,7 @@ export namespace config {
|
|||||||
handle_file_count: number;
|
handle_file_count: number;
|
||||||
is_run_on_start: boolean;
|
is_run_on_start: boolean;
|
||||||
check_dir: string;
|
check_dir: string;
|
||||||
|
clear_files_no_prompt: boolean;
|
||||||
|
|
||||||
static createFrom(source: any = {}) {
|
static createFrom(source: any = {}) {
|
||||||
return new Config(source);
|
return new Config(source);
|
||||||
@@ -20,6 +21,7 @@ export namespace config {
|
|||||||
this.handle_file_count = source["handle_file_count"];
|
this.handle_file_count = source["handle_file_count"];
|
||||||
this.is_run_on_start = source["is_run_on_start"];
|
this.is_run_on_start = source["is_run_on_start"];
|
||||||
this.check_dir = source["check_dir"];
|
this.check_dir = source["check_dir"];
|
||||||
|
this.clear_files_no_prompt = source["clear_files_no_prompt"];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ type Config struct {
|
|||||||
HandleFileCount int `json:"handle_file_count" mapstructure:"handle-file-count"`
|
HandleFileCount int `json:"handle_file_count" mapstructure:"handle-file-count"`
|
||||||
IsRunOnStart bool `json:"is_run_on_start" mapstructure:"is-run-on-start"`
|
IsRunOnStart bool `json:"is_run_on_start" mapstructure:"is-run-on-start"`
|
||||||
CheckDir string `json:"check_dir" mapstructure:"check-dir"`
|
CheckDir string `json:"check_dir" mapstructure:"check-dir"`
|
||||||
|
ClearFilesNoPrompt bool `json:"clear_files_no_prompt" mapstructure:"clear-files-no-prompt"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var APPConfig Config
|
var APPConfig Config
|
||||||
@@ -27,6 +28,7 @@ const (
|
|||||||
HandleFileCount = "handle-file-count"
|
HandleFileCount = "handle-file-count"
|
||||||
IsRunOnStart = "is-run-on-start"
|
IsRunOnStart = "is-run-on-start"
|
||||||
CheckDir = "check-dir"
|
CheckDir = "check-dir"
|
||||||
|
ClearFilesNoPrompt = "clear-files-no-prompt"
|
||||||
)
|
)
|
||||||
|
|
||||||
func InitConfig() {
|
func InitConfig() {
|
||||||
@@ -38,6 +40,7 @@ func InitConfig() {
|
|||||||
HandleFileCount: 25,
|
HandleFileCount: 25,
|
||||||
IsRunOnStart: false,
|
IsRunOnStart: false,
|
||||||
CheckDir: "",
|
CheckDir: "",
|
||||||
|
ClearFilesNoPrompt: false,
|
||||||
}
|
}
|
||||||
viper.SetDefault(Url, defaultConfig.Url)
|
viper.SetDefault(Url, defaultConfig.Url)
|
||||||
viper.SetDefault(Token, defaultConfig.Token)
|
viper.SetDefault(Token, defaultConfig.Token)
|
||||||
@@ -45,6 +48,7 @@ func InitConfig() {
|
|||||||
viper.SetDefault(HandleFileCount, defaultConfig.HandleFileCount)
|
viper.SetDefault(HandleFileCount, defaultConfig.HandleFileCount)
|
||||||
viper.SetDefault(IsRunOnStart, defaultConfig.IsRunOnStart)
|
viper.SetDefault(IsRunOnStart, defaultConfig.IsRunOnStart)
|
||||||
viper.SetDefault(CheckDir, defaultConfig.CheckDir)
|
viper.SetDefault(CheckDir, defaultConfig.CheckDir)
|
||||||
|
viper.SetDefault(ClearFilesNoPrompt, defaultConfig.ClearFilesNoPrompt)
|
||||||
|
|
||||||
//设置配置文件名和路径 ./config.toml
|
//设置配置文件名和路径 ./config.toml
|
||||||
viper.AddConfigPath(".")
|
viper.AddConfigPath(".")
|
||||||
|
|||||||
+161
-35
@@ -7,6 +7,7 @@ import (
|
|||||||
"dypid-client/internal/api"
|
"dypid-client/internal/api"
|
||||||
"dypid-client/internal/config"
|
"dypid-client/internal/config"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
@@ -20,7 +21,7 @@ import (
|
|||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Task struct {
|
type fileInfo struct {
|
||||||
FilePath string
|
FilePath string
|
||||||
FileLines int
|
FileLines int
|
||||||
}
|
}
|
||||||
@@ -56,6 +57,8 @@ func StartUpload(ctx context.Context, logChan *chan string) {
|
|||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
var pg []Progress
|
var pg []Progress
|
||||||
progress.Range(func(_, value any) bool {
|
progress.Range(func(_, value any) bool {
|
||||||
pg = append(pg, value.(Progress))
|
pg = append(pg, value.(Progress))
|
||||||
@@ -65,12 +68,12 @@ func StartUpload(ctx context.Context, logChan *chan string) {
|
|||||||
|
|
||||||
time.Sleep(250 * time.Millisecond)
|
time.Sleep(250 * time.Millisecond)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
//开启上传程序
|
//开启上传程序
|
||||||
for {
|
for {
|
||||||
uploadData(ctx, logChan)
|
uploadData(ctx, logChan)
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
@@ -80,34 +83,112 @@ func StartUpload(ctx context.Context, logChan *chan string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func uploadData(ctx context.Context, logChan *chan string) {
|
func uploadData(ctx context.Context, logChan *chan string) {
|
||||||
|
start := time.Now()
|
||||||
|
|
||||||
// 获取检测目录
|
// 获取检测目录
|
||||||
var path = "./"
|
var checkPath = "./"
|
||||||
if config.APPConfig.CheckDir != "" {
|
if config.APPConfig.CheckDir != "" {
|
||||||
path = config.APPConfig.CheckDir
|
checkPath = config.APPConfig.CheckDir
|
||||||
}
|
}
|
||||||
|
|
||||||
//获取文件列表
|
//要上传的文件路径字符串数组
|
||||||
files, err := getTxtFiles(path)
|
var files []string
|
||||||
|
|
||||||
|
//先检测tmp目录有没有残余文件
|
||||||
|
os.Mkdir("./tmp", os.ModePerm)
|
||||||
|
tmpFiles, err := getTxtFiles("./tmp")
|
||||||
|
if err != nil {
|
||||||
|
AddLog(logChan, "获取 tmp 文件列表失败:"+err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
//tmp有文件,优先上传(else:tmp没文件扫描指定文件夹,并复制文件到tmp)
|
||||||
|
if tmpFiles != nil {
|
||||||
|
AddLog(logChan, "当前 tmp 目录下还有未上传完成文件,将优先上传 tmp 目录文件")
|
||||||
|
files = tmpFiles
|
||||||
|
} else {
|
||||||
|
//tmp没文件,扫描指定文件夹
|
||||||
|
f, err := getTxtFiles(checkPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
AddLog(logChan, "获取文件列表失败:"+err.Error())
|
AddLog(logChan, "获取文件列表失败:"+err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if files == nil {
|
|
||||||
|
//指定文件夹没文件,退出函数
|
||||||
|
if f == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
start := time.Now()
|
//是否向用户提示清空文件,并复制文件到tmp
|
||||||
|
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())
|
||||||
|
} else {
|
||||||
|
files = append(files, "./tmp/"+filepath.Base(p))
|
||||||
|
err := os.Truncate(p, 0)
|
||||||
|
if err != nil {
|
||||||
|
AddLog(logChan, "清空文件失败:"+err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
//提示用户
|
||||||
|
wailsruntime.EventsEmit(ctx, "clear-files", f)
|
||||||
|
|
||||||
|
confirm := make(chan bool)
|
||||||
|
|
||||||
|
wailsruntime.EventsOn(ctx, "confirm-clear-files", func(optionalData ...any) {
|
||||||
|
confirm <- optionalData[0].(bool)
|
||||||
|
})
|
||||||
|
|
||||||
|
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())
|
||||||
|
} else {
|
||||||
|
files = append(files, "./tmp/"+filepath.Base(p))
|
||||||
|
err := os.Truncate(p, 0)
|
||||||
|
if err != nil {
|
||||||
|
AddLog(logChan, "清空文件失败:"+err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
AddLog(logChan, "已取消上传,1分钟后再运行")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//检测到文件
|
//检测到文件
|
||||||
//统计文件行数
|
//统计文件行数
|
||||||
fileLines := make(map[string]int)
|
var filesInfo = make(map[string]fileInfo)
|
||||||
AddLog(logChan, fmt.Sprintf("正在统计 %v 个文件行数", len(files)))
|
|
||||||
isAllEmpty := true
|
isAllEmpty := true
|
||||||
|
|
||||||
|
AddLog(logChan, fmt.Sprintf("正在统计 %v 个文件行数", len(files)))
|
||||||
for _, filePath := range files {
|
for _, filePath := range files {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
file, err := os.Open(filePath)
|
file, err := os.Open(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
AddLog(logChan, "打开文件失败:"+err.Error())
|
AddLog(logChan, "打开文件失败:"+err.Error())
|
||||||
@@ -124,10 +205,14 @@ func uploadData(ctx context.Context, logChan *chan string) {
|
|||||||
if lineCount == 0 {
|
if lineCount == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
fileLines[filepath.Base(filePath)] = lineCount
|
filesInfo[filepath.Base(filePath)] = fileInfo{
|
||||||
isAllEmpty = false
|
FilePath: filePath,
|
||||||
AddLog(logChan, fmt.Sprintf("%s 文件行数:%v", filepath.Base(filePath), lineCount))
|
FileLines: lineCount,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
isAllEmpty = false
|
||||||
|
|
||||||
|
AddLog(logChan, fmt.Sprintf("%s 文件行数:%v", filepath.Base(filePath), lineCount))
|
||||||
}
|
}
|
||||||
|
|
||||||
if isAllEmpty {
|
if isAllEmpty {
|
||||||
@@ -135,62 +220,97 @@ func uploadData(ctx context.Context, logChan *chan string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//刷新文件上传进度
|
||||||
progress.Clear()
|
progress.Clear()
|
||||||
|
for fileName, info := range filesInfo {
|
||||||
//添加文件上传任务参数(文件路径,文件行数)
|
progress.Store(fileName,
|
||||||
var tasks []Task
|
Progress{
|
||||||
for fileName, lines := range fileLines {
|
FileName: fileName,
|
||||||
tasks = append(tasks, Task{FilePath: path + "/" + fileName, FileLines: lines})
|
Total: info.FileLines,
|
||||||
progress.Store(fileName, Progress{FileName: fileName, Total: lines, Uploaded: 0, Percentage: 0})
|
Uploaded: 0,
|
||||||
|
Percentage: 0,
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 使用 errgroup 控制同时处理的文件数,并开始上传文件任务
|
// 使用 errgroup 控制同时处理的文件数,并开始上传文件任务
|
||||||
g, egctx := errgroup.WithContext(ctx)
|
g, egctx := errgroup.WithContext(ctx)
|
||||||
g.SetLimit(config.APPConfig.HandleFileCount) // 设置同时处理文件数
|
// 设置同时处理文件数
|
||||||
// 执行所有任务
|
g.SetLimit(config.APPConfig.HandleFileCount)
|
||||||
for _, task := range tasks {
|
// 执行文件上传任务参数(文件路径,文件行数)
|
||||||
|
for fileName, info := range filesInfo {
|
||||||
select {
|
select {
|
||||||
case <-egctx.Done():
|
case <-egctx.Done():
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
g.Go(func() error {
|
g.Go(func() error {
|
||||||
select {
|
select {
|
||||||
case <-egctx.Done():
|
case <-egctx.Done():
|
||||||
return egctx.Err()
|
return egctx.Err()
|
||||||
default:
|
default:
|
||||||
AddLog(logChan, "正在上传文件:"+filepath.Base(task.FilePath))
|
}
|
||||||
|
|
||||||
processFile(egctx, logChan, task.FilePath, task.FileLines)
|
AddLog(logChan, "正在上传文件:"+fileName)
|
||||||
|
|
||||||
|
processFile(egctx, logChan, info.FilePath, info.FileLines)
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-egctx.Done():
|
case <-egctx.Done():
|
||||||
return egctx.Err()
|
return egctx.Err()
|
||||||
default:
|
default:
|
||||||
//上传完成,清空文件
|
}
|
||||||
err := os.Truncate(task.FilePath, 0)
|
|
||||||
|
//上传完成,删除缓存文件
|
||||||
|
err := os.Remove(info.FilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
AddLog(logChan, "清空文件失败:"+err.Error())
|
AddLog(logChan, "删除缓存文件失败:"+err.Error())
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
// 等待所有任务完成
|
// 等待所有任务完成
|
||||||
g.Wait()
|
g.Wait()
|
||||||
|
|
||||||
AddLog(logChan, "所有任务执行完成!")
|
AddLog(logChan, "所有任务执行完成!")
|
||||||
AddLog(logChan, fmt.Sprintf("上传完成,耗时:%s", time.Since(start).String()))
|
AddLog(logChan, fmt.Sprintf("上传完成,耗时:%s", time.Since(start).String()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// copyFile 快速拷贝文件 src -> dst
|
||||||
|
func copyFile(src, dst string) error {
|
||||||
|
// 打开源文件
|
||||||
|
sourceFile, err := os.Open(src)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer sourceFile.Close()
|
||||||
|
|
||||||
|
// 创建目标文件
|
||||||
|
destFile, err := os.Create(dst)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer destFile.Close()
|
||||||
|
|
||||||
|
// 核心:最快拷贝,底层使用操作系统零拷贝技术
|
||||||
|
_, err = io.Copy(destFile, sourceFile)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取目录中的所有txt文件
|
// 强制刷入磁盘,保证数据完整
|
||||||
|
return destFile.Sync()
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取目录中的所有txt文件(文件大小为0的不返回)
|
||||||
func getTxtFiles(dir string) (txtFiles []string, err error) {
|
func getTxtFiles(dir string) (txtFiles []string, err error) {
|
||||||
err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -204,8 +324,10 @@ func getTxtFiles(dir string) (txtFiles []string, err error) {
|
|||||||
|
|
||||||
// 检查文件扩展名是否为.txt
|
// 检查文件扩展名是否为.txt
|
||||||
if strings.ToLower(filepath.Ext(path)) == ".txt" {
|
if strings.ToLower(filepath.Ext(path)) == ".txt" {
|
||||||
|
if info.Size() != 0 {
|
||||||
txtFiles = append(txtFiles, path)
|
txtFiles = append(txtFiles, path)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
@@ -233,11 +355,12 @@ func processFile(ctx context.Context, logChan *chan string, filePath string, fil
|
|||||||
close(lines)
|
close(lines)
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
processLines(ctx, logChan, &lines, i, filePath, &countLine)
|
processLines(ctx, logChan, &lines, i, filePath, &countLine)
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// 读取文件并发送到通道
|
// 读取文件并发送到通道
|
||||||
scanner := bufio.NewScanner(file)
|
scanner := bufio.NewScanner(file)
|
||||||
@@ -253,8 +376,9 @@ func processFile(ctx context.Context, logChan *chan string, filePath string, fil
|
|||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
lines <- scanner.Text()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lines <- scanner.Text()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@@ -265,6 +389,8 @@ func processFile(ctx context.Context, logChan *chan string, filePath string, fil
|
|||||||
close(lines) //关闭processLines中的上传线程
|
close(lines) //关闭processLines中的上传线程
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
progress.Store(filepath.Base(filePath),
|
progress.Store(filepath.Base(filePath),
|
||||||
Progress{
|
Progress{
|
||||||
FileName: filepath.Base(filePath),
|
FileName: filepath.Base(filePath),
|
||||||
@@ -274,7 +400,6 @@ func processFile(ctx context.Context, logChan *chan string, filePath string, fil
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
//上传完成,进度设为100
|
//上传完成,进度设为100
|
||||||
progress.Store(filepath.Base(filePath),
|
progress.Store(filepath.Base(filePath),
|
||||||
Progress{
|
Progress{
|
||||||
@@ -301,6 +426,8 @@ func processLines(ctx context.Context, logChan *chan string, lines *chan string,
|
|||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
// 跳过空行
|
// 跳过空行
|
||||||
if strings.TrimSpace(line) == "" {
|
if strings.TrimSpace(line) == "" {
|
||||||
continue
|
continue
|
||||||
@@ -312,7 +439,6 @@ func processLines(ctx context.Context, logChan *chan string, lines *chan string,
|
|||||||
atomic.AddInt32(countLine, 1)
|
atomic.AddInt32(countLine, 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// AddLog 添加日志
|
// AddLog 添加日志
|
||||||
func AddLog(logChan *chan string, message string) {
|
func AddLog(logChan *chan string, message string) {
|
||||||
|
|||||||
Reference in New Issue
Block a user