stream-validate.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package http
  2. import (
  3. "encoding/json"
  4. "go-common/app/service/video/stream-mng/service"
  5. "go-common/library/ecode"
  6. bm "go-common/library/net/http/blademaster"
  7. "io/ioutil"
  8. )
  9. // streamValidate 流鉴权接口
  10. func streamValidate(c *bm.Context) {
  11. var vp service.ValidateParams
  12. switch c.Request.Header.Get("Content-Type") {
  13. case "application/x-www-form-urlencoded":
  14. if len(c.Request.PostForm) == 0 {
  15. c.Set("output_data", "stream_valid_err = empty post body")
  16. c.JSONMap(map[string]interface{}{"message": "empty post body"}, ecode.RequestErr)
  17. c.Abort()
  18. return
  19. }
  20. vp.Key = c.Request.PostFormValue("key")
  21. vp.StreamName = c.Request.PostFormValue("stream_name")
  22. vp.Src = c.Request.PostFormValue("src")
  23. vp.Type = json.Number(c.Request.PostFormValue("type"))
  24. default:
  25. defer c.Request.Body.Close()
  26. b, err := ioutil.ReadAll(c.Request.Body)
  27. if err != nil {
  28. c.Set("output_data", err.Error())
  29. c.JSONMap(map[string]interface{}{"message": err.Error()}, ecode.RequestErr)
  30. c.Abort()
  31. return
  32. }
  33. if len(b) == 0 {
  34. c.Set("output_data", "stream_valid_err = empty params")
  35. c.JSONMap(map[string]interface{}{"message": "empty params"}, ecode.RequestErr)
  36. c.Abort()
  37. return
  38. }
  39. err = json.Unmarshal(b, &vp)
  40. if err != nil {
  41. c.Set("output_data", err.Error())
  42. c.JSONMap(map[string]interface{}{"message": err.Error()}, ecode.RequestErr)
  43. c.Abort()
  44. return
  45. }
  46. }
  47. c.Set("input_params", vp)
  48. permission, err := srv.CheckStreamValidate(c, &vp, false)
  49. if err != nil {
  50. c.Set("output_data", err.Error())
  51. if err.Error() == "room is closed" {
  52. c.JSONMap(map[string]interface{}{"message": err.Error()}, ecode.LimitExceed)
  53. } else {
  54. c.JSONMap(map[string]interface{}{"message": err.Error()}, ecode.RequestErr)
  55. }
  56. c.Abort()
  57. return
  58. }
  59. c.Set("output_data", permission)
  60. c.JSONMap(map[string]interface{}{"data": map[string]int{"permission": permission}}, nil)
  61. }