github.com/fzfile/BaiduPCS-Go@v0.0.0-20200606205115-4408961cf336/baidupcs/util.go (about)

     1  package baidupcs
     2  
     3  import (
     4  	"errors"
     5  	"github.com/fzfile/BaiduPCS-Go/baidupcs/pcserror"
     6  	"github.com/fzfile/BaiduPCS-Go/pcsutil"
     7  	"github.com/fzfile/BaiduPCS-Go/pcsutil/converter"
     8  	"path"
     9  	"strings"
    10  )
    11  
    12  // Isdir 检查路径在网盘中是否为目录
    13  func (pcs *BaiduPCS) Isdir(pcspath string) (isdir bool, pcsError pcserror.Error) {
    14  	if path.Clean(pcspath) == PathSeparator {
    15  		return true, nil
    16  	}
    17  
    18  	f, pcsError := pcs.FilesDirectoriesMeta(pcspath)
    19  	if pcsError != nil {
    20  		return false, pcsError
    21  	}
    22  
    23  	return f.Isdir, nil
    24  }
    25  
    26  func (pcs *BaiduPCS) checkIsdir(op string, targetPath string) pcserror.Error {
    27  	// 检测文件是否存在于网盘路径
    28  	// 很重要, 如果文件存在会直接覆盖!!! 即使是根目录!
    29  	isdir, pcsError := pcs.Isdir(targetPath)
    30  	if pcsError != nil {
    31  		// 忽略远程服务端返回的错误
    32  		if pcsError.GetErrType() != pcserror.ErrTypeRemoteError {
    33  			return pcsError
    34  		}
    35  	}
    36  
    37  	errInfo := pcserror.NewPCSErrorInfo(op)
    38  	if isdir {
    39  		errInfo.ErrType = pcserror.ErrTypeOthers
    40  		errInfo.Err = errors.New("保存路径不可以覆盖目录")
    41  		return errInfo
    42  	}
    43  	return nil
    44  }
    45  
    46  func mergeStringList(a ...string) string {
    47  	s := strings.Join(a, `","`)
    48  	return `["` + s + `"]`
    49  }
    50  
    51  func mergeInt64List(si ...int64) string {
    52  	i := converter.SliceInt64ToString(si)
    53  	s := strings.Join(i, ",")
    54  	return "[" + s + "]"
    55  }
    56  
    57  func allRelatedDir(pcspaths []string) (dirs []string) {
    58  	for _, pcspath := range pcspaths {
    59  		pathDir := path.Dir(pcspath)
    60  		if !pcsutil.ContainsString(dirs, pathDir) {
    61  			dirs = append(dirs, pathDir)
    62  		}
    63  	}
    64  	return
    65  }
    66  
    67  // GetHTTPScheme 获取 http 协议, https 或 http
    68  func GetHTTPScheme(https bool) (scheme string) {
    69  	if https {
    70  		return "https"
    71  	}
    72  	return "http"
    73  }