Skip to content

Commit

Permalink
add parent id in share and upload commnad.
Browse files Browse the repository at this point in the history
  • Loading branch information
52funny committed Mar 18, 2023
1 parent 9da0075 commit 9453e85
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 30 deletions.
36 changes: 19 additions & 17 deletions cmd/share/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package share
import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/52funny/pikpakcli/conf"
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
36 changes: 23 additions & 13 deletions cmd/upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 9453e85

Please sign in to comment.