From 7e3a238524af69296158cb63d673ff88bff8e0a3 Mon Sep 17 00:00:00 2001 From: godo Date: Tue, 10 Sep 2024 17:29:59 +0800 Subject: [PATCH] change win --- godo/localchat/filedown.go | 15 +++++++++++++-- godo/localchat/getfiles.go | 12 ++++-------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/godo/localchat/filedown.go b/godo/localchat/filedown.go index c5672be..0ee0b6a 100644 --- a/godo/localchat/filedown.go +++ b/godo/localchat/filedown.go @@ -33,6 +33,7 @@ import ( "net/http" "os" "path/filepath" + "runtime" "time" ) @@ -93,10 +94,15 @@ func handleResponse(reader io.Reader, ip string) error { log.Printf("Received file list: %v", fileList) for _, file := range fileList { + if runtime.GOOS != "windows" && containsBackslash(file.WritePath) { + file.WritePath = filepath.FromSlash(file.WritePath) + } checkpath := filepath.Join(receiveDir, file.WritePath) - if err := os.MkdirAll(checkpath, 0755); err != nil { - return fmt.Errorf("failed to create directory: %v", err) + + if !libs.PathExists(checkpath) { + os.MkdirAll(checkpath, 0755) } + if !file.IsDir { go downloadFile(file.Path, checkpath, ip) } @@ -105,6 +111,11 @@ func handleResponse(reader io.Reader, ip string) error { return nil } +// containsBackslash 检查字符串中是否包含反斜杠 +func containsBackslash(s string) bool { + return filepath.Separator == '\\' && filepath.VolumeName(s) == "" +} + // downloadFile 下载单个文件 func downloadFile(filePath string, checkpath string, ip string) error { url := fmt.Sprintf("http://%s:56780/localchat/servefile?path=%s", ip, filePath) diff --git a/godo/localchat/getfiles.go b/godo/localchat/getfiles.go index 4c9b050..2e586e7 100644 --- a/godo/localchat/getfiles.go +++ b/godo/localchat/getfiles.go @@ -107,14 +107,7 @@ func HandleGetFiles(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") w.Write(jsonData) } -func calculateWritePath(filePath, baseDir string) string { - relativePath, err := filepath.Rel(baseDir, filePath) - if err != nil { - log.Printf("Failed to calculate relative path: %v", err) - return "" - } - return filepath.Dir(relativePath) -} + func walkDirectory(rootPath string, files *[]FileItem, writePath string) error { return filepath.Walk(rootPath, func(path string, info os.FileInfo, err error) error { if err != nil { @@ -128,6 +121,9 @@ func walkDirectory(rootPath string, files *[]FileItem, writePath string) error { return fmt.Errorf("failed to calculate relative path") } currentWritePath := filepath.Join(writePath, filepath.Base(relativePath)) + if !isDir { + currentWritePath = filepath.Join(writePath, filepath.Dir(relativePath)) + } *files = append(*files, FileItem{ Path: path, IsDir: isDir,