JCS-pub/hub/internal/rpc/pub_shards.go

105 lines
3.1 KiB
Go

package rpc
import (
"context"
"gitlink.org.cn/cloudream/jcs-pub/common/ecode"
"gitlink.org.cn/cloudream/jcs-pub/common/pkgs/rpc"
hubrpc "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/rpc/hub"
)
func (svc *Service) PubShardsStore(ctx context.Context, msg *hubrpc.PubShardsStore) (*hubrpc.PubShardsStoreResp, *rpc.CodeError) {
authInfo, ok := rpc.GetAuthInfo(ctx)
if !ok {
return nil, rpc.Failed(ecode.Unauthorized, "unauthorized")
}
pubShards, cerr := svc.pubShards.GetOrLoad(msg.PubShardsID, msg.Password)
if cerr != nil {
return nil, rpc.Failed(ecode.OperationFailed, "load pub shards store: %v", cerr)
}
info, err := pubShards.StoreShard(authInfo.UserID, msg.Path, msg.Hash, msg.Size)
if err != nil {
return nil, rpc.Failed(ecode.OperationFailed, "store file: %v", err)
}
return &hubrpc.PubShardsStoreResp{
Info: info,
}, nil
}
func (svc *Service) PubShardsInfo(ctx context.Context, msg *hubrpc.PubShardsInfo) (*hubrpc.PubShardsInfoResp, *rpc.CodeError) {
pubShards, err := svc.pubShards.GetOrLoad(msg.PubShardsID, msg.Password)
if err != nil {
return nil, rpc.Failed(ecode.OperationFailed, "load pub shards store: %v", err)
}
info, err := pubShards.InfoShard(msg.FileHash)
if err != nil {
return nil, rpc.Failed(ecode.OperationFailed, err.Error())
}
return &hubrpc.PubShardsInfoResp{
Info: info,
}, nil
}
func (svc *Service) PubShardsListAll(ctx context.Context, msg *hubrpc.PubShardsListAll) (*hubrpc.PubShardsListAllResp, *rpc.CodeError) {
authInfo, ok := rpc.GetAuthInfo(ctx)
if !ok {
return nil, rpc.Failed(ecode.Unauthorized, "unauthorized")
}
pubShards, err := svc.pubShards.GetOrLoad(msg.PubShardsID, msg.Password)
if err != nil {
return nil, rpc.Failed(ecode.OperationFailed, "load pub shards store: %v", err)
}
infos, err := pubShards.ListUserAll(authInfo.UserID)
if err != nil {
return nil, rpc.Failed(ecode.OperationFailed, "list all: %v", err)
}
return &hubrpc.PubShardsListAllResp{
Infos: infos,
}, nil
}
func (svc *Service) PubShardsGC(ctx context.Context, msg *hubrpc.PubShardsGC) (*hubrpc.PubShardsGCResp, *rpc.CodeError) {
authInfo, ok := rpc.GetAuthInfo(ctx)
if !ok {
return nil, rpc.Failed(ecode.Unauthorized, "unauthorized")
}
pubShards, err := svc.pubShards.GetOrLoad(msg.PubShardsID, msg.Password)
if err != nil {
return nil, rpc.Failed(ecode.OperationFailed, "load pub shards store: %v", err)
}
err = pubShards.GC(authInfo.UserID, msg.FileHashes)
if err != nil {
return nil, rpc.Failed(ecode.OperationFailed, "reset hashes: %v", err)
}
return &hubrpc.PubShardsGCResp{}, nil
}
func (svc *Service) PubShardsStats(ctx context.Context, msg *hubrpc.PubShardsStats) (*hubrpc.PubShardsStatsResp, *rpc.CodeError) {
authInfo, ok := rpc.GetAuthInfo(ctx)
if !ok {
return nil, rpc.Failed(ecode.Unauthorized, "unauthorized")
}
pubShards, err := svc.pubShards.GetOrLoad(msg.PubShardsID, msg.Password)
if err != nil {
return nil, rpc.Failed(ecode.OperationFailed, "load pub shards store: %v", err)
}
stats := pubShards.GetUserStats(authInfo.UserID)
return &hubrpc.PubShardsStatsResp{
Stats: stats,
}, nil
}