github.com/qjfoidnh/BaiduPCS-Go@v0.0.0-20231011165705-caa18a3765f3/baidupcs/util.go (about)

     1  package baidupcs
     2  
     3  import (
     4  	"crypto/md5"
     5  	"errors"
     6  	"fmt"
     7  	"github.com/qjfoidnh/BaiduPCS-Go/baidupcs/pcserror"
     8  	"github.com/qjfoidnh/BaiduPCS-Go/pcsutil"
     9  	"github.com/qjfoidnh/BaiduPCS-Go/pcsutil/converter"
    10  	"io"
    11  	"path"
    12  	"regexp"
    13  	"strconv"
    14  	"strings"
    15  	"time"
    16  )
    17  
    18  // Isdir 检查路径在网盘中是否为目录
    19  func (pcs *BaiduPCS) Isdir(pcspath string) (fileSize int64, isdir bool, pcsError pcserror.Error) {
    20  	if path.Clean(pcspath) == PathSeparator {
    21  		return 0, true, nil
    22  	}
    23  
    24  	f, pcsError := pcs.FilesDirectoriesMeta(pcspath)
    25  	if pcsError != nil {
    26  		return 0, false, pcsError
    27  	}
    28  
    29  	return f.Size, f.Isdir, nil
    30  }
    31  
    32  func (pcs *BaiduPCS) CheckIsdir(op string, targetPath string, policy string, fileSize int64) pcserror.Error {
    33  	// 检测文件是否存在于网盘路径
    34  	// 很重要, 如果文件存在会直接覆盖!!! 即使是根目录!
    35  	onlineSize, isdir, pcsError := pcs.Isdir(targetPath)
    36  	if pcsError != nil {
    37  		// 忽略远程服务端返回的错误
    38  		if pcsError.GetErrType() != pcserror.ErrTypeRemoteError {
    39  			return pcsError
    40  		}
    41  	}
    42  
    43  	errInfo := pcserror.NewPCSErrorInfo(op)
    44  	if isdir {
    45  		errInfo.ErrType = pcserror.ErrTypeOthers
    46  		errInfo.Err = errors.New("保存路径不可以覆盖目录")
    47  		return errInfo
    48  	}
    49  	// 如果存在文件, 则根据upload策略选择返回的错误码
    50  	if pcsError == nil {
    51  		switch policy {
    52  		case "fail":
    53  			errInfo.ErrCode = 114514
    54  			errInfo.ErrType = pcserror.ErrTypeRemoteError
    55  			errInfo.ErrMsg = "目标位置存在同名文件"
    56  			return errInfo
    57  		case "skip":
    58  			errInfo.ErrCode = 114514
    59  			errInfo.ErrMsg = "目标位置存在同名文件"
    60  			errInfo.ErrType = pcserror.ErrTypeRemoteError
    61  			return errInfo
    62  		case "rsync":
    63  			if onlineSize == fileSize {
    64  				errInfo.ErrCode = 1919810
    65  				errInfo.ErrMsg = "目标位置文件大小与源文件一致"
    66  				errInfo.ErrType = pcserror.ErrTypeRemoteError
    67  				return errInfo
    68  			}
    69  		default:
    70  			return nil
    71  		}
    72  	}
    73  	return nil
    74  }
    75  
    76  func mergeStringList(a ...string) string {
    77  	s := strings.Join(a, `","`)
    78  	return `["` + s + `"]`
    79  }
    80  
    81  func mergeInt64List(si ...int64) string {
    82  	i := converter.SliceInt64ToString(si)
    83  	s := strings.Join(i, ",")
    84  	return "[" + s + "]"
    85  }
    86  
    87  func allRelatedDir(pcspaths []string) (dirs []string) {
    88  	for _, pcspath := range pcspaths {
    89  		pathDir := path.Dir(pcspath)
    90  		if !pcsutil.ContainsString(dirs, pathDir) {
    91  			dirs = append(dirs, pathDir)
    92  		}
    93  	}
    94  	return
    95  }
    96  
    97  func CreatePasswd() string {
    98  	t := time.Now()
    99  	h := md5.New()
   100  	io.WriteString(h, "Asswecan")
   101  	io.WriteString(h, t.String())
   102  	passwd := fmt.Sprintf("%x", h.Sum(nil))
   103  	return passwd[0:4]
   104  }
   105  
   106  // GetHTTPScheme 获取 http 协议, https 或 http
   107  func GetHTTPScheme(https bool) (scheme string) {
   108  	if https {
   109  		return "https"
   110  	}
   111  	return "http"
   112  }
   113  
   114  func DecryptMD5(rawMD5 string) string {
   115  	if len(rawMD5) != 32 {
   116  		return rawMD5
   117  	}
   118  	var keychar string = rawMD5[9:10]
   119  	match, _ := regexp.MatchString("[a-f0-9]", keychar)
   120  	if match {
   121  		return rawMD5
   122  	}
   123  	sliceFirst := fmt.Sprintf("%x", []rune(rawMD5)[9] -'g')
   124  	sliceSecond := rawMD5[0:9] + sliceFirst + rawMD5[10:]
   125  	sliceThird := ""
   126  	for i := 0; i < len(sliceSecond); i++ {
   127  		if sliceSecond[i:i+1] == "-" {
   128  			sliceThird += fmt.Sprintf("%x", 15 & i)
   129  			continue
   130  		}
   131  		num, err := strconv.ParseInt(sliceSecond[i:i+1], 16, 64)
   132  		if err != nil {
   133  			return rawMD5
   134  		}
   135  		sliceThird += fmt.Sprintf("%x", int(num) ^ (15 & i))
   136  	}
   137  	return sliceThird[8:16] + sliceThird[0:8] + sliceThird[24:32] + sliceThird[16:24]
   138  }