dm.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. package http
  2. import (
  3. "math"
  4. "net/http"
  5. "strconv"
  6. xtime "time"
  7. "go-common/app/interface/main/dm2/model"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/net/ip"
  12. "go-common/library/net/metadata"
  13. "go-common/library/time"
  14. )
  15. func httpCode(err error) (code int) {
  16. switch err {
  17. case ecode.NotModified:
  18. code = http.StatusNotModified
  19. case ecode.RequestErr:
  20. code = http.StatusBadRequest
  21. case ecode.NothingFound:
  22. code = http.StatusNotFound
  23. case ecode.ServiceUnavailable:
  24. code = http.StatusServiceUnavailable
  25. default:
  26. code = http.StatusInternalServerError
  27. }
  28. return
  29. }
  30. func dmXML(c *bm.Context) {
  31. var (
  32. p = c.Request.Form
  33. comp = p.Get("comp")
  34. contentType = "text/xml"
  35. )
  36. oid, err := strconv.ParseInt(p.Get("oid"), 10, 64)
  37. if err != nil || oid <= 0 {
  38. c.AbortWithStatus(http.StatusBadRequest)
  39. return
  40. }
  41. data, err := dmSvc.DMXML(c, model.SubTypeVideo, oid)
  42. if err != nil {
  43. c.AbortWithStatus(httpCode(err))
  44. log.Error("dmSvc.XML(%d) error(%v)", oid, err)
  45. return
  46. }
  47. c.Writer.Header().Set("Content-Encoding", "deflate")
  48. c.Writer.Header().Set("Last-Modified", xtime.Now().Format(http.TimeFormat))
  49. if comp == "0" {
  50. c.Writer.Header().Set("Content-Encoding", "none")
  51. if data, err = dmSvc.Gzdeflate(data); err != nil {
  52. log.Error("dmSvc.Gzdeflate(%d) error(%v)", oid, err)
  53. c.AbortWithStatus(httpCode(err))
  54. return
  55. }
  56. }
  57. c.Bytes(http.StatusOK, contentType, data)
  58. }
  59. func dmSeg(c *bm.Context) {
  60. var (
  61. plat int32
  62. mid int64
  63. contentType = "application/octet-stream"
  64. p = c.Request.Form
  65. )
  66. iMid, ok := c.Get("mid")
  67. if ok {
  68. mid = iMid.(int64)
  69. }
  70. oid, err := strconv.ParseInt(p.Get("oid"), 10, 64)
  71. if err != nil || oid <= 0 {
  72. c.AbortWithStatus(http.StatusBadRequest)
  73. return
  74. }
  75. ps, err := strconv.ParseInt(p.Get("ps"), 10, 64)
  76. if err != nil {
  77. c.AbortWithStatus(http.StatusBadRequest)
  78. return
  79. }
  80. tp, err := strconv.ParseInt(p.Get("type"), 10, 64)
  81. if err != nil {
  82. c.AbortWithStatus(http.StatusBadRequest)
  83. return
  84. }
  85. aid, err := strconv.ParseInt(p.Get("aid"), 10, 64)
  86. if err != nil {
  87. c.AbortWithStatus(http.StatusBadRequest)
  88. return
  89. }
  90. platform, err := strconv.ParseInt(p.Get("plat"), 10, 64)
  91. if err != nil {
  92. plat = model.PlatUnknow
  93. } else {
  94. plat = int32(platform)
  95. }
  96. data, err := dmSvc.DMSeg(c, int32(tp), plat, mid, aid, oid, ps)
  97. if err != nil {
  98. c.AbortWithStatus(httpCode(err))
  99. return
  100. }
  101. if len(data) == 0 {
  102. c.AbortWithStatus(http.StatusNotFound)
  103. return
  104. }
  105. c.Bytes(http.StatusOK, contentType, data)
  106. }
  107. func dmSegV2(c *bm.Context) {
  108. var (
  109. plat int32
  110. mid int64
  111. p = c.Request.Form
  112. )
  113. iMid, ok := c.Get("mid")
  114. if ok {
  115. mid = iMid.(int64)
  116. }
  117. oid, err := strconv.ParseInt(p.Get("oid"), 10, 64)
  118. if err != nil || oid <= 0 {
  119. c.JSON(nil, ecode.RequestErr)
  120. return
  121. }
  122. pn, err := strconv.ParseInt(p.Get("pn"), 10, 64)
  123. if err != nil || pn <= 0 {
  124. c.JSON(nil, ecode.RequestErr)
  125. return
  126. }
  127. tp, err := strconv.ParseInt(p.Get("type"), 10, 64)
  128. if err != nil {
  129. c.JSON(nil, ecode.RequestErr)
  130. return
  131. }
  132. aid, err := strconv.ParseInt(p.Get("aid"), 10, 64)
  133. if err != nil {
  134. c.JSON(nil, ecode.RequestErr)
  135. return
  136. }
  137. platform, err := strconv.ParseInt(p.Get("plat"), 10, 64)
  138. if err != nil {
  139. plat = model.PlatUnknow
  140. } else {
  141. plat = int32(platform)
  142. }
  143. c.JSON(dmSvc.DMSegV2(c, int32(tp), mid, aid, oid, pn, plat))
  144. }
  145. func dm(c *bm.Context) {
  146. p := c.Request.Form
  147. oid, err := strconv.ParseInt(p.Get("oid"), 10, 64)
  148. if err != nil || oid <= 0 {
  149. c.JSON(nil, ecode.RequestErr)
  150. return
  151. }
  152. tp, err := strconv.ParseInt(p.Get("type"), 10, 64)
  153. if err != nil {
  154. c.JSON(nil, ecode.RequestErr)
  155. return
  156. }
  157. aid, err := strconv.ParseInt(p.Get("aid"), 10, 64)
  158. if err != nil {
  159. c.JSON(nil, ecode.RequestErr)
  160. return
  161. }
  162. c.JSON(dmSvc.DM(c, int32(tp), aid, oid))
  163. }
  164. func ajaxDM(c *bm.Context) {
  165. var (
  166. p = c.Request.Form
  167. msgs = make([]string, 0)
  168. )
  169. app := p.Get("mobi_app")
  170. if app == "android" || app == "iphone" {
  171. c.JSON(msgs, nil)
  172. return
  173. }
  174. aid, err := strconv.ParseInt(p.Get("aid"), 10, 64)
  175. if err != nil {
  176. c.JSON(nil, ecode.RequestErr)
  177. return
  178. }
  179. c.JSON(dmSvc.AjaxDM(c, aid))
  180. }
  181. // validDMStyle 验证弾幕pool and mode.
  182. func validDMStyle(pool, mode int32) (valid bool) {
  183. switch pool {
  184. case model.PoolNormal, model.PoolSubtitle:
  185. if mode == model.ModeRolling || mode == model.ModeBottom || mode == model.ModeTop ||
  186. mode == model.ModeReverse || mode == model.ModeSpecial {
  187. valid = true
  188. }
  189. case model.PoolSpecial:
  190. if mode == model.ModeCode || mode == model.ModeBAS {
  191. valid = true
  192. }
  193. }
  194. return
  195. }
  196. //dm post
  197. func dmPost(c *bm.Context) {
  198. var (
  199. plat = int64(model.PlatUnknow)
  200. p = c.Request.Form
  201. now = xtime.Now().Unix()
  202. )
  203. mid, _ := c.Get("mid")
  204. msg := p.Get("msg")
  205. typ, err := strconv.ParseInt(p.Get("type"), 10, 64)
  206. if err != nil || int32(typ) != model.SubTypeVideo {
  207. c.JSON(nil, ecode.RequestErr)
  208. return
  209. }
  210. aid, err := strconv.ParseInt(p.Get("aid"), 10, 64)
  211. if err != nil || aid <= 0 {
  212. c.JSON(nil, ecode.RequestErr)
  213. return
  214. }
  215. oid, err := strconv.ParseInt(p.Get("oid"), 10, 64)
  216. if err != nil || oid <= 0 {
  217. c.JSON(nil, ecode.RequestErr)
  218. return
  219. }
  220. progress, err := strconv.ParseInt(p.Get("progress"), 10, 32) // NOTE 老接口过来的弹幕时间为秒
  221. if err != nil || progress < 0 || progress > math.MaxInt32 {
  222. c.JSON(nil, ecode.RequestErr)
  223. return
  224. }
  225. color, err := strconv.ParseInt(p.Get("color"), 10, 64)
  226. if err != nil || color < 0 || color > math.MaxInt32 {
  227. c.JSON(nil, ecode.RequestErr)
  228. return
  229. }
  230. fontsize, err := strconv.ParseInt(p.Get("fontsize"), 10, 32)
  231. if err != nil || fontsize <= 0 || fontsize > 127 {
  232. c.JSON(nil, ecode.RequestErr)
  233. return
  234. }
  235. pool, err := strconv.ParseInt(p.Get("pool"), 10, 32)
  236. if err != nil {
  237. c.JSON(nil, ecode.RequestErr)
  238. return
  239. }
  240. mode, err := strconv.ParseInt(p.Get("mode"), 10, 32)
  241. if err != nil {
  242. c.JSON(nil, ecode.RequestErr)
  243. return
  244. }
  245. if !validDMStyle(int32(pool), int32(mode)) {
  246. c.JSON(nil, ecode.RequestErr)
  247. return
  248. }
  249. rnd, err := strconv.ParseInt(p.Get("rnd"), 10, 64)
  250. if err != nil {
  251. c.JSON(nil, ecode.RequestErr)
  252. return
  253. }
  254. platStr := p.Get("plat")
  255. if platStr != "" {
  256. if plat, err = strconv.ParseInt(platStr, 10, 32); err != nil {
  257. c.JSON(nil, ecode.RequestErr)
  258. return
  259. }
  260. }
  261. dm := &model.DM{
  262. Type: int32(typ),
  263. Oid: oid,
  264. Mid: mid.(int64),
  265. Progress: int32(progress),
  266. Pool: int32(pool),
  267. State: model.StateNormal,
  268. Ctime: time.Time(now),
  269. Mtime: time.Time(now),
  270. Content: &model.Content{
  271. FontSize: int32(fontsize),
  272. Color: color,
  273. IP: int64(ip.InetAtoN(metadata.String(c, metadata.RemoteIP))),
  274. Mode: int32(mode),
  275. Plat: int32(plat),
  276. Msg: msg,
  277. Ctime: time.Time(now),
  278. Mtime: time.Time(now),
  279. },
  280. }
  281. if dm.Pool == model.PoolSpecial {
  282. dm.ContentSpe = &model.ContentSpecial{
  283. Msg: msg,
  284. Ctime: time.Time(now),
  285. Mtime: time.Time(now),
  286. }
  287. }
  288. if err = dmSvc.Post(c, dm, aid, rnd); err != nil {
  289. c.JSON(nil, err)
  290. return
  291. }
  292. data := map[string]interface{}{
  293. "dmid": dm.ID,
  294. }
  295. c.JSON(data, nil)
  296. }
  297. // judgeDM dm judge list.
  298. func judgeDM(c *bm.Context) {
  299. p := c.Request.Form
  300. cid, err := strconv.ParseInt(p.Get("cid"), 10, 64)
  301. if err != nil {
  302. c.JSON(nil, ecode.RequestErr)
  303. return
  304. }
  305. dmid, err := strconv.ParseInt(p.Get("dmid"), 10, 64)
  306. if err != nil {
  307. c.JSON(nil, ecode.RequestErr)
  308. return
  309. }
  310. data, err := dmSvc.JudgeDms(c, 1, cid, dmid)
  311. if err != nil {
  312. log.Error("dmSvc.JudgeDms(cid:%d,dmid:%d) error(%v)", cid, dmid, err)
  313. c.JSON(nil, err)
  314. return
  315. }
  316. c.JSON(data, nil)
  317. }
  318. func dmAdvert(c *bm.Context) {
  319. p := c.Request.Form
  320. arg := &model.ADReq{
  321. ClientIP: metadata.String(c, metadata.RemoteIP),
  322. Buvid: c.Request.Header.Get("Buvid"),
  323. MobiApp: p.Get("mobi_app"),
  324. ADExtra: p.Get("ad_extra"),
  325. }
  326. if mid, ok := c.Get("mid"); ok {
  327. arg.Mid = mid.(int64)
  328. }
  329. typ, err := strconv.ParseInt(p.Get("type"), 10, 64)
  330. if err != nil || int32(typ) != model.SubTypeVideo {
  331. c.JSON(nil, ecode.RequestErr)
  332. return
  333. }
  334. aid, err := strconv.ParseInt(p.Get("aid"), 10, 64)
  335. if err != nil || aid <= 0 {
  336. c.JSON(nil, ecode.RequestErr)
  337. return
  338. }
  339. arg.Aid = aid
  340. oid, err := strconv.ParseInt(p.Get("oid"), 10, 64)
  341. if err != nil || oid <= 0 {
  342. c.JSON(nil, ecode.RequestErr)
  343. return
  344. }
  345. arg.Oid = oid
  346. build, err := strconv.ParseInt(p.Get("build"), 10, 64)
  347. if err != nil || build <= 0 {
  348. c.JSON(nil, ecode.RequestErr)
  349. return
  350. }
  351. arg.Build = build
  352. c.JSON(dmSvc.DMAdvert(c, arg))
  353. }