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.

pub_shards.go 3.3 kB

4 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package http
  2. import (
  3. "fmt"
  4. "net/http"
  5. "github.com/gin-gonic/gin"
  6. "gitlink.org.cn/cloudream/common/pkgs/logger"
  7. "gitlink.org.cn/cloudream/jcs-pub/client/internal/http/types"
  8. cliapi "gitlink.org.cn/cloudream/jcs-pub/client/sdk/api/v1"
  9. "gitlink.org.cn/cloudream/jcs-pub/common/ecode"
  10. stgglb "gitlink.org.cn/cloudream/jcs-pub/common/globals"
  11. corrpc "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/rpc/coordinator"
  12. jcstypes "gitlink.org.cn/cloudream/jcs-pub/common/types"
  13. )
  14. type PubShardsService struct {
  15. *Server
  16. }
  17. func (s *Server) PubShards() *PubShardsService {
  18. return &PubShardsService{s}
  19. }
  20. func (s *PubShardsService) Create(ctx *gin.Context) {
  21. log := logger.WithField("HTTP", "PubShards.Create")
  22. req, err := types.ShouldBindJSONEx[cliapi.PubShardsCreate](ctx)
  23. if err != nil {
  24. log.Warnf("binding body: %s", err.Error())
  25. ctx.JSON(http.StatusBadRequest, types.Failed(ecode.BadArgument, "%v", err))
  26. return
  27. }
  28. if stgglb.StandaloneMode {
  29. ctx.JSON(http.StatusOK, types.Failed(ecode.OperationFailed, "client is not online"))
  30. return
  31. }
  32. corCli := stgglb.CoordinatorRPCPool.Get()
  33. defer corCli.Release()
  34. resp, cerr := corCli.CreatePubShards(ctx.Request.Context(), &corrpc.CreatePubShards{
  35. Password: req.Password,
  36. MasterHub: req.MasterHub,
  37. Name: req.Name,
  38. Storage: req.Storage,
  39. Credential: req.Credential,
  40. ShardStore: req.ShardStore,
  41. Features: req.Features,
  42. WorkingDir: jcstypes.PathFromJcsPathString(req.WorkingDir),
  43. })
  44. if cerr != nil {
  45. ctx.JSON(http.StatusOK, types.Failed(ecode.ErrorCode(cerr.Code), cerr.Message))
  46. return
  47. }
  48. ctx.JSON(http.StatusOK, types.OK(cliapi.PubShardsCreateResp{
  49. PubShards: resp.PubShardStore,
  50. }))
  51. }
  52. func (s *PubShardsService) Join(ctx *gin.Context) {
  53. log := logger.WithField("HTTP", "PubShards.Join")
  54. var req cliapi.PubShardsJoin
  55. if err := ctx.ShouldBindJSON(&req); err != nil {
  56. log.Warnf("binding body: %s", err.Error())
  57. ctx.JSON(http.StatusBadRequest, types.Failed(ecode.BadArgument, "%v", err))
  58. return
  59. }
  60. if stgglb.StandaloneMode {
  61. ctx.JSON(http.StatusOK, types.Failed(ecode.OperationFailed, "client is not online"))
  62. return
  63. }
  64. corCli := stgglb.CoordinatorRPCPool.Get()
  65. defer corCli.Release()
  66. resp, cerr := corCli.UserGetPubShards(ctx.Request.Context(), &corrpc.UserGetPubShards{
  67. PubShardsID: req.PubShardsID,
  68. Password: req.Password,
  69. })
  70. if cerr != nil {
  71. ctx.JSON(http.StatusOK, types.Failed(ecode.ErrorCode(cerr.Code), cerr.Message))
  72. return
  73. }
  74. resp2, cerr2 := s.svc.UserSpaceSvc().Create(cliapi.UserSpaceCreate{
  75. Name: req.Name,
  76. Storage: &jcstypes.PubShardsType{
  77. Type: "PubShards",
  78. Base: resp.PubShards.Storage,
  79. PubShardsID: req.PubShardsID,
  80. Password: req.Password,
  81. MasterHub: resp.MasterHub.HubID,
  82. },
  83. Credential: resp.PubShards.Credential,
  84. ShardStore: &resp.PubShards.ShardStore,
  85. Features: resp.PubShards.Features,
  86. WorkingDir: resp.PubShards.WorkingDir.PushNew("parts", fmt.Sprintf("%v", s.svc.AccToken.GetToken().UserID)).String(),
  87. })
  88. if cerr2 != nil {
  89. ctx.JSON(http.StatusOK, types.Failed(ecode.ErrorCode(cerr2.Code), cerr2.Message))
  90. return
  91. }
  92. ctx.JSON(http.StatusOK, types.OK(cliapi.PubShardsJoinResp{
  93. PubShards: resp.PubShards,
  94. UserSpace: resp2.UserSpace,
  95. }))
  96. }

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