Skip to content

Commit

Permalink
support parent id in download and new commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
52funny committed Mar 18, 2023
1 parent 9453e85 commit fb4b521
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 36 deletions.
52 changes: 33 additions & 19 deletions cmd/download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ var count int
// default server root directory (.)
var folder string

// parent path id
var parentId string

// Output directory
//
// default current directory (.)
Expand All @@ -57,15 +60,20 @@ func init() {
DownloadCmd.Flags().IntVarP(&count, "count", "c", 3, "number of simultaneous downloads")
DownloadCmd.Flags().StringVarP(&output, "output", "o", "", "output directory")
DownloadCmd.Flags().StringVarP(&folder, "path", "p", "/", "specific the folder of the pikpak server\nonly support download folder")
DownloadCmd.Flags().StringVarP(&parentId, "parent-id", "P", "", "the parent path id")
}

// Downloads all files in the specified directory
func downloadFolder(p *pikpak.PikPak) {
base := filepath.Base(folder)
parentId, err := p.GetPathFolderId(folder)
if err != nil {
logrus.Errorln("Get Parent Folder Id Failed:", err)
return
var err error
if parentId == "" {
parentId, err = p.GetPathFolderId(folder)
if err != nil {
logrus.Errorln("Get Parent Folder Id Failed:", err)
return
}

}
collectStat := make([]warpStat, 0)
recursive(p, &collectStat, parentId, filepath.Join(output, base))
Expand Down Expand Up @@ -141,31 +149,37 @@ func recursive(p *pikpak.PikPak, collectWarpFile *[]warpStat, parentId string, p
}

func downloadFile(p *pikpak.PikPak, args []string) {
files := make([]string, 0, len(args))
for _, v := range args {
files = append(files, filepath.Join(folder, v))
var err error
if parentId == "" {
parentId, err = p.GetPathFolderId(folder)
if err != nil {
logrus.Errorln("get folder failed:", err)
return
}
}

// if output not exists then create.
if err := utils.CreateDirIfNotExist(output); err != nil {
logrus.Errorln("Create output directory failed:", err)
return
}

sendCh := make(chan warpFile, 1)
receiveCh := make(chan struct{}, len(files))
receiveCh := make(chan struct{}, len(args))

for i := 0; i < count; i++ {
go download(sendCh, receiveCh)
}
for _, f := range files {
dir, base := filepath.Dir(f), filepath.Base(f)
id, err := p.GetPathFolderId(dir)
for _, path := range args {
stat, err := p.GetFileStat(parentId, path)
if err != nil {
logrus.Errorln(dir, "Get Parent Folder Id Failed:", err)
continue
}
stat, err := p.GetFileStat(id, base)
if err != nil {
logrus.Errorln(base, "Get File Stat Failed:", err)
logrus.Errorln(path, "get parent id failed:", err)
continue
}

file, err := p.GetFile(stat.ID)
if err != nil {
logrus.Errorln(base, "Get File Failed:", err)
logrus.Errorln(path, "get file failed", err)
continue
}
sendCh <- warpFile{
Expand All @@ -174,7 +188,7 @@ func downloadFile(p *pikpak.PikPak, args []string) {
}
}
close(sendCh)
for i := 0; i < len(files); i++ {
for i := 0; i < len(args); i++ {
<-receiveCh
}
close(receiveCh)
Expand Down
13 changes: 9 additions & 4 deletions cmd/new/folder/folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,22 @@ var NewFolderCommand = &cobra.Command{
}

var path string
var parentId string

func init() {
NewFolderCommand.Flags().StringVarP(&path, "path", "p", "/", "The path of the folder")
NewFolderCommand.Flags().StringVarP(&parentId, "parent-id", "P", "", "The parent id")
}

// new folder
func handleNewFolder(p *pikpak.PikPak, folders []string) {
parentId, err := p.GetPathFolderId(path)
if err != nil {
logrus.Errorf("Get parent id failed: %s\n", err)
return
var err error
if parentId == "" {
parentId, err = p.GetPathFolderId(path)
if err != nil {
logrus.Errorf("Get parent id failed: %s\n", err)
return
}
}

for _, folder := range folders {
Expand Down
14 changes: 10 additions & 4 deletions cmd/new/sha/sha.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,25 @@ var NewShaCommand = &cobra.Command{

var path string

var parentId string

var input string

func init() {
NewShaCommand.Flags().StringVarP(&path, "path", "p", "/", "The path of the folder")
NewShaCommand.Flags().StringVarP(&input, "input", "i", "", "The input of the sha file")
NewShaCommand.Flags().StringVarP(&parentId, "parent-id", "P", "", "The parent id")
}

// new folder
func handleNewSha(p *pikpak.PikPak, shas []string) {
parentId, err := p.GetPathFolderId(path)
if err != nil {
logrus.Errorf("Get parent id failed: %s\n", err)
return
var err error
if parentId == "" {
parentId, err = p.GetPathFolderId(path)
if err != nil {
logrus.Errorf("Get parent id failed: %s\n", err)
return
}
}

for _, sha := range shas {
Expand Down
26 changes: 17 additions & 9 deletions cmd/new/url/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,29 @@ var NewUrlCommand = &cobra.Command{

var path string

var parentId string

var input string

var cli bool

func init() {
NewUrlCommand.Flags().StringVarP(&path, "path", "p", "/", "The path of the folder")
NewUrlCommand.Flags().StringVarP(&parentId, "parent-id", "P", "", "The parent id")
NewUrlCommand.Flags().StringVarP(&input, "input", "i", "", "The input of the sha file")
NewUrlCommand.Flags().BoolVarP(&cli, "cli", "c", false, "The cli mode")
}

// new folder
func handleNewUrl(p *pikpak.PikPak, shas []string) {
parentId, err := p.GetPathFolderId(path)
if err != nil {
logrus.Errorf("Get parent id failed: %s\n", err)
return
var err error
if parentId == "" {
parentId, err = p.GetPathFolderId(path)
if err != nil {
logrus.Errorf("Get parent id failed: %s\n", err)
return
}
}

for _, url := range shas {
err := p.CreateUrlFile(parentId, url)
if err != nil {
Expand All @@ -86,10 +91,13 @@ func handleNewUrl(p *pikpak.PikPak, shas []string) {
}

func handleCli(p *pikpak.PikPak) {
parentId, err := p.GetPathFolderId(path)
if err != nil {
logrus.Errorf("Get parent id failed: %s\n", err)
return
var err error
if parentId == "" {
parentId, err = p.GetPathFolderId(path)
if err != nil {
logrus.Errorf("Get parent id failed: %s\n", err)
return
}
}

reader := bufio.NewReader(os.Stdin)
Expand Down

0 comments on commit fb4b521

Please sign in to comment.