|
- package http
-
- import (
- "fmt"
- "net/http"
-
- "github.com/gin-gonic/gin"
- "gitlink.org.cn/cloudream/common/pkgs/logger"
- "gitlink.org.cn/cloudream/jcs-pub/client/internal/http/types"
- cliapi "gitlink.org.cn/cloudream/jcs-pub/client/sdk/api/v1"
- "gitlink.org.cn/cloudream/jcs-pub/common/ecode"
- stgglb "gitlink.org.cn/cloudream/jcs-pub/common/globals"
- corrpc "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/rpc/coordinator"
- jcstypes "gitlink.org.cn/cloudream/jcs-pub/common/types"
- )
-
- type PubShardsService struct {
- *Server
- }
-
- func (s *Server) PubShards() *PubShardsService {
- return &PubShardsService{s}
- }
-
- func (s *PubShardsService) Create(ctx *gin.Context) {
- log := logger.WithField("HTTP", "PubShards.Create")
-
- req, err := types.ShouldBindJSONEx[cliapi.PubShardsCreate](ctx)
- if err != nil {
- log.Warnf("binding body: %s", err.Error())
- ctx.JSON(http.StatusBadRequest, types.Failed(ecode.BadArgument, "%v", err))
- return
- }
-
- if stgglb.StandaloneMode {
- ctx.JSON(http.StatusOK, types.Failed(ecode.OperationFailed, "client is not online"))
- return
- }
-
- corCli := stgglb.CoordinatorRPCPool.Get()
- defer corCli.Release()
-
- resp, cerr := corCli.CreatePubShards(ctx.Request.Context(), &corrpc.CreatePubShards{
- Password: req.Password,
- MasterHub: req.MasterHub,
- Name: req.Name,
- Storage: req.Storage,
- Credential: req.Credential,
- ShardStore: req.ShardStore,
- Features: req.Features,
- WorkingDir: jcstypes.PathFromJcsPathString(req.WorkingDir),
- })
- if cerr != nil {
- ctx.JSON(http.StatusOK, types.Failed(ecode.ErrorCode(cerr.Code), cerr.Message))
- return
- }
- ctx.JSON(http.StatusOK, types.OK(cliapi.PubShardsCreateResp{
- PubShards: resp.PubShardStore,
- }))
- }
-
- func (s *PubShardsService) Join(ctx *gin.Context) {
- log := logger.WithField("HTTP", "PubShards.Join")
-
- var req cliapi.PubShardsJoin
- if err := ctx.ShouldBindJSON(&req); err != nil {
- log.Warnf("binding body: %s", err.Error())
- ctx.JSON(http.StatusBadRequest, types.Failed(ecode.BadArgument, "%v", err))
- return
- }
-
- if stgglb.StandaloneMode {
- ctx.JSON(http.StatusOK, types.Failed(ecode.OperationFailed, "client is not online"))
- return
- }
-
- corCli := stgglb.CoordinatorRPCPool.Get()
- defer corCli.Release()
-
- resp, cerr := corCli.UserGetPubShards(ctx.Request.Context(), &corrpc.UserGetPubShards{
- PubShardsID: req.PubShardsID,
- Password: req.Password,
- })
- if cerr != nil {
- ctx.JSON(http.StatusOK, types.Failed(ecode.ErrorCode(cerr.Code), cerr.Message))
- return
- }
-
- resp2, cerr2 := s.svc.UserSpaceSvc().Create(cliapi.UserSpaceCreate{
- Name: req.Name,
- Storage: &jcstypes.PubShardsType{
- Type: "PubShards",
- Base: resp.PubShards.Storage,
- PubShardsID: req.PubShardsID,
- Password: req.Password,
- MasterHub: resp.MasterHub.HubID,
- },
- Credential: resp.PubShards.Credential,
- ShardStore: &resp.PubShards.ShardStore,
- Features: resp.PubShards.Features,
- WorkingDir: resp.PubShards.WorkingDir.PushNew("parts", fmt.Sprintf("%v", s.svc.AccToken.GetToken().UserID)).String(),
- })
- if cerr2 != nil {
- ctx.JSON(http.StatusOK, types.Failed(ecode.ErrorCode(cerr2.Code), cerr2.Message))
- return
- }
-
- ctx.JSON(http.StatusOK, types.OK(cliapi.PubShardsJoinResp{
- PubShards: resp.PubShards,
- UserSpace: resp2.UserSpace,
- }))
- }
|