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

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

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