You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

getp.go 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package cmdline
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. "path/filepath"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/inhies/go-bytesize"
  11. "github.com/spf13/cobra"
  12. cdssdk "gitlink.org.cn/cloudream/common/sdks/storage"
  13. "gitlink.org.cn/cloudream/storage/client/internal/config"
  14. "gitlink.org.cn/cloudream/storage/common/pkgs/iterator"
  15. )
  16. func init() {
  17. var usePkgID bool
  18. cmd := &cobra.Command{
  19. Use: "getp",
  20. Short: "Download whole package by package id or path",
  21. Args: cobra.ExactArgs(2),
  22. Run: func(cmd *cobra.Command, args []string) {
  23. cmdCtx := GetCmdCtx(cmd)
  24. if usePkgID {
  25. id, err := strconv.ParseInt(args[0], 10, 64)
  26. if err != nil {
  27. fmt.Printf("Invalid package id: %s\n", args[0])
  28. return
  29. }
  30. getpByID(cmdCtx, cdssdk.PackageID(id), args[1])
  31. } else {
  32. getpByPath(cmdCtx, args[0], args[1])
  33. }
  34. },
  35. }
  36. cmd.Flags().BoolVarP(&usePkgID, "id", "i", false, "Download with package id instead of path")
  37. rootCmd.AddCommand(cmd)
  38. }
  39. func getpByPath(cmdCtx *CommandContext, path string, output string) {
  40. userID := cdssdk.UserID(1)
  41. comps := strings.Split(strings.Trim(path, cdssdk.ObjectPathSeparator), cdssdk.ObjectPathSeparator)
  42. if len(comps) != 2 {
  43. fmt.Printf("Package path must be in format of <bucket>/<package>")
  44. return
  45. }
  46. pkg, err := cmdCtx.Cmdline.Svc.PackageSvc().GetByFullName(userID, comps[0], comps[1])
  47. if err != nil {
  48. fmt.Println(err)
  49. return
  50. }
  51. getpByID(cmdCtx, pkg.PackageID, output)
  52. }
  53. func getpByID(cmdCtx *CommandContext, id cdssdk.PackageID, output string) {
  54. userID := cdssdk.UserID(1)
  55. startTime := time.Now()
  56. objIter, err := cmdCtx.Cmdline.Svc.PackageSvc().DownloadPackage(userID, id)
  57. if err != nil {
  58. fmt.Println(err)
  59. return
  60. }
  61. err = os.MkdirAll(output, os.ModePerm)
  62. if err != nil {
  63. fmt.Printf("Create output directory %s failed, err: %v", output, err)
  64. return
  65. }
  66. defer objIter.Close()
  67. madeDirs := make(map[string]bool)
  68. fileCount := 0
  69. totalSize := int64(0)
  70. for {
  71. objInfo, err := objIter.MoveNext()
  72. if err == iterator.ErrNoMoreItem {
  73. break
  74. }
  75. if err != nil {
  76. fmt.Println(err)
  77. return
  78. }
  79. err = func() error {
  80. defer objInfo.File.Close()
  81. fileCount++
  82. totalSize += objInfo.Object.Size
  83. fullPath := filepath.Join(output, objInfo.Object.Path)
  84. dirPath := filepath.Dir(fullPath)
  85. if !madeDirs[dirPath] {
  86. if err := os.MkdirAll(dirPath, 0755); err != nil {
  87. return fmt.Errorf("creating object dir: %w", err)
  88. }
  89. madeDirs[dirPath] = true
  90. }
  91. outputFile, err := os.Create(fullPath)
  92. if err != nil {
  93. return fmt.Errorf("creating object file: %w", err)
  94. }
  95. defer outputFile.Close()
  96. _, err = io.Copy(outputFile, objInfo.File)
  97. if err != nil {
  98. return fmt.Errorf("copy object data to local file failed, err: %w", err)
  99. }
  100. if config.Cfg().StorageID > 0 {
  101. cmdCtx.Cmdline.Svc.AccessStat.AddAccessCounter(objInfo.Object.ObjectID, id, config.Cfg().StorageID, 1)
  102. }
  103. return nil
  104. }()
  105. if err != nil {
  106. fmt.Println(err)
  107. return
  108. }
  109. }
  110. fmt.Printf("Get %v files (%v) to %s in %v.\n", fileCount, bytesize.ByteSize(totalSize), output, time.Since(startTime))
  111. }

本项目旨在将云际存储公共基础设施化,使个人及企业可低门槛使用高效的云际存储服务(安装开箱即用云际存储客户端即可,无需关注其他组件的部署),同时支持用户灵活便捷定制云际存储的功能细节。