dm_seg.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. package service
  2. import (
  3. "bytes"
  4. "compress/flate"
  5. "compress/gzip"
  6. "context"
  7. "encoding/json"
  8. "fmt"
  9. "io/ioutil"
  10. "strconv"
  11. "go-common/app/interface/main/dm2/model"
  12. "go-common/library/ecode"
  13. "go-common/library/log"
  14. )
  15. func (s *Service) gzip(input []byte) ([]byte, error) {
  16. buf := new(bytes.Buffer)
  17. zw := gzip.NewWriter(buf)
  18. if _, err := zw.Write(input); err != nil {
  19. return nil, err
  20. }
  21. if err := zw.Close(); err != nil {
  22. return nil, err
  23. }
  24. return buf.Bytes(), nil
  25. }
  26. // gzflate flate 压缩
  27. func (s *Service) gzflate(input []byte) ([]byte, error) {
  28. buf := new(bytes.Buffer)
  29. w, err := flate.NewWriter(buf, 4)
  30. if err != nil {
  31. return nil, err
  32. }
  33. if _, err = w.Write(input); err != nil {
  34. return nil, err
  35. }
  36. if err = w.Close(); err != nil {
  37. return nil, err
  38. }
  39. return buf.Bytes(), err
  40. }
  41. // Gzdeflate deflate 解码
  42. func (s *Service) Gzdeflate(in []byte) (out []byte, err error) {
  43. if len(in) == 0 {
  44. return
  45. }
  46. out, err = ioutil.ReadAll(flate.NewReader(bytes.NewReader(in)))
  47. return
  48. }
  49. func (s *Service) dmsSeg(c context.Context, tp int32, oid int64, missed []int64) (dms []*model.DM, err error) {
  50. idxMap, special, err := s.dao.IndexsByid(c, tp, oid, missed)
  51. if err != nil || len(idxMap) == 0 {
  52. return
  53. }
  54. ctsMap, err := s.dao.Contents(c, oid, missed)
  55. if err != nil {
  56. return
  57. }
  58. ctsSpeMap := make(map[int64]*model.ContentSpecial)
  59. if len(special) > 0 {
  60. if ctsSpeMap, err = s.dao.ContentsSpecial(c, special); err != nil {
  61. return
  62. }
  63. }
  64. for _, content := range ctsMap {
  65. if idx, ok := idxMap[content.ID]; ok {
  66. dm := &model.DM{
  67. ID: idx.ID,
  68. Type: idx.Type,
  69. Oid: idx.Oid,
  70. Mid: idx.Mid,
  71. Progress: idx.Progress,
  72. Pool: idx.Pool,
  73. Attr: idx.Attr,
  74. State: idx.State,
  75. Ctime: idx.Ctime,
  76. Mtime: idx.Mtime,
  77. Content: content,
  78. }
  79. if idx.Pool == model.PoolSpecial {
  80. if _, ok = ctsSpeMap[dm.ID]; ok {
  81. dm.ContentSpe = ctsSpeMap[dm.ID]
  82. }
  83. }
  84. dms = append(dms, dm)
  85. }
  86. }
  87. return
  88. }
  89. func (s *Service) dmSegXML(c context.Context, aid int64, sub *model.Subject, seg *model.Segment) (res []byte, err error) {
  90. var (
  91. cache = true
  92. buf = new(bytes.Buffer)
  93. realname = s.isRealname(c, sub.Pid, sub.Oid)
  94. dms []*model.DM
  95. dmids, normalIds, spIds []int64
  96. )
  97. if realname {
  98. buf.WriteString(seg.ToXMLHeader(sub.Oid, sub.State, 1))
  99. } else {
  100. buf.WriteString(seg.ToXMLHeader(sub.Oid, sub.State, 0))
  101. }
  102. defer func() {
  103. if err == nil {
  104. buf.WriteString(`</i>`)
  105. res, err = s.gzip(buf.Bytes())
  106. }
  107. }()
  108. if normalIds, err = s.dmNormalIds(c, sub.Type, sub.Oid, seg.Cnt, seg.Num, seg.Start, seg.End, 2*sub.Maxlimit); err != nil {
  109. return
  110. }
  111. dmids = append(dmids, normalIds...)
  112. if sub.Childpool > 0 {
  113. if spIds, err = s.dmSegSubtitlesIds(c, sub.Type, sub.Oid, seg.Start, seg.End, 2*sub.Maxlimit); err != nil {
  114. return
  115. }
  116. dmids = append(dmids, spIds...)
  117. }
  118. if len(dmids) <= 0 {
  119. return
  120. }
  121. content, missed, err := s.dao.IdxContentCache(c, sub.Type, sub.Oid, dmids)
  122. if err != nil {
  123. missed = dmids
  124. cache = false
  125. } else {
  126. buf.Write(content)
  127. }
  128. if len(missed) > 0 {
  129. if dms, err = s.dmsSeg(c, sub.Type, sub.Oid, missed); err != nil {
  130. return
  131. }
  132. for _, dm := range dms {
  133. buf.WriteString(dm.ToXMLSeg(realname))
  134. }
  135. }
  136. if cache && len(dms) > 0 {
  137. s.cache.Do(c, func(ctx context.Context) {
  138. s.dao.AddIdxContentCache(ctx, sub.Type, sub.Oid, dms, realname)
  139. })
  140. }
  141. s.cache.Do(c, func(ctx context.Context) {
  142. var (
  143. bs []byte
  144. err error
  145. )
  146. dmSeg := &model.ActionFlushDMSeg{
  147. Type: sub.Type,
  148. Oid: sub.Oid,
  149. Force: false,
  150. Page: &model.Page{
  151. Num: seg.Num,
  152. Total: seg.Cnt,
  153. Size: model.DefaultPageSize,
  154. },
  155. }
  156. if bs, err = json.Marshal(dmSeg); err != nil {
  157. return
  158. }
  159. s.dao.SendAction(ctx, fmt.Sprint(sub.Oid), &model.Action{
  160. Action: model.ActFlushDMSeg,
  161. Data: bs,
  162. })
  163. })
  164. return
  165. }
  166. // DMSeg return dm content.
  167. func (s *Service) DMSeg(c context.Context, tp, plat int32, mid, aid, oid, ps int64) (res []byte, err error) {
  168. var (
  169. sub = &model.Subject{}
  170. flag = model.DefaultFlag
  171. )
  172. seg, err := s.segmentInfo(c, tp, aid, oid, ps)
  173. if err != nil {
  174. return
  175. }
  176. if sub, err = s.subject(c, tp, oid); err != nil {
  177. return
  178. }
  179. if sub.State == model.SubStateClosed {
  180. xml := []byte(seg.ToXMLHeader(sub.Oid, sub.State, 0) + `</i>`)
  181. if xml, err = s.gzip(xml); err != nil {
  182. return
  183. }
  184. res = model.Encode(flag, xml)
  185. return
  186. }
  187. // get from local cache first
  188. xml, ok := s.localCache[keySeg(tp, oid, seg.Cnt, seg.Num)]
  189. if ok {
  190. res = model.Encode(flag, xml)
  191. return
  192. }
  193. // NOTE 将视频弹幕上限调整为 2*maxlimit条
  194. data, err := s.dao.RecFlag(c, mid, aid, oid, 2*sub.Maxlimit, seg.Start, seg.End, plat)
  195. if err == nil {
  196. flag = data
  197. }
  198. // get from remote cache or database
  199. if xml, err = s.singleGenSegXML(c, aid, sub, seg); err != nil {
  200. return
  201. }
  202. res = model.Encode(flag, xml)
  203. return
  204. }
  205. func (s *Service) singleGenSegXML(c context.Context, aid int64, sub *model.Subject, seg *model.Segment) (xml []byte, err error) {
  206. key := keySeg(sub.Type, sub.Oid, seg.Cnt, seg.Num)
  207. v, err, _ := s.singleGroup.Do(key, func() (res interface{}, err error) {
  208. data, err := s.dao.XMLSegCache(c, sub.Type, sub.Oid, seg.Cnt, seg.Num)
  209. if err != nil {
  210. return
  211. }
  212. if len(data) > 0 {
  213. res = data
  214. return
  215. }
  216. if data, err = s.dmSegXML(c, aid, sub, seg); err != nil {
  217. return
  218. }
  219. s.cache.Do(c, func(ctx context.Context) {
  220. s.dao.SetXMLSegCache(ctx, sub.Type, sub.Oid, seg.Cnt, seg.Num, data)
  221. })
  222. return data, err
  223. })
  224. if err != nil {
  225. return
  226. }
  227. xml = v.([]byte)
  228. return
  229. }
  230. // segmentInfo get segment info of oid.
  231. func (s *Service) segmentInfo(c context.Context, tp int32, aid, oid, ps int64) (seg *model.Segment, err error) {
  232. var duration int64
  233. data, ok := s.localCache[keyDuration(tp, oid)]
  234. if ok {
  235. duration, err = strconv.ParseInt(string(data), 10, 64)
  236. } else {
  237. duration, err = s.videoDuration(c, aid, oid)
  238. }
  239. if err != nil {
  240. return
  241. }
  242. if duration != 0 && ps >= duration {
  243. log.Warn("oid:%d ps:%d larger than duration:%d", oid, ps, duration)
  244. err = ecode.NotModified
  245. return
  246. }
  247. seg = model.SegmentInfo(ps, duration)
  248. return
  249. }
  250. func (s *Service) dmNormalIds(c context.Context, tp int32, oid int64, cnt, n, ps, pe, limit int64) (dmids []int64, err error) {
  251. dmids, err = s.dao.DMIDCache(c, tp, oid, cnt, n, limit)
  252. if err != nil || len(dmids) == 0 {
  253. if dmids, err = s.dao.DMIDs(c, tp, oid, ps, pe, limit, model.PoolNormal); err != nil {
  254. return
  255. }
  256. }
  257. return
  258. }
  259. //dmSegSubtitlesIds dm Subtitles
  260. func (s *Service) dmSegSubtitlesIds(c context.Context, tp int32, oid int64, ps, pe, limit int64) (dmids []int64, err error) {
  261. dmids, err = s.dao.DMIDSubtitlesCache(c, tp, oid, ps, pe, limit)
  262. if err != nil || len(dmids) == 0 {
  263. if dmids, err = s.dao.DMIDs(c, tp, oid, ps, pe, limit, model.PoolSubtitle); err != nil || len(dmids) == 0 {
  264. return
  265. }
  266. }
  267. return
  268. }