mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-26 03:26:57 +08:00
Refactor code
This commit is contained in:
parent
5d8e972ac7
commit
655f7ff7d0
@ -129,7 +129,7 @@ func AddFriendResponse(c *gin.Context) {
|
|||||||
req := &rpc.AddFriendResponseReq{CommID: &rpc.CommID{}}
|
req := &rpc.AddFriendResponseReq{CommID: &rpc.CommID{}}
|
||||||
utils.CopyStructFields(req.CommID, ¶ms.ParamsCommFriend)
|
utils.CopyStructFields(req.CommID, ¶ms.ParamsCommFriend)
|
||||||
req.HandleMsg = params.HandleMsg
|
req.HandleMsg = params.HandleMsg
|
||||||
req.Flag = params.Flag
|
req.HandleResult = params.Flag
|
||||||
var ok bool
|
var ok bool
|
||||||
ok, req.CommID.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token"))
|
ok, req.CommID.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token"))
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -1,51 +1,37 @@
|
|||||||
package apiThird
|
package apiThird
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
api "Open_IM/pkg/base_info"
|
||||||
"Open_IM/pkg/common/config"
|
"Open_IM/pkg/common/config"
|
||||||
"Open_IM/pkg/common/constant"
|
"Open_IM/pkg/common/constant"
|
||||||
log2 "Open_IM/pkg/common/log"
|
"Open_IM/pkg/common/log"
|
||||||
|
"Open_IM/pkg/common/token_verify"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
sts "github.com/tencentyun/qcloud-cos-sts-sdk/go"
|
sts "github.com/tencentyun/qcloud-cos-sts-sdk/go"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type paramsTencentCloudStorageCredential struct {
|
|
||||||
Token string `json:"token"`
|
|
||||||
OperationID string `json:"operationID"`
|
|
||||||
}
|
|
||||||
|
|
||||||
var lastTime int64
|
|
||||||
var lastRes *sts.CredentialResult
|
|
||||||
|
|
||||||
func TencentCloudStorageCredential(c *gin.Context) {
|
func TencentCloudStorageCredential(c *gin.Context) {
|
||||||
params := paramsTencentCloudStorageCredential{}
|
req := api.TencentCloudStorageCredentialReq{}
|
||||||
if err := c.BindJSON(¶ms); err != nil {
|
if err := c.BindJSON(&req); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": "Parameter parsing error,please check the parameters and request service again"})
|
log.NewError("0", "BindJSON failed ", err.Error())
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
ok, userID := token_verify.GetUserIDFromToken(c.Request.Header.Get("token"))
|
||||||
log2.Info(params.Token, params.OperationID, "api TencentUpLoadCredential call start...")
|
if !ok {
|
||||||
|
log.NewError(req.OperationID, "GetUserIDFromToken false ", c.Request.Header.Get("token"))
|
||||||
if time.Now().Unix()-lastTime < 10 && lastRes != nil {
|
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "GetUserIDFromToken failed"})
|
||||||
c.JSON(http.StatusOK, gin.H{
|
|
||||||
"errCode": 0,
|
|
||||||
"errMsg": "",
|
|
||||||
"region": config.Config.Credential.Tencent.Region,
|
|
||||||
"bucket": config.Config.Credential.Tencent.Bucket,
|
|
||||||
"data": lastRes,
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
log.NewInfo(req.OperationID, "TencentCloudStorageCredential args ", userID)
|
||||||
lastTime = time.Now().Unix()
|
|
||||||
|
|
||||||
cli := sts.NewClient(
|
cli := sts.NewClient(
|
||||||
config.Config.Credential.Tencent.SecretID,
|
config.Config.Credential.Tencent.SecretID,
|
||||||
config.Config.Credential.Tencent.SecretKey,
|
config.Config.Credential.Tencent.SecretKey,
|
||||||
nil,
|
nil,
|
||||||
)
|
)
|
||||||
log2.Info(c.Request.Header.Get("token"), c.PostForm("optionID"), "api TencentUpLoadCredential sts.NewClient cli = %v", cli)
|
|
||||||
|
|
||||||
opt := &sts.CredentialOptions{
|
opt := &sts.CredentialOptions{
|
||||||
DurationSeconds: int64(time.Hour.Seconds()),
|
DurationSeconds: int64(time.Hour.Seconds()),
|
||||||
@ -65,29 +51,15 @@ func TencentCloudStorageCredential(c *gin.Context) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
log2.Info(c.Request.Header.Get("token"), c.PostForm("optionID"), "api TencentUpLoadCredential sts.CredentialOptions opt = %v", opt)
|
|
||||||
|
|
||||||
res, err := cli.GetCredential(opt)
|
res, err := cli.GetCredential(opt)
|
||||||
|
resp := api.TencentCloudStorageCredentialResp{}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log2.Error(c.Request.Header.Get("token"), c.PostForm("optionID"), "api TencentUpLoadCredential cli.GetCredential err = %s", err.Error())
|
resp.ErrCode = constant.ErrTencentCredential.ErrCode
|
||||||
c.JSON(http.StatusOK, gin.H{
|
resp.ErrMsg = err.Error()
|
||||||
"errCode": constant.ErrTencentCredential.ErrCode,
|
} else {
|
||||||
"errMsg": err.Error(),
|
resp.Bucket = config.Config.Credential.Tencent.Bucket
|
||||||
"bucket": "",
|
resp.Region = config.Config.Credential.Tencent.Region
|
||||||
"region": "",
|
resp.CredentialResult = res
|
||||||
"data": res,
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
log2.Info(c.Request.Header.Get("token"), c.PostForm("optionID"), "api TencentUpLoadCredential cli.GetCredential success res = %v, res.Credentials = %v", res, res.Credentials)
|
c.JSON(http.StatusOK, resp)
|
||||||
|
|
||||||
lastRes = res
|
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
|
||||||
"errCode": 0,
|
|
||||||
"errMsg": "",
|
|
||||||
"region": config.Config.Credential.Tencent.Region,
|
|
||||||
"bucket": config.Config.Credential.Tencent.Bucket,
|
|
||||||
"data": res,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
18
pkg/base_info/cos_api_struct.go
Normal file
18
pkg/base_info/cos_api_struct.go
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package base_info
|
||||||
|
|
||||||
|
import sts "github.com/tencentyun/qcloud-cos-sts-sdk/go"
|
||||||
|
|
||||||
|
type TencentCloudStorageCredentialReq struct {
|
||||||
|
OperationID string `json:"operationID"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type tencentCloudStorageCredentialRespData struct {
|
||||||
|
*sts.CredentialResult
|
||||||
|
Region string `json:"region"`
|
||||||
|
Bucket string `json:"bucket"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type TencentCloudStorageCredentialResp struct {
|
||||||
|
CommResp
|
||||||
|
tencentCloudStorageCredentialRespData `json:"data"`
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user