subtitle_video.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/interface/main/dm2/model"
  6. "go-common/library/log"
  7. )
  8. func keySubtitleSingle(oid int64, tp int32) string {
  9. return fmt.Sprintf("subtitle_%d_%d", oid, tp)
  10. }
  11. // GetWebVideoSubtitle .
  12. func (s *Service) GetWebVideoSubtitle(c context.Context, aid, oid int64, tp int32) (res *model.VideoSubtitles, err error) {
  13. var (
  14. subtitles []*model.VideoSubtitle
  15. subtitleSubject *model.SubtitleSubject
  16. allowSubmit bool
  17. closed bool
  18. lan, lanDoc string
  19. )
  20. if subtitleSubject, err = s.subtitleSubject(c, aid); err != nil {
  21. log.Error("params(aid:%v).err(%v)", aid, err)
  22. err = nil
  23. }
  24. if subtitleSubject != nil {
  25. allowSubmit = subtitleSubject.Allow
  26. closed = subtitleSubject.AttrVal(model.AttrSubtitleClose) == model.AttrYes
  27. lan, lanDoc = s.subtitleLans.GetByID(int64(subtitleSubject.Lan))
  28. }
  29. res = &model.VideoSubtitles{
  30. AllowSubmit: allowSubmit,
  31. Lan: lan,
  32. LanDoc: lanDoc,
  33. }
  34. if closed {
  35. res.AllowSubmit = false
  36. return
  37. }
  38. if subtitles, err = s.getVideoSubtitles(c, oid, tp); err != nil {
  39. return
  40. }
  41. res.Subtitles = subtitles
  42. return
  43. }
  44. // singleGetVideoSubtitle use singleflight, but not cache sub item
  45. func (s *Service) singleGetVideoSubtitle(c context.Context, oid int64, tp int32) (res []*model.VideoSubtitle, err error) {
  46. var (
  47. v interface{}
  48. subtitleIds []int64
  49. subtitles map[int64]*model.Subtitle
  50. )
  51. v, err, _ = s.subtitleSingleGroup.Do(keySubtitleSingle(oid, tp), func() (reply interface{}, err error) {
  52. if subtitleIds, err = s.dao.GetSubtitleIds(c, oid, tp); err != nil {
  53. log.Error("params(oid:%v, tp:%v).err(%v)", oid, tp, err)
  54. return
  55. }
  56. if len(subtitleIds) == 0 {
  57. return
  58. }
  59. if subtitles, err = s.getSubtitles(c, oid, subtitleIds); err != nil {
  60. log.Error("params(oid:%v, subtitleIds:%v).err(%v)", oid, subtitleIds, err)
  61. return
  62. }
  63. result := make([]*model.VideoSubtitle, 0, len(subtitles))
  64. for _, subtitle := range subtitles {
  65. lan, lanDoc := s.subtitleLans.GetByID(int64(subtitle.Lan))
  66. vs := &model.VideoSubtitle{
  67. ID: subtitle.ID,
  68. IsLock: subtitle.IsLock,
  69. Lan: lan,
  70. LanDoc: lanDoc,
  71. SubtitleURL: subtitle.SubtitleURL,
  72. }
  73. if subtitle.IsSign {
  74. vs.AuthorMid = subtitle.AuthorID
  75. }
  76. result = append(result, vs)
  77. }
  78. reply = result
  79. return
  80. })
  81. if err != nil {
  82. log.Error("params(oid:%v, tp:%v).err(%v)", oid, tp, err)
  83. return
  84. }
  85. res, _ = v.([]*model.VideoSubtitle)
  86. return
  87. }
  88. // getVideoSubtitles get from cache
  89. func (s *Service) getVideoSubtitles(c context.Context, oid int64, tp int32) (subtitles []*model.VideoSubtitle, err error) {
  90. var (
  91. cacheErr bool
  92. videoSubtitleCache *model.VideoSubtitleCache
  93. )
  94. if videoSubtitleCache, err = s.dao.VideoSubtitleCache(c, oid, tp); err != nil {
  95. cacheErr = true
  96. err = nil
  97. }
  98. if videoSubtitleCache != nil {
  99. subtitles = videoSubtitleCache.VideoSubtitles
  100. return
  101. }
  102. if subtitles, err = s.singleGetVideoSubtitle(c, oid, tp); err != nil {
  103. log.Error("params(oid:%v,tp:%v).err(%v)", oid, tp, err)
  104. return
  105. }
  106. videoSubtitleCache = &model.VideoSubtitleCache{
  107. VideoSubtitles: subtitles,
  108. }
  109. if !cacheErr {
  110. temp := videoSubtitleCache
  111. s.cache.Do(c, func(ctx context.Context) {
  112. s.dao.SetVideoSubtitleCache(ctx, oid, tp, temp)
  113. })
  114. }
  115. return
  116. }