Browse Source

change win

master
godo 9 months ago
parent
commit
7e3a238524
  1. 15
      godo/localchat/filedown.go
  2. 12
      godo/localchat/getfiles.go

15
godo/localchat/filedown.go

@ -33,6 +33,7 @@ import (
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"runtime"
"time" "time"
) )
@ -93,10 +94,15 @@ func handleResponse(reader io.Reader, ip string) error {
log.Printf("Received file list: %v", fileList) log.Printf("Received file list: %v", fileList)
for _, file := range fileList { for _, file := range fileList {
if runtime.GOOS != "windows" && containsBackslash(file.WritePath) {
file.WritePath = filepath.FromSlash(file.WritePath)
}
checkpath := filepath.Join(receiveDir, 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 { if !file.IsDir {
go downloadFile(file.Path, checkpath, ip) go downloadFile(file.Path, checkpath, ip)
} }
@ -105,6 +111,11 @@ func handleResponse(reader io.Reader, ip string) error {
return nil return nil
} }
// containsBackslash 检查字符串中是否包含反斜杠
func containsBackslash(s string) bool {
return filepath.Separator == '\\' && filepath.VolumeName(s) == ""
}
// downloadFile 下载单个文件 // downloadFile 下载单个文件
func downloadFile(filePath string, checkpath string, ip string) error { func downloadFile(filePath string, checkpath string, ip string) error {
url := fmt.Sprintf("http://%s:56780/localchat/servefile?path=%s", ip, filePath) url := fmt.Sprintf("http://%s:56780/localchat/servefile?path=%s", ip, filePath)

12
godo/localchat/getfiles.go

@ -107,14 +107,7 @@ func HandleGetFiles(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
w.Write(jsonData) 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 { func walkDirectory(rootPath string, files *[]FileItem, writePath string) error {
return filepath.Walk(rootPath, func(path string, info os.FileInfo, err error) error { return filepath.Walk(rootPath, func(path string, info os.FileInfo, err error) error {
if err != nil { if err != nil {
@ -128,6 +121,9 @@ func walkDirectory(rootPath string, files *[]FileItem, writePath string) error {
return fmt.Errorf("failed to calculate relative path") return fmt.Errorf("failed to calculate relative path")
} }
currentWritePath := filepath.Join(writePath, filepath.Base(relativePath)) currentWritePath := filepath.Join(writePath, filepath.Base(relativePath))
if !isDir {
currentWritePath = filepath.Join(writePath, filepath.Dir(relativePath))
}
*files = append(*files, FileItem{ *files = append(*files, FileItem{
Path: path, Path: path,
IsDir: isDir, IsDir: isDir,

Loading…
Cancel
Save