change-src.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package http
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "go-common/app/service/video/stream-mng/common"
  6. "go-common/library/ecode"
  7. bm "go-common/library/net/http/blademaster"
  8. "io/ioutil"
  9. "strconv"
  10. )
  11. // 单独文件,切换cdn
  12. // changeSrc 切换cdn
  13. func changeSrc(c *bm.Context) {
  14. // 这里传递的src是新的src
  15. type changStruct struct {
  16. RoomID int64 `json:"room_id,omitempty"`
  17. ToOrigin int8 `json:"src,omitempty"`
  18. Source string `json:"source,omitempty"`
  19. OperateName string `json:"operate_name,omitempty"`
  20. Reason string `json:"reason,omitempty"`
  21. }
  22. var cs changStruct
  23. switch c.Request.Header.Get("Content-Type") {
  24. case "application/x-www-form-urlencoded":
  25. if len(c.Request.PostForm) == 0 {
  26. c.Set("output_data", "参数为空")
  27. c.JSONMap(map[string]interface{}{"message": "参数为空"}, ecode.RequestErr)
  28. c.Abort()
  29. return
  30. }
  31. toOrigin := c.Request.PostFormValue("src")
  32. or, _ := strconv.ParseInt(toOrigin, 10, 64)
  33. cs.ToOrigin = int8(or)
  34. rid, _ := strconv.ParseInt(c.Request.PostFormValue("room_id"), 10, 64)
  35. cs.RoomID = rid
  36. cs.Source = c.Request.PostFormValue("source")
  37. cs.OperateName = c.Request.PostFormValue("operate_name")
  38. default:
  39. // 验证传参数
  40. defer c.Request.Body.Close()
  41. b, err := ioutil.ReadAll(c.Request.Body)
  42. if err != nil {
  43. c.JSONMap(map[string]interface{}{"message": err}, ecode.RequestErr)
  44. c.Abort()
  45. return
  46. }
  47. if len(b) == 0 {
  48. c.JSONMap(map[string]interface{}{"message": "参数为空"}, ecode.RequestErr)
  49. c.Abort()
  50. return
  51. }
  52. err = json.Unmarshal(b, &cs)
  53. if err != nil {
  54. c.Set("output_data", err.Error())
  55. c.JSONMap(map[string]interface{}{"message": err.Error()}, ecode.RequestErr)
  56. c.Abort()
  57. return
  58. }
  59. }
  60. c.Set("input_params", cs)
  61. // 校验:房间号+src+平台来源+操作人 都是必须的, 操作理由可以不填
  62. if cs.RoomID <= 0 || cs.ToOrigin == 0 || cs.Source == "" || cs.OperateName == "" {
  63. c.Set("output_data", "some fields are not right")
  64. c.JSONMap(map[string]interface{}{"message": "部分参数为空"}, ecode.RequestErr)
  65. c.Abort()
  66. return
  67. }
  68. // todo 先使用老的src, 后续改为新的src
  69. src := int8(cs.ToOrigin)
  70. if _, ok := common.SrcMapBitwise[src]; !ok {
  71. c.Set("output_data", "src is not right")
  72. c.JSONMap(map[string]interface{}{"message": "src is not right"}, ecode.RequestErr)
  73. c.Abort()
  74. return
  75. }
  76. err := srv.ChangeSrc(c, cs.RoomID, common.SrcMapBitwise[src], cs.Source, cs.OperateName, cs.Reason)
  77. if err == nil {
  78. c.Set("output_data", fmt.Sprintf("room_id = %d, change src success", cs.RoomID))
  79. c.JSONMap(map[string]interface{}{"message": "ok"}, nil)
  80. c.Abort()
  81. return
  82. }
  83. c.Set("output_data", fmt.Sprintf("room_id = %d, change src faild = %v", cs.RoomID, err))
  84. c.JSONMap(map[string]interface{}{"message": err.Error()}, ecode.RequestErr)
  85. }