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

     1  package pcscommand
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"math/rand"
     7  	"path"
     8  	"time"
     9  )
    10  
    11  const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    12  
    13  var (
    14  	// ErrShellPatternMultiRes 多条通配符匹配结果
    15  	ErrShellPatternMultiRes = errors.New("多条通配符匹配结果")
    16  	// ErrShellPatternNoHit 未匹配到路径
    17  	ErrShellPatternNoHit = errors.New("未匹配到路径, 请检测通配符")
    18  )
    19  
    20  // ListTask 队列状态 (基类)
    21  type ListTask struct {
    22  	ID       int // 任务id
    23  	MaxRetry int // 最大重试次数
    24  	retry    int // 任务失败的重试次数
    25  }
    26  
    27  func init() {
    28  	rand.Seed(time.Now().UnixNano())
    29  }
    30  
    31  // RunTestShellPattern 执行测试通配符
    32  func RunTestShellPattern(pattern string) {
    33  	pcs := GetBaiduPCS()
    34  	paths, err := pcs.MatchPathByShellPattern(GetActiveUser().PathJoin(pattern))
    35  	if err != nil {
    36  		fmt.Println(err)
    37  		return
    38  	}
    39  	for k := range paths {
    40  		fmt.Printf("%s\n", paths[k])
    41  	}
    42  	return
    43  }
    44  
    45  func matchPathByShellPatternOnce(pattern *string) error {
    46  	paths, err := GetBaiduPCS().MatchPathByShellPattern(GetActiveUser().PathJoin(*pattern))
    47  	if err != nil {
    48  		return err
    49  	}
    50  	switch len(paths) {
    51  	case 0:
    52  		return ErrShellPatternNoHit
    53  	case 1:
    54  		*pattern = paths[0]
    55  	default:
    56  		return ErrShellPatternMultiRes
    57  	}
    58  
    59  	return nil
    60  }
    61  
    62  func matchPathByShellPattern(patterns ...string) (pcspaths []string, err error) {
    63  	acUser, pcs := GetActiveUser(), GetBaiduPCS()
    64  	for k := range patterns {
    65  		ps, err := pcs.MatchPathByShellPattern(acUser.PathJoin(patterns[k]))
    66  		if err != nil {
    67  			return nil, err
    68  		}
    69  
    70  		pcspaths = append(pcspaths, ps...)
    71  	}
    72  	return pcspaths, nil
    73  }
    74  
    75  
    76  
    77  func randReplaceStr(s string, rname bool) string {
    78  	if !rname {
    79  		return s
    80  	}
    81  	filenameAll := path.Base(s)
    82  	fileSuffix := path.Ext(s)
    83  	filePrefix := filenameAll[0:len(filenameAll) - len(fileSuffix)]
    84  	runes := []rune(filePrefix)
    85  
    86  	for i := 0; i< len(filePrefix); i++ {
    87  		runes[i] = rune(letters[rand.Int63()%int64(len(letters))])
    88  		if i == 3 {
    89  			break
    90  		}
    91  	}
    92  	return path.Join(path.Dir(s), string(runes) + fileSuffix)
    93  }