Compare commits

..

10 Commits

Author SHA1 Message Date
ygxbnet b100fc9b32 fix(upload): 修正上传文件清空确认对话框文本
构建上传工具 / build (push) Successful in 1m51s
- 修正了标题文本"是否确认清空需要上传文件"
- 修正了提示文本中的"tmp"文件夹显示格式
- 更新了确认对话框的内容描述文本
2026-05-01 22:51:12 +08:00
ygxbnet d426c16104 refactor(uploader): 优化上传器代码结构和上下文取消处理
- 在进度更新循环中添加上下文取消检查点
- 在文件复制操作前添加上下文取消检查点
- 重构代码缩进和括号位置以提高可读性
- 优化 goroutine 中的上下文取消处理逻辑
- 统一代码块的括号格式和缩进风格
2026-05-01 22:47:39 +08:00
ygxbnet 993814cdfa refactor(uploader): 优化文件信息统计逻辑
- 将变量名 fInfo 重命名为 filesInfo 以提高可读性
- 调整代码顺序,将 AddLog 调用移到变量声明后
- 统一使用新变量名在所有相关位置进行引用
- 移动 g.SetLimit 注释位置以提高代码可读性
2026-05-01 22:37:45 +08:00
ygxbnet 8ffa0531d6 feat(uploader): 添加文件上传配置和临时文件处理功能
构建上传工具 / build (push) Successful in 1m10s
- 新增 ClearFilesNoPrompt 配置项用于控制是否提示清空文件
- 实现临时文件目录(tmp)管理,优先处理未上传完的文件
- 添加文件复制功能,支持快速零拷贝技术
- 实现文件清空提示机制,支持用户确认操作
- 优化文件上传流程,添加进度跟踪和状态更新
- 过滤掉大小为0的文件,避免无效上传
- 修改数据结构名称提升代码可读性
2026-05-01 21:01:01 +08:00
ygxbnet 3f6e999783 feat(app): 添加文件清空确认对话框功能
- 引入 ConfirmClearDialog 组件用于清空文件确认
- 添加 clear-files-no-prompt 配置项控制是否显示确认弹窗
- 实现清空文件列表显示和确认逻辑
- 集成 Element Plus 图标组件库
- 优化日志输出格式增加空格分隔
- 重构配置写入方法使用统一的 configModel 枚举
- 添加事件监听处理清空文件操作
- 实现勾选不再提示选项并保存配置
2026-05-01 20:45:17 +08:00
ygxbnet 03e4e6f45b fix(uploader): 修复上传器逻辑和UI提示问题
构建上传工具 / build (push) Successful in 1m23s
- 修复目录选择为空时不更改配置的逻辑
- 将警告提示改为错误提示以提高用户体验
- 优化停止运行时的成功提示
- 增加进度列表的最大高度以显示更多内容
- 调整定时器逻辑顺序避免提前退出
- 移除重复的任务等待错误处理
- 优化代码结构和空行格式
2026-04-30 21:24:30 +08:00
ygxbnet 6bd82024d9 refactor(App): 移除服务器地址和自动启动配置功能
- 注释掉 serverUrl 相关的响应式变量定义
- 注释掉 autoStart 相关的响应式变量定义
- 移除进度列表中的测试数据注释
- 注释掉服务器地址验证逻辑
- 注释掉服务器地址和自动启动的写入配置函数
- 注释掉配置加载中的相关字段赋值
- 在模板中隐藏服务器地址输入框和自动启动复选框组件
2026-04-30 20:39:08 +08:00
ygxbnet 6952c33f16 refactor(ci): 重命名构建工作流文件
构建上传工具 / build (push) Successful in 2m56s
- 将 .gitea/workflows/build_tool.yaml 重命名为 .gitea/workflows/build.yaml
- 更新工作流中的作业名称从 build-tool 到 build
- 保持相同的触发条件和运行环境配置
2026-04-29 14:07:22 +08:00
ygxbnet 3d2b3469cc refactor(uploader): 重命名文件上传函数并优化代码结构
构建上传工具 / build-tool (push) Successful in 1m59s
- 将 StartLooking 函数重命名为 StartUpload 以更准确反映功能
- 修复构建脚本中的版本标记 ldflags 格式问题
- 更新应用标题格式,在版本号前添加冒号分隔符
- 优化上传进度循环逻辑,调整代码执行顺序
- 添加注释说明上传程序启动和文件处理功能
- 清理代码中的多余空行和变量声明
2026-04-29 14:01:50 +08:00
ygxbnet a528d6a877 fix(uploader): 修复上传器上下文取消时的资源泄露
构建上传工具 / build-tool (push) Successful in 1m19s
- 在上下文取消时正确关闭 lines channel
- 防止 goroutine 阻塞导致的内存泄露
- 确保所有资源在程序退出时正确释放
2026-04-28 15:44:48 +08:00
12 changed files with 494 additions and 194 deletions
@@ -2,7 +2,7 @@ name: 构建上传工具
on: [ push ] on: [ push ]
jobs: jobs:
build-tool: build:
env: env:
RUNNER_TOOL_CACHE: /toolcache RUNNER_TOOL_CACHE: /toolcache
runs-on: ubuntu-latest runs-on: ubuntu-latest
@@ -56,7 +56,7 @@ jobs:
build_date=$(TZ=Asia/Shanghai date +"%Y%m%d%H%M") build_date=$(TZ=Asia/Shanghai date +"%Y%m%d%H%M")
wails build \ wails build \
-platform windows/amd64 \ -platform windows/amd64 \
-ldflags "-X 'main.version= $build_date - $git_hash'" \ -ldflags "-X 'main.version=$build_date - $git_hash'" \
-o 上传工具.exe -o 上传工具.exe
- name: 上传构建文件 - name: 上传构建文件
+7 -7
View File
@@ -45,12 +45,12 @@ func (a *App) startup(ctx context.Context) {
}() }()
//在程序启动时运行上传程序 //在程序启动时运行上传程序
if config.APPConfig.IsRunOnStart { //if config.APPConfig.IsRunOnStart {
time.Sleep(time.Second) // time.Sleep(time.Second)
a.uploaderCTX, a.uploaderCancel = context.WithCancel(a.ctx) // a.uploaderCTX, a.uploaderCancel = context.WithCancel(a.ctx)
go uploader.StartLooking(a.uploaderCTX, &a.logChan, config.APPConfig.CheckDir) // go uploader.StartUpload(a.uploaderCTX, &a.logChan)
a.isRun = true // a.isRun = true
} //}
} }
// SelectPath 打开选择路径弹框 // SelectPath 打开选择路径弹框
@@ -74,7 +74,7 @@ func (a *App) StartUpload() {
return return
} }
a.uploaderCTX, a.uploaderCancel = context.WithCancel(a.ctx) a.uploaderCTX, a.uploaderCancel = context.WithCancel(a.ctx)
go uploader.StartLooking(a.uploaderCTX, &a.logChan, config.APPConfig.CheckDir) go uploader.StartUpload(a.uploaderCTX, &a.logChan)
a.isRun = true a.isRun = true
} }
+1
View File
@@ -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
View File
@@ -1 +1 @@
aa914e6b4676ee4621ced7ad6d81c58c 05225657934ff66d822c925754c951bf
+3
View File
@@ -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))
+74 -48
View File
@@ -1,24 +1,29 @@
<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('')
const checkDir = ref('') const checkDir = ref('')
const concurrentFiles = ref(1) const concurrentFiles = ref(1)
const uploadThreads = ref(1) const uploadThreads = ref(1)
const autoStart = ref(false) // const autoStart = ref(false)
const isRunning = ref(false) const isRunning = ref(false)
const logOutput = ref<string[]>([]) 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
@@ -26,9 +31,8 @@ interface FileProgress {
percentage: number percentage: number
} }
const progressList = ref<FileProgress[]>([ // {name: '测试文件1.txt', uploaded: 100, total: 500, percentage: 20},
// {name: '测试文件1.txt', uploaded: 100, total: 500, percentage: 20}, const progressList = ref<FileProgress[]>([])
])
const sortedProgressList = computed(() => { const sortedProgressList = computed(() => {
return [...progressList.value].sort((a, b) => { return [...progressList.value].sort((a, b) => {
@@ -42,7 +46,7 @@ const sortedProgressList = computed(() => {
}) })
const addLog = (msg: string) => { const addLog = (msg: string) => {
logOutput.value.push(`[${new Date().toLocaleString()}]` + msg) logOutput.value.push(`[${new Date().toLocaleString()}] ` + msg)
nextTick(() => { nextTick(() => {
if (logContentRef.value && logRoll.value) { if (logContentRef.value && logRoll.value) {
logContentRef.value.scrollTop = logContentRef.value.scrollHeight logContentRef.value.scrollTop = logContentRef.value.scrollHeight
@@ -53,21 +57,25 @@ const addLog = (msg: string) => {
const selectDirectory = () => { const selectDirectory = () => {
// ElMessage.info('请手动输入检测目录路径') // ElMessage.info('请手动输入检测目录路径')
SelectPath().then((path) => { SelectPath().then((path) => {
if (path) {
checkDir.value = path checkDir.value = path
} else {
ElMessage.warning('未选择目录,不更改配置')
}
}) })
} }
const startRun = () => { const startRun = () => {
if (!serverUrl.value) { // if (!serverUrl.value) {
ElMessage.warning('请输入服务器地址') // ElMessage.warning('请输入服务器地址')
return // return
} // }
if (!token.value) { if (!token.value) {
ElMessage.warning('请输入Token') ElMessage.error('请输入Token')
return return
} }
if (!checkDir.value) { if (!checkDir.value) {
ElMessage.warning('请选择检测目录') ElMessage.error('请选择检测目录')
return return
} }
@@ -77,7 +85,7 @@ const startRun = () => {
const stopRun = () => { const stopRun = () => {
addLog(`正在停止运行`) addLog(`正在停止运行`)
StopUpload().then(() => { StopUpload().then(() => {
ElMessage.info('已停止运行') ElMessage.success('已停止运行')
}) })
} }
@@ -93,34 +101,35 @@ const clearLog = () => {
}) })
} }
const writeServerUrl =() => { // const writeServerUrl =() => {
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 = () => {
WriteConfig("is-run-on-start", autoStart.value)
} }
// const writeAutoStart = () => {
// WriteConfig("is-run-on-start", autoStart.value)
// }
// 加载配置 // 加载配置
try { try {
GetConfig().then((config: Config) => { GetConfig().then((config: Config) => {
serverUrl.value = config.url // serverUrl.value = config.url
token.value = config.token token.value = config.token
checkDir.value = config.check_dir checkDir.value = config.check_dir
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()}] 配置已加载`)
}) })
@@ -128,24 +137,34 @@ try {
console.log(e) console.log(e)
} }
EventsOn("is-run", (run) => { try {
EventsOn("is-run", (run) => {
isRunning.value = run isRunning.value = run
}) })
EventsOn("progress", (progress) => { EventsOn("progress", (progress) => {
progressList.value = progress progressList.value = 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>
<div class="container"> <div class="container">
<div class="left-panel"> <div class="left-panel">
<div class="form-item"> <!-- <div class="form-item">-->
<label>服务器地址</label> <!-- <label>服务器地址</label>-->
<el-input v-model="serverUrl" placeholder="请输入服务器地址" :disabled="isRunning" @change="writeServerUrl()"/> <!-- <el-input v-model="serverUrl" placeholder="请输入服务器地址" :disabled="isRunning" @change="writeServerUrl()"/>-->
</div> <!-- </div>-->
<div class="form-item"> <div class="form-item">
<label>Token</label> <label>Token</label>
@@ -162,17 +181,19 @@ 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">-->
<el-checkbox v-model="autoStart" label="运行时自动启动上传" size="large" :disabled="isRunning" @change="writeAutoStart()"/> <!-- <el-checkbox v-model="autoStart" label="运行时自动启动上传" size="large" :disabled="isRunning" @change="writeAutoStart()"/>-->
</div> <!-- </div>-->
<div class="form-item"> <div class="form-item">
<label>上传进度</label> <label>上传进度</label>
@@ -202,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>
@@ -253,7 +279,7 @@ EventsOn("log", (msg) => {
.progress-list { .progress-list {
margin-top: 12px; margin-top: 12px;
max-height: 160px; max-height: 250px;
overflow-y: auto; overflow-y: auto;
display: flex; display: flex;
flex-direction: column; flex-direction: column;
@@ -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>
+9
View File
@@ -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",
}
+2
View File
@@ -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"];
} }
} }
+4
View File
@@ -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(".")
+182 -51
View File
@@ -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,9 +21,7 @@ import (
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
) )
var progress sync.Map type fileInfo struct {
type Task struct {
FilePath string FilePath string
FileLines int FileLines int
} }
@@ -34,7 +33,9 @@ type Progress struct {
Percentage int `json:"percentage"` Percentage int `json:"percentage"`
} }
func StartLooking(ctx context.Context, logChan *chan string, lookingPath string) { var progress sync.Map
func StartUpload(ctx context.Context, logChan *chan string) {
AddLog(logChan, "===============================================") AddLog(logChan, "===============================================")
AddLog(logChan, `服务器: `+config.APPConfig.Url) AddLog(logChan, `服务器: `+config.APPConfig.Url)
AddLog(logChan, `Token: `+config.APPConfig.Token) AddLog(logChan, `Token: `+config.APPConfig.Token)
@@ -48,6 +49,7 @@ func StartLooking(ctx context.Context, logChan *chan string, lookingPath string)
AddLog(logChan, "创建连接池完成,开始运行程序") AddLog(logChan, "创建连接池完成,开始运行程序")
progress.Clear() progress.Clear()
//推送上传进度 //推送上传进度
go func() { go func() {
for { for {
@@ -55,60 +57,143 @@ func StartLooking(ctx context.Context, logChan *chan string, lookingPath string)
case <-ctx.Done(): case <-ctx.Done():
return return
default: default:
time.Sleep(250 * time.Millisecond) }
var pg []Progress var pg []Progress
progress.Range(func(key, value any) bool { progress.Range(func(_, value any) bool {
p := value.(Progress) pg = append(pg, value.(Progress))
pg = append(pg, p)
return true return true
}) })
wailsruntime.EventsEmit(ctx, "progress", pg) wailsruntime.EventsEmit(ctx, "progress", pg)
}
time.Sleep(250 * time.Millisecond)
} }
}() }()
//开启上传程序
for { for {
uploadData(ctx, logChan, lookingPath) uploadData(ctx, logChan)
select { select {
case <-time.After(time.Minute):
case <-ctx.Done(): case <-ctx.Done():
return return
case <-time.After(time.Minute):
} }
} }
} }
func uploadData(ctx context.Context, logChan *chan string, lookingPath string) { func uploadData(ctx context.Context, logChan *chan string) {
var path = "./" start := time.Now()
if lookingPath != "" {
path = lookingPath // 获取检测目录
var checkPath = "./"
if 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())
} }
// 使用 bufio.Scanner 逐行读取 // 使用 bufio.Scanner 逐行读取
scanner := bufio.NewScanner(file) scanner := bufio.NewScanner(file)
lineCount := 0 lineCount := 0
@@ -120,89 +205,129 @@ func uploadData(ctx context.Context, logChan *chan string, lookingPath string) {
if lineCount == 0 { if lineCount == 0 {
continue continue
} }
fileLines[filepath.Base(filePath)] = lineCount filesInfo[filepath.Base(filePath)] = fileInfo{
FilePath: filePath,
FileLines: lineCount,
}
isAllEmpty = false isAllEmpty = false
AddLog(logChan, fmt.Sprintf("%s 文件行数:%v", filepath.Base(filePath), lineCount)) AddLog(logChan, fmt.Sprintf("%s 文件行数:%v", filepath.Base(filePath), lineCount))
} }
}
if isAllEmpty { if isAllEmpty {
AddLog(logChan, "所有文件都为空,不进行上传") AddLog(logChan, "所有文件都为空,不进行上传")
return return
} }
//刷新文件上传进度
progress.Clear() progress.Clear()
//添加文件上传任务参数(文件路径,文件行数) for fileName, info := range filesInfo {
var tasks []Task progress.Store(fileName,
for fileName, lines := range fileLines { Progress{
tasks = append(tasks, Task{FilePath: path + "/" + fileName, FileLines: lines}) FileName: fileName,
progress.Store(fileName, Progress{FileName: fileName, Total: lines, Uploaded: 0, Percentage: 0}) Total: info.FileLines,
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:
// 等待所有任务完成
if err := g.Wait(); err != nil {
AddLog(logChan, fmt.Sprintf("任务执行出错: %v", err))
} else {
AddLog(logChan, "所有任务执行完成!")
} }
// 等待所有任务完成
g.Wait()
AddLog(logChan, "所有任务执行完成!")
AddLog(logChan, fmt.Sprintf("上传完成,耗时:%s", time.Since(start).String())) AddLog(logChan, fmt.Sprintf("上传完成,耗时:%s", time.Since(start).String()))
}
} }
// 获取目录中的所有txt文件 // 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
}
// 强制刷入磁盘,保证数据完整
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 {
return err return err
} }
// 只处理普通文件,跳过目录 // 跳过目录,只处理普通文件
if !info.Mode().IsRegular() { if !info.Mode().IsRegular() {
return nil return nil
} }
// 检查文件扩展名是否为.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
}) })
@@ -210,6 +335,7 @@ func getTxtFiles(dir string) (txtFiles []string, err error) {
return txtFiles, err return txtFiles, err
} }
// processFile 处理每个文件
func processFile(ctx context.Context, logChan *chan string, filePath string, fileLines int) { func processFile(ctx context.Context, logChan *chan string, filePath string, fileLines int) {
// 打开文件 // 打开文件
file, err := os.Open(filePath) file, err := os.Open(filePath)
@@ -226,13 +352,15 @@ func processFile(ctx context.Context, logChan *chan string, filePath string, fil
for i := 0; i < config.APPConfig.ThreadCount; i++ { for i := 0; i < config.APPConfig.ThreadCount; i++ {
select { select {
case <-ctx.Done(): case <-ctx.Done():
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)
@@ -248,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()
} }
}() }()
@@ -260,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),
@@ -269,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{
@@ -296,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
@@ -306,7 +438,6 @@ func processLines(ctx context.Context, logChan *chan string, lines *chan string,
} }
atomic.AddInt32(countLine, 1) atomic.AddInt32(countLine, 1)
} }
}
} }
// AddLog 添加日志 // AddLog 添加日志
+1 -1
View File
@@ -22,7 +22,7 @@ func main() {
// Create application with options // Create application with options
err := wails.Run(&options.App{ err := wails.Run(&options.App{
Title: "dypid-client - 版本" + version, Title: "dypid-client - 版本" + version,
Width: 1024, Width: 1024,
Height: 768, Height: 768,
AssetServer: &assetserver.Options{ AssetServer: &assetserver.Options{