archive.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package service
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/md5"
  6. "encoding/base64"
  7. "encoding/binary"
  8. "encoding/hex"
  9. "io"
  10. "net/url"
  11. "strconv"
  12. "strings"
  13. "time"
  14. "go-common/app/interface/main/player/dao"
  15. "go-common/app/interface/main/player/model"
  16. accmdl "go-common/app/service/main/account/api"
  17. arcmdl "go-common/app/service/main/archive/api"
  18. "go-common/app/service/main/archive/model/archive"
  19. "go-common/library/ecode"
  20. "go-common/library/log"
  21. "go-common/library/net/metadata"
  22. )
  23. const (
  24. _maxLevel = 6
  25. _hasUGCPay = 1
  26. )
  27. // View get view info
  28. func (s *Service) View(c context.Context, aid int64) (view *model.View, err error) {
  29. var viewReply *arcmdl.ViewReply
  30. if viewReply, err = s.arcClient.View(c, &arcmdl.ViewRequest{Aid: aid}); err != nil {
  31. dao.PromError("View接口错误", "s.arcClientView3(%d) error(%v)", aid, err)
  32. return
  33. }
  34. view = &model.View{Arc: viewReply.Arc, Pages: viewReply.Pages}
  35. return
  36. }
  37. // Matsuri get matsuri info
  38. func (s *Service) Matsuri(c context.Context, now time.Time) (view *model.View) {
  39. if now.Unix() < s.matTime.Unix() {
  40. return s.pastView
  41. }
  42. if s.matOn || len(s.matView.Pages) < 1 {
  43. return s.matView
  44. }
  45. view = new(model.View)
  46. *view = *s.matView
  47. view.Pages = view.Pages[0 : len(view.Pages)-1]
  48. return
  49. }
  50. // PageList many p video pages
  51. func (s *Service) PageList(c context.Context, aid int64) (rs []*arcmdl.Page, err error) {
  52. ip := metadata.String(c, metadata.RemoteIP)
  53. if rs, err = s.arc.Page3(c, &archive.ArgAid2{Aid: aid, RealIP: ip}); err != nil {
  54. dao.PromError("Page3 接口错误", "s.arc.Page3(%d) error(%v)", aid, err)
  55. }
  56. return
  57. }
  58. // VideoShot get archive video shot data
  59. func (s *Service) VideoShot(c context.Context, aid, cid int64, index bool) (res *model.Videoshot, err error) {
  60. var (
  61. viewReply *arcmdl.ViewReply
  62. ip = metadata.String(c, metadata.RemoteIP)
  63. )
  64. if viewReply, err = s.arcClient.View(c, &arcmdl.ViewRequest{Aid: aid}); err != nil {
  65. log.Error("VideoShot s.arcClient.View(%d) error(%v)", aid, err)
  66. return
  67. }
  68. if !viewReply.Arc.IsNormal() || viewReply.Arc.Rights.UGCPay == _hasUGCPay {
  69. log.Warn("VideoShot warn arc(%d) state(%d) or ugcpay(%d)", aid, viewReply.Arc.State, viewReply.Arc.Rights.UGCPay)
  70. err = ecode.NothingFound
  71. return
  72. }
  73. if cid == 0 {
  74. if len(viewReply.Pages) == 0 {
  75. err = ecode.NothingFound
  76. return
  77. }
  78. cid = viewReply.Pages[0].Cid
  79. }
  80. res = &model.Videoshot{}
  81. if res.Videoshot, err = s.arc.Videoshot2(c, &archive.ArgCid2{Aid: aid, Cid: cid, RealIP: ip}); err != nil {
  82. log.Error("s.arc.Videoshot2(%d,%d) err(%v)", aid, cid, err)
  83. return
  84. }
  85. if index && res.PvData != "" {
  86. if pv, e := s.dao.PvData(c, res.PvData); e != nil {
  87. log.Error("s.dao.PvData(aid:%d,cid:%d) err(%+v)", aid, cid, e)
  88. } else if len(pv) > 0 {
  89. var (
  90. v uint16
  91. pvs []uint16
  92. buf = bytes.NewReader(pv)
  93. )
  94. for {
  95. if e := binary.Read(buf, binary.BigEndian, &v); e != nil {
  96. if e != io.EOF {
  97. log.Warn("binary.Read pvdata(%s) err(%v)", res.PvData, e)
  98. }
  99. break
  100. }
  101. pvs = append(pvs, v)
  102. }
  103. res.Index = pvs
  104. }
  105. }
  106. fmtVideshot(res)
  107. return
  108. }
  109. func fmtVideshot(res *model.Videoshot) {
  110. if res.PvData != "" {
  111. res.PvData = strings.Replace(res.PvData, "http://", "//", 1)
  112. }
  113. for i, v := range res.Image {
  114. res.Image[i] = strings.Replace(v, "http://", "//", 1)
  115. }
  116. }
  117. // PlayURLToken get playurl token
  118. func (s *Service) PlayURLToken(c context.Context, mid, aid, cid int64) (res *model.PlayURLToken, err error) {
  119. var (
  120. arcReply *arcmdl.ArcReply
  121. ui *accmdl.CardReply
  122. owner, svip int
  123. vip int32
  124. )
  125. if arcReply, err = s.arcClient.Arc(c, &arcmdl.ArcRequest{Aid: aid}); err != nil {
  126. dao.PromError("Arc接口错误", "s.arcClient.Arc(%d) error(%v)", aid, err)
  127. err = ecode.NothingFound
  128. return
  129. }
  130. if !arcReply.Arc.IsNormal() {
  131. err = ecode.NothingFound
  132. return
  133. }
  134. if mid == arcReply.Arc.Author.Mid {
  135. owner = 1
  136. }
  137. if ui, err = s.accClient.Card3(c, &accmdl.MidReq{Mid: mid}); err != nil {
  138. dao.PromError("Card3接口错误", "s.accClient.Card3(%d) error(%v)", mid, err)
  139. err = ecode.AccessDenied
  140. return
  141. }
  142. if vip = ui.Card.Level; vip > _maxLevel {
  143. vip = _maxLevel
  144. }
  145. if ui.Card.Vip.Type != 0 && ui.Card.Vip.Status == 1 {
  146. svip = 1
  147. }
  148. res = &model.PlayURLToken{
  149. From: "pc",
  150. Ts: time.Now().Unix(),
  151. Aid: aid,
  152. Cid: cid,
  153. Mid: mid,
  154. Owner: owner,
  155. VIP: int(vip),
  156. SVIP: svip,
  157. }
  158. params := url.Values{}
  159. params.Set("from", res.From)
  160. params.Set("ts", strconv.FormatInt(res.Ts, 10))
  161. params.Set("aid", strconv.FormatInt(res.Aid, 10))
  162. params.Set("cid", strconv.FormatInt(res.Cid, 10))
  163. params.Set("mid", strconv.FormatInt(res.Mid, 10))
  164. params.Set("vip", strconv.Itoa(res.VIP))
  165. params.Set("svip", strconv.Itoa(res.SVIP))
  166. params.Set("owner", strconv.Itoa(res.Owner))
  167. tmp := params.Encode()
  168. if strings.IndexByte(tmp, '+') > -1 {
  169. tmp = strings.Replace(tmp, "+", "%20", -1)
  170. }
  171. mh := md5.Sum([]byte(strings.ToLower(tmp) + s.c.PlayURLToken.Secret))
  172. res.Fcs = hex.EncodeToString(mh[:])
  173. res.Token = base64.StdEncoding.EncodeToString([]byte(tmp + "&fcs=" + res.Fcs))
  174. return
  175. }