fix relative path error to fix issue #119

This commit is contained in:
xiaoyaofenfen 2022-08-09 19:29:42 +08:00
parent d90894898f
commit f13177214e
3 changed files with 43 additions and 1 deletions

View File

@ -36,7 +36,7 @@ func CmdSync() cli.Command {
UsageText: cmder.App().Name + " sync",
Description: `
备份功能支持备份本地文件到云盘备份云盘文件到本地双向同步备份三种模式支持JavaScript插件对备份文件进行过滤
指定本地目录和对应的一个网盘目录以备份文件网盘目录必须和本地目录独占使用不要用作其他用途不然备份可能会有问题
指定本地目录和对应的一个网盘目录以备份文件网盘目录必须和本地目录独占使用不要用作其他用途不然备份可能会有问题
备份功能支持以下三种模式
1. upload
@ -107,6 +107,8 @@ mode - 模式,支持三种: upload(备份本地文件到云盘),download(备
fmt.Println("未登录账号")
return nil
}
activeUser := GetActiveUser()
dp := c.Int("dp")
if dp == 0 {
dp = config.Config.MaxDownloadParallel
@ -140,7 +142,21 @@ mode - 模式,支持三种: upload(备份本地文件到云盘),download(备
localDir := c.String("ldir")
panDir := c.String("pdir")
mode := c.String("mode")
// make path absolute
if !utils.IsAbsPath(localDir) {
pwd, _ := os.Getwd()
localDir = pwd + "/" + path.Clean(localDir)
}
panDir = activeUser.PathJoin(activeUser.ActiveDriveId, panDir)
if localDir != "" && panDir != "" {
if !utils.IsAbsPath(localDir) {
fmt.Println("本地目录请指定绝对路径")
return nil
}
if !path.IsAbs(panDir) {
fmt.Println("网盘目录请指定绝对路径")
return nil
}
//if b, e := utils.PathExists(localDir); e == nil {
// if !b {
// fmt.Println("本地文件夹不存在:", localDir)

View File

@ -26,6 +26,9 @@ import (
"net/http/cookiejar"
"net/url"
"os"
"path"
"regexp"
"runtime"
"strconv"
"strings"
"time"
@ -231,3 +234,18 @@ func Md5Str(text string) string {
fmt.Fprintf(sb, "%x", re)
return strings.ToLower(sb.String())
}
// IsAbsPath 是否是绝对路径
func IsAbsPath(filePath string) bool {
if runtime.GOOS == "windows" {
// 是否是windows路径
matched, _ := regexp.MatchString("^([a-zA-Z]:)", filePath)
if matched {
// windows volume label
return true
}
return false
} else {
return path.IsAbs(filePath)
}
}

View File

@ -31,3 +31,11 @@ func TestMd5Str(t *testing.T) {
func TestParseTimeStr(t *testing.T) {
fmt.Println(ParseTimeStr(""))
}
func TestIsAbsPath_ReturnTrue(t *testing.T) {
fmt.Println(IsAbsPath("D:\\my\\folder\\test"))
}
func TestIsAbsPath_ReturnFalse(t *testing.T) {
fmt.Println(IsAbsPath("my\\folder\\test"))
}