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.

package.go 8.5 kB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. package cmdline
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. "path/filepath"
  7. "time"
  8. "github.com/jedib0t/go-pretty/v6/table"
  9. "gitlink.org.cn/cloudream/common/models"
  10. "gitlink.org.cn/cloudream/storage/client/internal/config"
  11. "gitlink.org.cn/cloudream/storage/common/pkgs/iterator"
  12. )
  13. func PackageListBucketPackages(ctx CommandContext, bucketID int64) error {
  14. userID := int64(0)
  15. packages, err := ctx.Cmdline.Svc.BucketSvc().GetBucketPackages(userID, bucketID)
  16. if err != nil {
  17. return err
  18. }
  19. fmt.Printf("Find %d packages in bucket %d for user %d:\n", len(packages), bucketID, userID)
  20. tb := table.NewWriter()
  21. tb.AppendHeader(table.Row{"ID", "Name", "BucketID", "State", "Redundancy"})
  22. for _, obj := range packages {
  23. tb.AppendRow(table.Row{obj.PackageID, obj.Name, obj.BucketID, obj.State, obj.Redundancy})
  24. }
  25. fmt.Print(tb.Render())
  26. return nil
  27. }
  28. func PackageDownloadPackage(ctx CommandContext, outputDir string, packageID int64) error {
  29. err := os.MkdirAll(outputDir, os.ModePerm)
  30. if err != nil {
  31. return fmt.Errorf("create output directory %s failed, err: %w", outputDir, err)
  32. }
  33. // 下载文件
  34. objIter, err := ctx.Cmdline.Svc.PackageSvc().DownloadPackage(0, packageID)
  35. if err != nil {
  36. return fmt.Errorf("download object failed, err: %w", err)
  37. }
  38. defer objIter.Close()
  39. for {
  40. objInfo, err := objIter.MoveNext()
  41. if err == iterator.ErrNoMoreItem {
  42. break
  43. }
  44. if err != nil {
  45. return err
  46. }
  47. err = func() error {
  48. defer objInfo.File.Close()
  49. fullPath := filepath.Join(outputDir, objInfo.Object.Path)
  50. dirPath := filepath.Dir(fullPath)
  51. if err := os.MkdirAll(dirPath, 0755); err != nil {
  52. return fmt.Errorf("creating object dir: %w", err)
  53. }
  54. outputFile, err := os.Create(fullPath)
  55. if err != nil {
  56. return fmt.Errorf("creating object file: %w", err)
  57. }
  58. defer outputFile.Close()
  59. _, err = io.Copy(outputFile, objInfo.File)
  60. if err != nil {
  61. return fmt.Errorf("copy object data to local file failed, err: %w", err)
  62. }
  63. return nil
  64. }()
  65. if err != nil {
  66. return err
  67. }
  68. }
  69. return nil
  70. }
  71. func PackageUploadRepPackage(ctx CommandContext, rootPath string, bucketID int64, name string, repCount int) error {
  72. rootPath = filepath.Clean(rootPath)
  73. var uploadFilePathes []string
  74. err := filepath.WalkDir(rootPath, func(fname string, fi os.DirEntry, err error) error {
  75. if err != nil {
  76. return nil
  77. }
  78. if !fi.IsDir() {
  79. uploadFilePathes = append(uploadFilePathes, fname)
  80. }
  81. return nil
  82. })
  83. if err != nil {
  84. return fmt.Errorf("open directory %s failed, err: %w", rootPath, err)
  85. }
  86. objIter := iterator.NewUploadingObjectIterator(rootPath, uploadFilePathes)
  87. taskID, err := ctx.Cmdline.Svc.PackageSvc().StartCreatingRepPackage(0, bucketID, name, objIter, models.NewRepRedundancyInfo(repCount))
  88. if err != nil {
  89. return fmt.Errorf("upload file data failed, err: %w", err)
  90. }
  91. for {
  92. complete, uploadObjectResult, err := ctx.Cmdline.Svc.PackageSvc().WaitCreatingRepPackage(taskID, time.Second*5)
  93. if complete {
  94. if err != nil {
  95. return fmt.Errorf("uploading rep object: %w", err)
  96. }
  97. tb := table.NewWriter()
  98. tb.AppendHeader(table.Row{"Path", "ObjectID", "FileHash"})
  99. for i := 0; i < len(uploadObjectResult.ObjectResults); i++ {
  100. tb.AppendRow(table.Row{
  101. uploadObjectResult.ObjectResults[i].Info.Path,
  102. uploadObjectResult.ObjectResults[i].ObjectID,
  103. uploadObjectResult.ObjectResults[i].FileHash,
  104. })
  105. }
  106. fmt.Print(tb.Render())
  107. return nil
  108. }
  109. if err != nil {
  110. return fmt.Errorf("wait uploading: %w", err)
  111. }
  112. }
  113. }
  114. func PackageUpdateRepPackage(ctx CommandContext, packageID int64, rootPath string) error {
  115. //userID := int64(0)
  116. var uploadFilePathes []string
  117. err := filepath.WalkDir(rootPath, func(fname string, fi os.DirEntry, err error) error {
  118. if err != nil {
  119. return nil
  120. }
  121. if !fi.IsDir() {
  122. uploadFilePathes = append(uploadFilePathes, fname)
  123. }
  124. return nil
  125. })
  126. if err != nil {
  127. return fmt.Errorf("open directory %s failed, err: %w", rootPath, err)
  128. }
  129. objIter := iterator.NewUploadingObjectIterator(rootPath, uploadFilePathes)
  130. taskID, err := ctx.Cmdline.Svc.PackageSvc().StartUpdatingRepPackage(0, packageID, objIter)
  131. if err != nil {
  132. return fmt.Errorf("update object %d failed, err: %w", packageID, err)
  133. }
  134. for {
  135. complete, _, err := ctx.Cmdline.Svc.PackageSvc().WaitUpdatingRepPackage(taskID, time.Second*5)
  136. if complete {
  137. if err != nil {
  138. return fmt.Errorf("updating rep object: %w", err)
  139. }
  140. return nil
  141. }
  142. if err != nil {
  143. return fmt.Errorf("wait updating: %w", err)
  144. }
  145. }
  146. }
  147. func PackageUploadECPackage(ctx CommandContext, rootPath string, bucketID int64, name string, ecName string) error {
  148. var uploadFilePathes []string
  149. err := filepath.WalkDir(rootPath, func(fname string, fi os.DirEntry, err error) error {
  150. if err != nil {
  151. return nil
  152. }
  153. if !fi.IsDir() {
  154. uploadFilePathes = append(uploadFilePathes, fname)
  155. }
  156. return nil
  157. })
  158. if err != nil {
  159. return fmt.Errorf("open directory %s failed, err: %w", rootPath, err)
  160. }
  161. objIter := iterator.NewUploadingObjectIterator(rootPath, uploadFilePathes)
  162. taskID, err := ctx.Cmdline.Svc.PackageSvc().StartCreatingECPackage(0, bucketID, name, objIter, models.NewECRedundancyInfo(ecName, config.Cfg().ECPacketSize))
  163. if err != nil {
  164. return fmt.Errorf("upload file data failed, err: %w", err)
  165. }
  166. for {
  167. complete, uploadObjectResult, err := ctx.Cmdline.Svc.PackageSvc().WaitCreatingRepPackage(taskID, time.Second*5)
  168. if complete {
  169. if err != nil {
  170. return fmt.Errorf("uploading ec package: %w", err)
  171. }
  172. tb := table.NewWriter()
  173. tb.AppendHeader(table.Row{"Path", "ObjectID", "FileHash"})
  174. for i := 0; i < len(uploadObjectResult.ObjectResults); i++ {
  175. tb.AppendRow(table.Row{
  176. uploadObjectResult.ObjectResults[i].Info.Path,
  177. uploadObjectResult.ObjectResults[i].ObjectID,
  178. uploadObjectResult.ObjectResults[i].FileHash,
  179. })
  180. }
  181. fmt.Print(tb.Render())
  182. return nil
  183. }
  184. if err != nil {
  185. return fmt.Errorf("wait uploading: %w", err)
  186. }
  187. }
  188. }
  189. func PackageUpdateECPackage(ctx CommandContext, packageID int64, rootPath string) error {
  190. //userID := int64(0)
  191. var uploadFilePathes []string
  192. err := filepath.WalkDir(rootPath, func(fname string, fi os.DirEntry, err error) error {
  193. if err != nil {
  194. return nil
  195. }
  196. if !fi.IsDir() {
  197. uploadFilePathes = append(uploadFilePathes, fname)
  198. }
  199. return nil
  200. })
  201. if err != nil {
  202. return fmt.Errorf("open directory %s failed, err: %w", rootPath, err)
  203. }
  204. objIter := iterator.NewUploadingObjectIterator(rootPath, uploadFilePathes)
  205. taskID, err := ctx.Cmdline.Svc.PackageSvc().StartUpdatingECPackage(0, packageID, objIter)
  206. if err != nil {
  207. return fmt.Errorf("update package %d failed, err: %w", packageID, err)
  208. }
  209. for {
  210. complete, _, err := ctx.Cmdline.Svc.PackageSvc().WaitUpdatingECPackage(taskID, time.Second*5)
  211. if complete {
  212. if err != nil {
  213. return fmt.Errorf("updating ec package: %w", err)
  214. }
  215. return nil
  216. }
  217. if err != nil {
  218. return fmt.Errorf("wait updating: %w", err)
  219. }
  220. }
  221. }
  222. func PackageDeletePackage(ctx CommandContext, packageID int64) error {
  223. userID := int64(0)
  224. err := ctx.Cmdline.Svc.PackageSvc().DeletePackage(userID, packageID)
  225. if err != nil {
  226. return fmt.Errorf("delete package %d failed, err: %w", packageID, err)
  227. }
  228. return nil
  229. }
  230. func PackageGetCachedNodes(ctx CommandContext, packageID int64, userID int64) error {
  231. resp, err := ctx.Cmdline.Svc.PackageSvc().GetCachedNodes(userID, packageID)
  232. fmt.Printf("resp: %v\n", resp)
  233. if err != nil {
  234. return fmt.Errorf("get package %d cached nodes failed, err: %w", packageID, err)
  235. }
  236. return nil
  237. }
  238. func PackageGetLoadedNodes(ctx CommandContext, packageID int64, userID int64) error {
  239. nodeIDs, err := ctx.Cmdline.Svc.PackageSvc().GetLoadedNodes(userID, packageID)
  240. fmt.Printf("nodeIDs: %v\n", nodeIDs)
  241. if err != nil {
  242. return fmt.Errorf("get package %d loaded nodes failed, err: %w", packageID, err)
  243. }
  244. return nil
  245. }
  246. func init() {
  247. commands.MustAdd(PackageListBucketPackages, "pkg", "ls")
  248. commands.MustAdd(PackageDownloadPackage, "pkg", "get")
  249. commands.MustAdd(PackageUploadRepPackage, "pkg", "new", "rep")
  250. commands.MustAdd(PackageUpdateRepPackage, "pkg", "update", "rep")
  251. commands.MustAdd(PackageUploadRepPackage, "pkg", "new", "ec")
  252. commands.MustAdd(PackageUpdateRepPackage, "pkg", "update", "ec")
  253. commands.MustAdd(PackageDeletePackage, "pkg", "delete")
  254. commands.MustAdd(PackageGetCachedNodes, "pkg", "cached")
  255. commands.MustAdd(PackageGetLoadedNodes, "pkg", "loaded")
  256. }

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