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.

object.go 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package services
  2. import (
  3. "fmt"
  4. "time"
  5. cdssdk "gitlink.org.cn/cloudream/common/sdks/storage"
  6. mytask "gitlink.org.cn/cloudream/storage/client/internal/task"
  7. stgglb "gitlink.org.cn/cloudream/storage/common/globals"
  8. stgmod "gitlink.org.cn/cloudream/storage/common/models"
  9. "gitlink.org.cn/cloudream/storage/common/pkgs/db/model"
  10. "gitlink.org.cn/cloudream/storage/common/pkgs/iterator"
  11. coormq "gitlink.org.cn/cloudream/storage/common/pkgs/mq/coordinator"
  12. )
  13. type ObjectService struct {
  14. *Service
  15. }
  16. func (svc *Service) ObjectSvc() *ObjectService {
  17. return &ObjectService{Service: svc}
  18. }
  19. func (svc *ObjectService) StartUploading(userID cdssdk.UserID, packageID cdssdk.PackageID, objIter iterator.UploadingObjectIterator, nodeAffinity *cdssdk.NodeID) (string, error) {
  20. tsk := svc.TaskMgr.StartNew(mytask.NewUploadObjects(userID, packageID, objIter, nodeAffinity))
  21. return tsk.ID(), nil
  22. }
  23. func (svc *ObjectService) WaitUploading(taskID string, waitTimeout time.Duration) (bool, *mytask.UploadObjectsResult, error) {
  24. tsk := svc.TaskMgr.FindByID(taskID)
  25. if tsk.WaitTimeout(waitTimeout) {
  26. updatePkgTask := tsk.Body().(*mytask.UploadObjects)
  27. return true, updatePkgTask.Result, tsk.Error()
  28. }
  29. return false, nil, nil
  30. }
  31. func (svc *ObjectService) UpdateInfo(userID cdssdk.UserID, updatings []cdssdk.UpdatingObject) ([]cdssdk.ObjectID, error) {
  32. coorCli, err := stgglb.CoordinatorMQPool.Acquire()
  33. if err != nil {
  34. return nil, fmt.Errorf("new coordinator client: %w", err)
  35. }
  36. defer stgglb.CoordinatorMQPool.Release(coorCli)
  37. resp, err := coorCli.UpdateObjectInfos(coormq.ReqUpdateObjectInfos(userID, updatings))
  38. if err != nil {
  39. return nil, fmt.Errorf("requsting to coodinator: %w", err)
  40. }
  41. return resp.Successes, nil
  42. }
  43. func (svc *ObjectService) Move(userID cdssdk.UserID, movings []cdssdk.MovingObject) ([]cdssdk.ObjectID, error) {
  44. coorCli, err := stgglb.CoordinatorMQPool.Acquire()
  45. if err != nil {
  46. return nil, fmt.Errorf("new coordinator client: %w", err)
  47. }
  48. defer stgglb.CoordinatorMQPool.Release(coorCli)
  49. resp, err := coorCli.MoveObjects(coormq.ReqMoveObjects(userID, movings))
  50. if err != nil {
  51. return nil, fmt.Errorf("requsting to coodinator: %w", err)
  52. }
  53. return resp.Successes, nil
  54. }
  55. func (svc *ObjectService) Download(userID cdssdk.UserID, objectID cdssdk.ObjectID) (*iterator.IterDownloadingObject, error) {
  56. coorCli, err := stgglb.CoordinatorMQPool.Acquire()
  57. if err != nil {
  58. return nil, fmt.Errorf("new coordinator client: %w", err)
  59. }
  60. defer stgglb.CoordinatorMQPool.Release(coorCli)
  61. resp, err := coorCli.GetObjectDetails(coormq.ReqGetObjectDetails([]cdssdk.ObjectID{objectID}))
  62. if err != nil {
  63. return nil, fmt.Errorf("requesting to coordinator")
  64. }
  65. if resp.Objects[0] == nil {
  66. return nil, fmt.Errorf("object not found")
  67. }
  68. iter := iterator.NewDownloadObjectIterator([]stgmod.ObjectDetail{*resp.Objects[0]}, &iterator.DownloadContext{
  69. Distlock: svc.DistLock,
  70. })
  71. defer iter.Close()
  72. downloading, err := iter.MoveNext()
  73. if err != nil {
  74. return nil, err
  75. }
  76. return downloading, nil
  77. }
  78. func (svc *ObjectService) Delete(userID cdssdk.UserID, objectIDs []cdssdk.ObjectID) error {
  79. coorCli, err := stgglb.CoordinatorMQPool.Acquire()
  80. if err != nil {
  81. return fmt.Errorf("new coordinator client: %w", err)
  82. }
  83. defer stgglb.CoordinatorMQPool.Release(coorCli)
  84. _, err = coorCli.DeleteObjects(coormq.ReqDeleteObjects(userID, objectIDs))
  85. if err != nil {
  86. return fmt.Errorf("requsting to coodinator: %w", err)
  87. }
  88. return nil
  89. }
  90. func (svc *ObjectService) GetPackageObjects(userID cdssdk.UserID, packageID cdssdk.PackageID) ([]model.Object, error) {
  91. coorCli, err := stgglb.CoordinatorMQPool.Acquire()
  92. if err != nil {
  93. return nil, fmt.Errorf("new coordinator client: %w", err)
  94. }
  95. defer stgglb.CoordinatorMQPool.Release(coorCli)
  96. getResp, err := coorCli.GetPackageObjects(coormq.NewGetPackageObjects(userID, packageID))
  97. if err != nil {
  98. return nil, fmt.Errorf("requsting to coodinator: %w", err)
  99. }
  100. return getResp.Objects, nil
  101. }

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