Skip to content

Commit

Permalink
增加显示app_id
Browse files Browse the repository at this point in the history
Fix #377
  • Loading branch information
iikira committed Sep 19, 2018
1 parent 4f4e95b commit d03f680
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 25 deletions.
32 changes: 22 additions & 10 deletions baidupcs/file_directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ const (
)

type (
// HandleFileDirectoryFunc 处理文件或目录的元信息
HandleFileDirectoryFunc func(depth int, fd *FileDirectory)
// HandleFileDirectoryFunc 处理文件或目录的元信息, 返回值控制是否退出递归
HandleFileDirectoryFunc func(depth int, fd *FileDirectory) bool

// FileDirectory 文件或目录的元信息
FileDirectory struct {
FsID int64 // fs_id
AppID int64 // app_id
Path string // 路径
Filename string // 文件名 或 目录名
Ctime int64 // 创建日期
Expand All @@ -59,7 +60,8 @@ type (

// fdJSON 用于解析远程JSON数据
fdJSON struct {
FsID int64 `json:"fs_id"` // fs_id
FsID int64 `json:"fs_id"` // fs_id
AppID int64 `json:"app_id"`
Path string `json:"path"` // 路径
Filename string `json:"server_filename"` // 文件名 或 目录名
Ctime int64 `json:"ctime"` // 创建日期
Expand Down Expand Up @@ -211,30 +213,39 @@ func (pcs *BaiduPCS) Search(targetPath, keyword string, recursive bool) (fdl Fil
return
}

func (pcs *BaiduPCS) recurseList(path string, depth int, options *OrderOptions, handleFileDirectoryFunc HandleFileDirectoryFunc) (data FileDirectoryList, pcsError pcserror.Error) {
fdl, pcsError := pcs.FilesDirectoriesList(path, options)
func (pcs *BaiduPCS) recurseList(path string, depth int, options *OrderOptions, handleFileDirectoryFunc HandleFileDirectoryFunc) (fdl FileDirectoryList, ok bool, pcsError pcserror.Error) {
fdl, pcsError = pcs.FilesDirectoriesList(path, options)
if pcsError != nil {
return nil, pcsError
return nil, true, pcsError
}

for k := range fdl {
handleFileDirectoryFunc(depth+1, fdl[k])
ok = handleFileDirectoryFunc(depth+1, fdl[k])
if !ok {
return
}

if !fdl[k].Isdir {
continue
}

fdl[k].Children, pcsError = pcs.recurseList(fdl[k].Path, depth+1, options, handleFileDirectoryFunc)
fdl[k].Children, ok, pcsError = pcs.recurseList(fdl[k].Path, depth+1, options, handleFileDirectoryFunc)
if !ok {
return
}
if pcsError != nil {
// 未进行错误处理
pcsverbose.Verboseln(pcsError)
}
}

return fdl, nil
return fdl, true, nil
}

// FilesDirectoriesRecurseList 递归获取目录下的文件和目录列表
func (pcs *BaiduPCS) FilesDirectoriesRecurseList(path string, options *OrderOptions, handleFileDirectoryFunc HandleFileDirectoryFunc) (data FileDirectoryList, pcsError pcserror.Error) {
return pcs.recurseList(path, 0, options, handleFileDirectoryFunc)
data, _, pcsError = pcs.recurseList(path, 0, options, handleFileDirectoryFunc)
return data, pcsError
}

func (f *FileDirectory) String() string {
Expand Down Expand Up @@ -264,6 +275,7 @@ func (f *FileDirectory) String() string {
})
}

tb.Append([]string{"app_id", strconv.FormatInt(f.AppID, 10)})
tb.Append([]string{"fs_id", strconv.FormatInt(f.FsID, 10)})
tb.AppendBulk([][]string{
[]string{"创建日期", pcstime.FormatTime(f.Ctime)},
Expand Down
40 changes: 33 additions & 7 deletions internal/pcscommand/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func RunDownload(paths []string, options *DownloadOptions) {
options.Out = os.Stdout
}

if options.Load < 1 {
if options.Load <= 0 {
options.Load = pcsconfig.Config.MaxDownloadLoad()
}

Expand All @@ -249,23 +249,49 @@ func RunDownload(paths []string, options *DownloadOptions) {
options.Parallel = pcsconfig.Config.MaxParallel()
}

cfg.MaxParallel = pcsconfig.AverageParallel(options.Parallel, options.Load)

paths, err := getAllAbsPaths(paths...)
if err != nil {
fmt.Println(err)
return
}

fmt.Fprintf(options.Out, "\n")
fmt.Fprintf(options.Out, "[0] 提示: 当前下载最大并发量为: %d, 下载缓存为: %d\n", cfg.MaxParallel, cfg.CacheSize)
fmt.Fprintf(options.Out, "[0] 提示: 当前下载最大并发量为: %d, 下载缓存为: %d\n", options.Parallel, cfg.CacheSize)

var (
pcs = GetBaiduPCS()
dlist = lane.NewDeque()
lastID = 0
pcs = GetBaiduPCS()
dlist = lane.NewDeque()
lastID = 0
loadCount = 0
)

// 预测要下载的文件数量
// TODO: pcscache
for k := range paths {
pcs.FilesDirectoriesRecurseList(paths[k], baidupcs.DefaultOrderOptions, func(depth int, fd *baidupcs.FileDirectory) bool {
if !fd.Isdir {
loadCount++
if loadCount >= options.Load {
return false
}
}
return true
})

if loadCount >= options.Load {
break
}
}
// 修改Load, 设置MaxParallel
if loadCount > 0 {
options.Load = loadCount
// 取平均值
cfg.MaxParallel = pcsconfig.AverageParallel(options.Parallel, loadCount)
} else {
cfg.MaxParallel = options.Parallel
}

// 处理队列
for k := range paths {
lastID++
ptask := &dtask{
Expand Down
5 changes: 3 additions & 2 deletions internal/pcscommand/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ func RunExport(pcspaths []string, rootPath string) {
d int
cmdStr string
)
pcs.FilesDirectoriesRecurseList(pcspath, baidupcs.DefaultOrderOptions, func(depth int, fd *baidupcs.FileDirectory) {
pcs.FilesDirectoriesRecurseList(pcspath, baidupcs.DefaultOrderOptions, func(depth int, fd *baidupcs.FileDirectory) bool {
if fd.Isdir {
if depth > d {
d = depth
} else {
fmt.Printf("BaiduPCS-Go mkdir \"%s\"\n", getPath(fd.Path))
d = 0
}
return
return true
}

cmdStr = fmt.Sprintf("BaiduPCS-Go rapidupload -length=%d -md5=%s \"%s\"\n", fd.Size, fd.MD5, getPath(fd.Path))
Expand All @@ -57,6 +57,7 @@ func RunExport(pcspaths []string, rootPath string) {
} else {
fmt.Print(cmdStr)
}
return true
})
}

Expand Down
8 changes: 4 additions & 4 deletions internal/pcscommand/ls_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ func renderTable(op int, isTotal bool, path string, files baidupcs.FileDirectory
}

if isTotal {
tb.SetHeader([]string{"#", "fs_id", "文件大小", "创建日期", "修改日期", "md5(截图请打码)", showPath})
tb.SetHeader([]string{"#", "fs_id", "app_id", "文件大小", "创建日期", "修改日期", "md5(截图请打码)", showPath})
tb.SetColumnAlignment([]int{tablewriter.ALIGN_DEFAULT, tablewriter.ALIGN_RIGHT, tablewriter.ALIGN_RIGHT, tablewriter.ALIGN_LEFT, tablewriter.ALIGN_LEFT, tablewriter.ALIGN_LEFT, tablewriter.ALIGN_LEFT})
for k, file := range files {
if file.Isdir {
tb.Append([]string{strconv.Itoa(k), strconv.FormatInt(file.FsID, 10), "-", pcstime.FormatTime(file.Ctime), pcstime.FormatTime(file.Mtime), file.MD5, file.Filename + "/"})
tb.Append([]string{strconv.Itoa(k), strconv.FormatInt(file.FsID, 10), strconv.FormatInt(file.AppID, 10), "-", pcstime.FormatTime(file.Ctime), pcstime.FormatTime(file.Mtime), file.MD5, file.Filename + "/"})
continue
}

Expand All @@ -105,9 +105,9 @@ func renderTable(op int, isTotal bool, path string, files baidupcs.FileDirectory

switch op {
case opLs:
tb.Append([]string{strconv.Itoa(k), strconv.FormatInt(file.FsID, 10), converter.ConvertFileSize(file.Size, 2), pcstime.FormatTime(file.Ctime), pcstime.FormatTime(file.Mtime), md5, file.Filename})
tb.Append([]string{strconv.Itoa(k), strconv.FormatInt(file.FsID, 10), strconv.FormatInt(file.AppID, 10), converter.ConvertFileSize(file.Size, 2), pcstime.FormatTime(file.Ctime), pcstime.FormatTime(file.Mtime), md5, file.Filename})
case opSearch:
tb.Append([]string{strconv.Itoa(k), strconv.FormatInt(file.FsID, 10), converter.ConvertFileSize(file.Size, 2), pcstime.FormatTime(file.Ctime), pcstime.FormatTime(file.Mtime), md5, file.Path})
tb.Append([]string{strconv.Itoa(k), strconv.FormatInt(file.FsID, 10), strconv.FormatInt(file.AppID, 10), converter.ConvertFileSize(file.Size, 2), pcstime.FormatTime(file.Ctime), pcstime.FormatTime(file.Mtime), md5, file.Path})
}
}
fN, dN = files.Count()
Expand Down
7 changes: 5 additions & 2 deletions internal/pcsfunctions/pcsupload/upload_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ func NewUploadingDatabase() (ud *UploadingDatabase, err error) {
}

d := jsoniter.NewDecoder(file)

err = d.Decode(ud)
return ud, err
if err != nil {
return nil, err
}

return ud, nil
}

// Save 保存内容
Expand Down

0 comments on commit d03f680

Please sign in to comment.