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.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package rpc
  2. import (
  3. "context"
  4. "encoding/hex"
  5. "github.com/google/uuid"
  6. "gitlink.org.cn/cloudream/jcs-pub/common/ecode"
  7. "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/rpc"
  8. corrpc "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/rpc/coordinator"
  9. jcstypes "gitlink.org.cn/cloudream/jcs-pub/common/types"
  10. "gitlink.org.cn/cloudream/jcs-pub/coordinator/internal/db"
  11. "golang.org/x/crypto/bcrypt"
  12. "gorm.io/gorm"
  13. )
  14. func (svc *Service) CreatePubShards(ctx context.Context, msg *corrpc.CreatePubShards) (*corrpc.CreatePubShardsResp, *rpc.CodeError) {
  15. authInfo, ok := rpc.GetAuthInfo(ctx)
  16. if !ok {
  17. return nil, rpc.Failed(ecode.Unauthorized, "unauthorized")
  18. }
  19. passHash, err := bcrypt.GenerateFromPassword([]byte(msg.Password), bcrypt.DefaultCost)
  20. if err != nil {
  21. return nil, rpc.Failed(ecode.OperationFailed, "generate hash: %v", err)
  22. }
  23. cfg, err := db.DoTx01(svc.db, func(tx db.SQLContext) (jcstypes.PubShards, error) {
  24. _, err := svc.db.Hub().GetByID(tx, msg.MasterHub)
  25. if err != nil {
  26. return jcstypes.PubShards{}, err
  27. }
  28. cfg := jcstypes.PubShards{
  29. PubShardsID: jcstypes.PubShardsID(uuid.NewString()),
  30. Password: hex.EncodeToString(passHash),
  31. Creator: authInfo.UserID,
  32. MasterHub: msg.MasterHub,
  33. Name: msg.Name,
  34. Storage: msg.Storage,
  35. Credential: msg.Credential,
  36. ShardStore: msg.ShardStore,
  37. Features: msg.Features,
  38. WorkingDir: msg.WorkingDir,
  39. Revision: 0,
  40. }
  41. err = svc.db.PubShards().Create(svc.db.DefCtx(), cfg)
  42. if err != nil {
  43. return jcstypes.PubShards{}, err
  44. }
  45. return cfg, nil
  46. })
  47. if err != nil {
  48. return nil, rpc.Failed(ecode.OperationFailed, "%v", err)
  49. }
  50. return &corrpc.CreatePubShardsResp{PubShardStore: cfg}, nil
  51. }
  52. func (svc *Service) HubLoadPubShards(ctx context.Context, msg *corrpc.HubLoadPubShards) (*corrpc.HubLoadPubShardsResp, *rpc.CodeError) {
  53. pubShard, err := svc.db.PubShards().Get(svc.db.DefCtx(), msg.PubShardsID)
  54. if err != nil {
  55. if err == gorm.ErrRecordNotFound {
  56. return nil, rpc.Failed(ecode.DataNotFound, "not found")
  57. }
  58. return nil, rpc.Failed(ecode.OperationFailed, "get public shard store: %v", err)
  59. }
  60. return &corrpc.HubLoadPubShardsResp{PubShards: *pubShard}, nil
  61. }
  62. func (svc *Service) UserGetPubShards(ctx context.Context, msg *corrpc.UserGetPubShards) (*corrpc.UserGetPubShardsResp, *rpc.CodeError) {
  63. pubShard, err := svc.db.PubShards().Get(svc.db.DefCtx(), msg.PubShardsID)
  64. if err != nil {
  65. if err == gorm.ErrRecordNotFound {
  66. return nil, rpc.Failed(ecode.DataNotFound, "not found")
  67. }
  68. return nil, rpc.Failed(ecode.OperationFailed, "get public shard store: %v", err)
  69. }
  70. pass, err := hex.DecodeString(pubShard.Password)
  71. if err != nil {
  72. return nil, rpc.Failed(ecode.OperationFailed, "decode password: %v", err)
  73. }
  74. if err := bcrypt.CompareHashAndPassword(pass, []byte(msg.Password)); err != nil {
  75. return nil, rpc.Failed(ecode.Unauthorized, "invalid password")
  76. }
  77. hub, err := svc.db.Hub().GetByID(svc.db.DefCtx(), pubShard.MasterHub)
  78. if err != nil {
  79. return nil, rpc.Failed(ecode.OperationFailed, "get master hub: %v", err)
  80. }
  81. return &corrpc.UserGetPubShardsResp{PubShards: *pubShard, MasterHub: hub}, nil
  82. }

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