From eeef9be9d3d53bab88baee8f5da7e6616633e374 Mon Sep 17 00:00:00 2001 From: songzhibin97 <718428482@qq.com> Date: Fri, 22 Oct 2021 16:23:35 +0800 Subject: [PATCH] =?UTF-8?q?fix:#796=20=E8=A1=A5=E5=85=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/service/system/sys_auto_code.go | 6 ++++++ server/service/system/sys_autocode_history.go | 5 +++++ server/utils/file_operations.go | 11 ++++++----- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/server/service/system/sys_auto_code.go b/server/service/system/sys_auto_code.go index c27c40d6..aa605142 100644 --- a/server/service/system/sys_auto_code.go +++ b/server/service/system/sys_auto_code.go @@ -197,6 +197,12 @@ func (autoCodeService *AutoCodeService) CreateTemp(autoCode system.AutoCodeStruc for index := range dataList { autoCodeService.addAutoMoveFile(&dataList[index]) } + // 判断目标文件是否都可以移动 + for _, value := range dataList { + if utils.FileExist(value.autoMoveFilePath) { + return errors.New(fmt.Sprintf("目标文件已存在:%s\n", value.autoMoveFilePath)) + } + } for _, value := range dataList { // 移动文件 if err := utils.FileMove(value.autoCodePath, value.autoMoveFilePath); err != nil { return err diff --git a/server/service/system/sys_autocode_history.go b/server/service/system/sys_autocode_history.go index 542cc5a3..2e163e6f 100644 --- a/server/service/system/sys_autocode_history.go +++ b/server/service/system/sys_autocode_history.go @@ -81,6 +81,11 @@ func (autoCodeHistoryService *AutoCodeHistoryService) RollBack(id uint) error { // 迁移 nPath := filepath.Join(global.GVA_CONFIG.AutoCode.Root, "rm_file", time.Now().Format("20060102"), filepath.Base(filepath.Dir(filepath.Dir(path))), filepath.Base(filepath.Dir(path)), filepath.Base(path)) + // 判断目标文件是否存在 + for utils.FileExist(nPath) { + fmt.Println("文件已存在:", nPath) + nPath += fmt.Sprintf("_%d", time.Now().Nanosecond()) + } err = utils.FileMove(path, nPath) if err != nil { fmt.Println(">>>>>>>>>>>>>>>>>>>", err) diff --git a/server/utils/file_operations.go b/server/utils/file_operations.go index 497caa79..7e4bae99 100644 --- a/server/utils/file_operations.go +++ b/server/utils/file_operations.go @@ -1,7 +1,6 @@ package utils import ( - "errors" "os" "path/filepath" "reflect" @@ -26,10 +25,6 @@ func FileMove(src string, dst string) (err error) { if err != nil { return err } - _, err = os.Stat(dst) - if err == nil { - return errors.New("文件已存在") - } var revoke = false dir := filepath.Dir(dst) Redirect: @@ -71,3 +66,9 @@ func TrimSpace(target interface{}) { } } } + +// FileExist 判断文件是否存在 +func FileExist(path string) bool { + _, err := os.Lstat(path) + return !os.IsNotExist(err) +}