fix relative path error for sync command to fix issue #135

This commit is contained in:
xiaoyaofenfen 2022-08-31 11:51:28 +08:00
parent 4e7545e6e6
commit 08c9d58ef8
5 changed files with 23 additions and 7 deletions

View File

@ -166,16 +166,16 @@ priority - 优先级,只对双向同步备份模式有效。选项支持三种
mode := c.String("mode")
if localDir != "" && panDir != "" {
// make path absolute
if !utils.IsAbsPath(localDir) {
if !utils.IsLocalAbsPath(localDir) {
pwd, _ := os.Getwd()
localDir = path.Join(pwd, path.Clean(localDir))
}
panDir = activeUser.PathJoin(activeUser.ActiveDriveId, panDir)
if !utils.IsAbsPath(localDir) {
if !utils.IsLocalAbsPath(localDir) {
fmt.Println("本地目录请指定绝对路径")
return nil
}
if !path.IsAbs(panDir) {
if !utils.IsPanAbsPath(panDir) {
fmt.Println("网盘目录请指定绝对路径")
return nil
}

View File

@ -390,6 +390,7 @@ func (f *FileActionTask) uploadFile(ctx context.Context) error {
} else {
efi, apierr := f.panClient.FileInfoByPath(f.syncItem.DriveId, targetPanFilePath)
if apierr != nil && apierr.Code != apierror.ApiCodeFileNotFoundCode {
logger.Verbosef("上传文件错误: %s\n", apierr.String())
return apierr
}
if efi != nil && efi.FileId != "" {

View File

@ -134,6 +134,16 @@ func (m *SyncTaskManager) Start(tasks []*SyncTask) (bool, error) {
if len(task.Id) == 0 {
task.Id = utils.UuidStr()
}
// check pan path
if !utils.IsPanAbsPath(task.PanFolderPath) {
task.PanFolderPath = "/" + task.PanFolderPath
}
// check local path
if !utils.IsLocalAbsPath(task.LocalFolderPath) {
fmt.Println("任务启动失败,本地路径不是绝对路径: ", task.LocalFolderPath)
continue
}
task.panUser = m.PanUser
task.DriveId = m.DriveId
task.syncDbFolderPath = m.SyncConfigFolderPath

View File

@ -235,8 +235,8 @@ func Md5Str(text string) string {
return strings.ToLower(sb.String())
}
// IsAbsPath 是否是绝对路径
func IsAbsPath(filePath string) bool {
// IsLocalAbsPath 是否是本地绝对路径
func IsLocalAbsPath(filePath string) bool {
if runtime.GOOS == "windows" {
// 是否是windows路径
matched, _ := regexp.MatchString("^([a-zA-Z]:)", filePath)
@ -249,3 +249,8 @@ func IsAbsPath(filePath string) bool {
return path.IsAbs(filePath)
}
}
// IsPanAbsPath 是否是云盘绝对路径
func IsPanAbsPath(filePath string) bool {
return path.IsAbs(filePath)
}

View File

@ -33,9 +33,9 @@ func TestParseTimeStr(t *testing.T) {
}
func TestIsAbsPath_ReturnTrue(t *testing.T) {
fmt.Println(IsAbsPath("D:\\my\\folder\\test"))
fmt.Println(IsLocalAbsPath("D:\\my\\folder\\test"))
}
func TestIsAbsPath_ReturnFalse(t *testing.T) {
fmt.Println(IsAbsPath("my\\folder\\test"))
fmt.Println(IsLocalAbsPath("my\\folder\\test"))
}