client.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package http
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "net/http"
  6. "go-common/app/interface/main/videoup/model/archive"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. )
  11. func clientAdd(c *bm.Context) {
  12. var (
  13. aid int64
  14. ap = &archive.ArcParam{}
  15. cp = &archive.ArcParam{}
  16. err error
  17. )
  18. midI, _ := c.Get("mid")
  19. mid, ok := midI.(int64)
  20. if !ok || mid <= 0 {
  21. c.JSON(nil, ecode.RequestErr)
  22. return
  23. }
  24. bs, err := ioutil.ReadAll(c.Request.Body)
  25. if err != nil {
  26. c.JSON(nil, ecode.RequestErr)
  27. return
  28. }
  29. c.Request.Body.Close()
  30. err = json.Unmarshal(bs, ap)
  31. err = json.Unmarshal(bs, cp)
  32. if err != nil {
  33. c.JSON(nil, ecode.VideoupParamErr)
  34. return
  35. }
  36. ap.Aid = 0
  37. ap.Mid = mid
  38. ap.UpFrom = archive.UpFromWindows
  39. defer func() {
  40. cp.Aid = ap.Aid
  41. cp.Mid = ap.Mid
  42. cp.IPv6 = ap.IPv6
  43. cp.UpFrom = ap.UpFrom
  44. build, buvid := getBuildInfo(c)
  45. vdpSvc.SendArchiveLog(aid, build, buvid, "add", "windows", cp, err)
  46. }()
  47. aid, err = vdpSvc.ClientAdd(c, mid, ap)
  48. if err != nil {
  49. c.JSON(nil, err)
  50. log.Error("addErr err(%+v)|ap(%+v)", err, ap)
  51. return
  52. }
  53. c.JSON(map[string]interface{}{
  54. "aid": aid,
  55. }, nil)
  56. }
  57. func clientEdit(c *bm.Context) {
  58. var (
  59. aid int64
  60. ap = &archive.ArcParam{}
  61. cp = &archive.ArcParam{}
  62. err error
  63. )
  64. midI, _ := c.Get("mid")
  65. mid, ok := midI.(int64)
  66. if !ok || mid <= 0 {
  67. c.JSON(nil, ecode.RequestErr)
  68. return
  69. }
  70. bs, err := ioutil.ReadAll(c.Request.Body)
  71. if err != nil {
  72. c.JSON(nil, ecode.RequestErr)
  73. return
  74. }
  75. c.Request.Body.Close()
  76. err = json.Unmarshal(bs, ap)
  77. err = json.Unmarshal(bs, cp)
  78. if err != nil {
  79. c.JSON(nil, ecode.VideoupParamErr)
  80. return
  81. }
  82. aid = ap.Aid
  83. ap.Mid = mid
  84. ap.UpFrom = archive.UpFromWindows
  85. defer func() {
  86. cp.Aid = ap.Aid
  87. cp.Mid = ap.Mid
  88. cp.UpFrom = ap.UpFrom
  89. cp.IPv6 = ap.IPv6
  90. build, buvid := getBuildInfo(c)
  91. vdpSvc.SendArchiveLog(aid, build, buvid, "edit", "windows", cp, err)
  92. }()
  93. err = vdpSvc.ClientEdit(c, ap, mid)
  94. if err != nil {
  95. c.JSON(nil, err)
  96. log.Error("editErr err(%+v)|ap(%+v)", err, ap)
  97. return
  98. }
  99. c.JSON(map[string]interface{}{
  100. "aid": ap.Aid,
  101. }, nil)
  102. }
  103. func clientUpCover(c *bm.Context) {
  104. midI, _ := c.Get("mid")
  105. mid, ok := midI.(int64)
  106. if !ok || mid <= 0 {
  107. c.JSON(nil, ecode.RequestErr)
  108. return
  109. }
  110. file, _, err := c.Request.FormFile("file")
  111. if err != nil {
  112. log.Error("c.Request().FormFile(\"file\") error(%v) | ", err)
  113. c.JSON(nil, ecode.RequestErr)
  114. return
  115. }
  116. bs, err := ioutil.ReadAll(file)
  117. file.Close()
  118. if err != nil {
  119. log.Error("ioutil.ReadAll(c.Request().Body) error(%v)", err)
  120. c.JSON(nil, ecode.RequestErr)
  121. return
  122. }
  123. ftype := http.DetectContentType(bs)
  124. if ftype != "image/jpeg" && ftype != "image/png" && ftype != "image/webp" {
  125. log.Error("filetype not allow file type(%s)", ftype)
  126. c.JSON(nil, ecode.RequestErr)
  127. return
  128. }
  129. url, err := vdpSvc.ClientUpCover(c, ftype, bs, mid)
  130. if err != nil {
  131. c.JSON(nil, err)
  132. return
  133. }
  134. c.JSON(map[string]interface{}{
  135. "url": url,
  136. }, nil)
  137. }