From 9453e852a146eac143fc364ed96f85f940d31af5 Mon Sep 17 00:00:00 2001 From: 52funny Date: Sat, 18 Mar 2023 10:09:28 +0800 Subject: [PATCH] add parent id in share and upload commnad. --- cmd/share/share.go | 36 +++++++++++++++++++----------------- cmd/upload/upload.go | 36 +++++++++++++++++++++++------------- 2 files changed, 42 insertions(+), 30 deletions(-) diff --git a/cmd/share/share.go b/cmd/share/share.go index 754eb45..d06eaac 100644 --- a/cmd/share/share.go +++ b/cmd/share/share.go @@ -3,7 +3,6 @@ package share import ( "fmt" "os" - "path/filepath" "strings" "github.com/52funny/pikpakcli/conf" @@ -50,17 +49,23 @@ var folder string // default is the stdout var output string +var parentId string + func init() { ShareCommand.Flags().StringVarP(&folder, "path", "p", "/", "specific the folder of the pikpak server") ShareCommand.Flags().StringVarP(&output, "output", "o", "", "specific the file to write") + ShareCommand.Flags().StringVarP(&parentId, "parent-id", "P", "", "parent folder id") } // Share folder func shareFolder(p *pikpak.PikPak, f *os.File) { - parentId, err := p.GetDeepFolderId("", folder) - if err != nil { - logrus.Errorln("Get parent id failed:", err) - return + var err error + if parentId == "" { + parentId, err = p.GetDeepFolderId("", folder) + if err != nil { + logrus.Errorln("Get parent id failed:", err) + return + } } fileStat, err := p.GetFolderFileStatList(parentId) if err != nil { @@ -73,25 +78,22 @@ func shareFolder(p *pikpak.PikPak, f *os.File) { fmt.Fprintf(f, "PikPak://%s|%s|%s\n", stat.Name, stat.Size, stat.Hash) } } - } // Share files func shareFiles(p *pikpak.PikPak, args []string, f *os.File) { - files := make([]string, 0, len(args)) - for _, v := range args { - files = append(files, filepath.Join(folder, v)) - } - for _, file := range files { - dir, base := filepath.Dir(file), filepath.Base(file) - id, err := p.GetPathFolderId(dir) + var err error + if parentId == "" { + parentId, err = p.GetPathFolderId(folder) if err != nil { - logrus.Errorln(dir, "Get Parent Folder Id Failed:", err) - continue + logrus.Errorln("get parent id failed:", err) + return } - stat, err := p.GetFileStat(id, base) + } + for _, path := range args { + stat, err := p.GetFileStat(parentId, path) if err != nil { - logrus.Errorln(dir, "Get File Stat Failed:", err) + logrus.Errorln(path, "get file stat error:", err) continue } fmt.Fprintf(f, "PikPak://%s|%s|%s\n", stat.Name, stat.Size, stat.Hash) diff --git a/cmd/upload/upload.go b/cmd/upload/upload.go index 5d9464a..f72e4ca 100644 --- a/cmd/upload/upload.go +++ b/cmd/upload/upload.go @@ -66,12 +66,16 @@ var uploadConcurrency int64 // Sync mode var sync bool +// Parent path id +var parentId string + // Init upload command func init() { UploadCmd.Flags().StringVarP(&uploadFolder, "path", "p", "/", "specific the folder of the pikpak server") UploadCmd.Flags().Int64VarP(&uploadConcurrency, "concurrency", "c", 1<<4, "specific the concurrency of the upload") UploadCmd.Flags().StringSliceVarP(&exclude, "exn", "e", []string{}, "specific the exclude file or folder") UploadCmd.Flags().BoolVarP(&sync, "sync", "s", false, "sync mode") + UploadCmd.Flags().StringVarP(&parentId, "parent-id", "P", "", "parent folder id") } // Exclude string list @@ -89,19 +93,22 @@ func disposeExclude() { } } func handleUploadFile(p *pikpak.PikPak, path string) { - parentId, err := p.GetDeepFolderOrCreateId("", uploadFolder) - if err != nil { - logrus.Errorf("Get folder %s id failed: %s", uploadFolder, err) - return - } - dir := filepath.Dir(path) - - if dir != "." { - parentId, err = p.GetDeepFolderOrCreateId(parentId, dir) + var err error + if parentId == "" { + parentId, err = p.GetDeepFolderOrCreateId("", uploadFolder) if err != nil { - logrus.Errorf("Get folder %s id failed: %s\n", dir, err) + logrus.Errorf("Get folder %s id failed: %s", uploadFolder, err) return } + dir := filepath.Dir(path) + + if dir != "." { + parentId, err = p.GetDeepFolderOrCreateId(parentId, dir) + if err != nil { + logrus.Errorf("Get folder %s id failed: %s\n", dir, err) + return + } + } } err = p.UploadFile(parentId, path) if err != nil { @@ -160,9 +167,12 @@ func handleUploadFolder(p *pikpak.PikPak, path string) { // parentId = id // } // } - parentId, err := p.GetDeepFolderOrCreateId("", uploadFolder) - if err != nil { - logrus.Errorf("get folder %s id error: ", uploadFolder, err) + var err error + if parentId != "" { + parentId, err = p.GetDeepFolderOrCreateId("", uploadFolder) + if err != nil { + logrus.Errorf("get folder %s id error: ", uploadFolder, err) + } } logrus.Debug("upload folder: ", uploadFolder, " parentId: ", parentId)