Files

83 lines
2.2 KiB
Go
Raw Permalink Normal View History

2023-03-03 17:20:36 +08:00
package api
import (
2023-05-08 12:39:45 +08:00
"math/rand"
"net/http"
"strconv"
2023-03-17 11:27:34 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/a2r"
2023-03-28 17:11:04 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/mcontext"
2023-03-16 10:46:06 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry"
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/third"
2023-06-20 22:12:01 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient"
2023-03-03 17:20:36 +08:00
"github.com/gin-gonic/gin"
)
2023-06-20 22:12:01 +08:00
type ThirdApi rpcclient.Third
2023-03-03 17:20:36 +08:00
2023-06-20 22:12:01 +08:00
func NewThirdApi(discov discoveryregistry.SvcDiscoveryRegistry) ThirdApi {
return ThirdApi(*rpcclient.NewThird(discov))
2023-03-03 17:20:36 +08:00
}
2023-06-20 22:12:01 +08:00
func (o *ThirdApi) ApplyPut(c *gin.Context) {
2023-06-20 21:15:23 +08:00
a2r.Call(third.ThirdClient.ApplyPut, o.Client, c)
2023-03-03 17:20:36 +08:00
}
2023-06-20 22:12:01 +08:00
func (o *ThirdApi) GetPut(c *gin.Context) {
2023-06-20 21:15:23 +08:00
a2r.Call(third.ThirdClient.GetPut, o.Client, c)
2023-03-03 17:20:36 +08:00
}
2023-06-20 22:12:01 +08:00
func (o *ThirdApi) ConfirmPut(c *gin.Context) {
2023-06-20 21:15:23 +08:00
a2r.Call(third.ThirdClient.ConfirmPut, o.Client, c)
2023-03-03 17:20:36 +08:00
}
2023-06-20 22:12:01 +08:00
func (o *ThirdApi) GetHash(c *gin.Context) {
2023-06-20 21:15:23 +08:00
a2r.Call(third.ThirdClient.GetHashInfo, o.Client, c)
2023-03-29 18:33:02 +08:00
}
2023-06-20 22:12:01 +08:00
func (o *ThirdApi) FcmUpdateToken(c *gin.Context) {
2023-06-20 21:15:23 +08:00
a2r.Call(third.ThirdClient.FcmUpdateToken, o.Client, c)
2023-03-03 17:20:36 +08:00
}
2023-06-20 22:12:01 +08:00
func (o *ThirdApi) SetAppBadge(c *gin.Context) {
2023-06-20 21:15:23 +08:00
a2r.Call(third.ThirdClient.SetAppBadge, o.Client, c)
2023-03-03 17:20:36 +08:00
}
2023-03-14 19:33:44 +08:00
2023-06-20 22:12:01 +08:00
func (o *ThirdApi) GetURL(c *gin.Context) {
2023-03-14 19:33:44 +08:00
if c.Request.Method == http.MethodPost {
2023-06-20 21:15:23 +08:00
a2r.Call(third.ThirdClient.GetUrl, o.Client, c)
2023-03-14 19:33:44 +08:00
return
}
name := c.Query("name")
if name == "" {
c.String(http.StatusBadRequest, "name is empty")
return
}
2023-03-29 18:48:07 +08:00
operationID := c.Query("operationID")
if operationID == "" {
operationID = "auto_" + strconv.Itoa(rand.Int())
}
expires, _ := strconv.ParseInt(c.Query("expires"), 10, 64)
if expires <= 0 {
expires = 3600 * 1000
2023-03-28 17:03:52 +08:00
}
2023-03-29 11:17:16 +08:00
attachment, _ := strconv.ParseBool(c.Query("attachment"))
2023-03-28 17:11:04 +08:00
c.Set(constant.OperationID, operationID)
2023-06-20 22:12:01 +08:00
resp, err := o.Client.GetUrl(mcontext.SetOperationID(c, operationID), &third.GetUrlReq{Name: name, Expires: expires, Attachment: attachment})
2023-03-14 19:33:44 +08:00
if err != nil {
if errs.ErrArgs.Is(err) {
c.String(http.StatusBadRequest, err.Error())
return
}
if errs.ErrRecordNotFound.Is(err) {
c.String(http.StatusNotFound, err.Error())
return
}
c.String(http.StatusInternalServerError, err.Error())
return
}
c.Redirect(http.StatusTemporaryRedirect, resp.Url)
}