Skip to content

Commit

Permalink
随机修改文件名
Browse files Browse the repository at this point in the history
转存秒传随机修改文件名增加成功率
  • Loading branch information
qjfoidnh committed Sep 19, 2023
1 parent 0c7c515 commit d17e700
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
1 change: 1 addition & 0 deletions baidupcs/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type (
TransferOption struct {
Download bool // 是否直接开始下载
Collect bool // 多文件整合
Rname bool // 随机改文件名
}
)

Expand Down
12 changes: 8 additions & 4 deletions internal/pcscommand/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func RunShareTransfer(params []string, opt *baidupcs.TransferOption) {
if len(params) == 1 {
link = params[0]
if strings.Contains(link, "bdlink=") || !strings.Contains(link, "pan.baidu.com/") {
RunRapidTransfer(link)
RunRapidTransfer(link, opt.Rname)
//fmt.Printf("%s失败: %s\n", baidupcs.OperationShareFileSavetoLocal, "秒传已不再被支持")
return
}
Expand Down Expand Up @@ -114,7 +114,7 @@ func RunShareTransfer(params []string, opt *baidupcs.TransferOption) {
}

// RunRapidTransfer 执行秒传链接解析及保存
func RunRapidTransfer(link string) {
func RunRapidTransfer(link string, rnameOpt ...bool) {
if strings.Contains(link, "bdlink=") || strings.Contains(link, "bdpan://") {
r, _ := regexp.Compile(`(bdlink=|bdpan://)([^\s]+)`)
link1 := r.FindStringSubmatch(link)[2]
Expand All @@ -125,17 +125,21 @@ func RunRapidTransfer(link string) {
}
link = string(decodeBytes)
}
rname := false
if len(rnameOpt) > 0 {
rname = rnameOpt[0]
}
link = strings.TrimSpace(link)
substrs := strings.SplitN(link, "#", 4)
if len(substrs) == 4 {
md5, slicemd5 := substrs[0], substrs[1]
size, _ := strconv.ParseInt(substrs[2], 10, 64)
filename := path.Join(GetActiveUser().Workdir, substrs[3])
filename := path.Join(GetActiveUser().Workdir, randReplaceStr(substrs[3], rname))
RunRapidUpload(filename, md5, slicemd5, size)
} else if len(substrs) == 3 {
md5 := substrs[0]
size, _ := strconv.ParseInt(substrs[1], 10, 64)
filename := path.Join(GetActiveUser().Workdir, substrs[2])
filename := path.Join(GetActiveUser().Workdir, randReplaceStr(substrs[2], rname))
RunRapidUpload(filename, md5, "", size)
} else {
fmt.Printf("%s失败: %s\n", baidupcs.OperationRapidLinkSavetoLocal, "秒传链接格式错误")
Expand Down
24 changes: 24 additions & 0 deletions internal/pcscommand/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ package pcscommand
import (
"errors"
"fmt"
"math/rand"
"path"
)

const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

var (
// ErrShellPatternMultiRes 多条通配符匹配结果
ErrShellPatternMultiRes = errors.New("多条通配符匹配结果")
Expand Down Expand Up @@ -62,3 +66,23 @@ func matchPathByShellPattern(patterns ...string) (pcspaths []string, err error)
}
return pcspaths, nil
}



func randReplaceStr(s string, rname bool) string {
if !rname {
return s
}
filenameAll := path.Base(s)
fileSuffix := path.Ext(s)
filePrefix := filenameAll[0:len(filenameAll) - len(fileSuffix)]
runes := []rune(filePrefix)

for i := 0; i< len(filePrefix); i++ {
runes[i] = rune(letters[rand.Int63()%int64(len(letters))])
if i == 3 {
break
}
}
return path.Join(path.Dir(s), string(runes) + fileSuffix)
}
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1477,6 +1477,7 @@ func main() {
opt := &baidupcs.TransferOption{
Download: c.Bool("download"),
Collect: c.Bool("collect"),
Rname: c.Bool("rname"),
}
pcscommand.RunShareTransfer(c.Args(), opt)
return nil
Expand All @@ -1490,6 +1491,10 @@ func main() {
Name: "collect",
Usage: "多文件整合到一个文件夹中转存",
},
cli.BoolFlag{
Name: "rname",
Usage: "秒传随机替换4位文件名提高成功率",
},
},
},
{
Expand Down

0 comments on commit d17e700

Please sign in to comment.