web.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package service
  2. import (
  3. "bytes"
  4. "context"
  5. "net"
  6. "go-common/app/interface/main/videoup/model/archive"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. "go-common/library/net/metadata"
  10. "go-common/library/sync/errgroup"
  11. )
  12. // WebAdd add archive by web.
  13. func (s *Service) WebAdd(c context.Context, mid int64, ap *archive.ArcParam, validated bool) (aid int64, err error) {
  14. ip := metadata.String(c, metadata.RemoteIP)
  15. ap.IPv6 = net.ParseIP(ip)
  16. g := &errgroup.Group{}
  17. ctx := context.TODO()
  18. defer func() {
  19. // VideoupCanotRepeat high level but basic
  20. if err != nil && err != ecode.VideoupCanotRepeat && err != ecode.VideoupAddLimitHalfMin {
  21. g.Go(func() error {
  22. s.acc.DelSubmitCache(ctx, mid, ap.Title)
  23. return nil
  24. })
  25. }
  26. if err != nil && err != ecode.VideoupAddLimitHalfMin {
  27. g.Go(func() error {
  28. s.acc.DelHalfMin(ctx, mid)
  29. return nil
  30. })
  31. }
  32. }()
  33. if !validated && !s.allowHalfMin(c, mid) {
  34. log.Warn("VideoupAddLimitHalfMin mid(%d) ap(%+v) validated(%+v)", mid, ap, validated)
  35. err = ecode.VideoupAddLimitHalfMin
  36. return
  37. }
  38. if err = s.checkIdentify(c, mid, ip); err != nil {
  39. log.Error("s.CheckIdentify mid(%d) ap(%+v) error(%v)", mid, ap, err)
  40. return
  41. }
  42. if err = s.checkAddPay(c, ap, ip); err != nil {
  43. log.Error("s.checkAddPay mid(%d) ap(%+v) error(%v)", mid, ap, err)
  44. return
  45. }
  46. // 检查联合投稿
  47. if err = s.checkAddStaff(c, ap, mid, ip); err != nil {
  48. return
  49. }
  50. // pre check
  51. if err = s.preAdd(c, mid, ap, ip, archive.UpFromWeb); err != nil {
  52. return
  53. }
  54. // add
  55. if aid, err = s.arc.Add(c, ap, ip); err != nil || aid == 0 {
  56. return
  57. }
  58. g.Go(func() error {
  59. s.dealOrder(ctx, mid, aid, ap.OrderID, ip)
  60. return nil
  61. })
  62. g.Go(func() error {
  63. s.freshFavs(ctx, mid, ap, ip)
  64. return nil
  65. })
  66. g.Go(func() error {
  67. s.acc.AddHalfMin(ctx, mid)
  68. return nil
  69. })
  70. g.Go(func() error {
  71. s.dealElec(ctx, ap.OpenElec, aid, mid, ip)
  72. return nil
  73. })
  74. g.Go(func() error {
  75. s.dealSubtitle(ctx, ap.Subtitle, aid, mid, ip)
  76. return nil
  77. })
  78. // same to edit go func, 当且仅当付费设置有且开启
  79. g.Go(func() error {
  80. if ap.Pay != nil && ap.Pay.Open == 1 && !ap.Pay.RefuseUpdate {
  81. if err = s.dealAddPay(ctx, ap.Pay, aid, mid, ip); err != nil {
  82. //异步重试队列
  83. s.asyncCh <- func() error {
  84. return s.dealAddPay(ctx, ap.Pay, aid, mid, ip)
  85. }
  86. }
  87. err = nil
  88. }
  89. return nil
  90. })
  91. g.Wait()
  92. return
  93. }
  94. // WebEdit edit archive by web.
  95. func (s *Service) WebEdit(c context.Context, ap *archive.ArcParam, mid int64) (err error) {
  96. ip := metadata.String(c, metadata.RemoteIP)
  97. ap.IPv6 = net.ParseIP(ip)
  98. if err = s.checkIdentify(c, mid, ip); err != nil {
  99. log.Error("s.CheckIdentify mid(%d) ap(%+v) error(%v)", mid, ap, err)
  100. return
  101. }
  102. var (
  103. a = &archive.Archive{}
  104. vs = []*archive.Video{}
  105. )
  106. if a, vs, err = s.arc.View(c, ap.Aid, ip); err != nil {
  107. log.Error("s.arc.View err(%v) | aid(%d) ip(%s)", err, ap.Aid, ip)
  108. return
  109. }
  110. if a == nil {
  111. log.Error("s.arc.View(%d) not found", mid)
  112. err = ecode.ArchiveNotExist
  113. return
  114. }
  115. if nvsCnt := s.checkVideosMaxLimitForEdit(vs, ap.Videos); nvsCnt > s.c.MaxAddVsCnt {
  116. log.Error("checkVideosMaxLimitForEdit, vsCnt(%d), limit(%d), nvsCnt(%d)", len(vs), s.c.MaxAddVsCnt, nvsCnt)
  117. err = ecode.VideoupVideosMaxLimit
  118. return
  119. }
  120. if err = s.checkEditStaff(c, ap, mid, a, ip); err != nil {
  121. return
  122. }
  123. // pre check
  124. if err = s.preEdit(c, mid, a, vs, ap, ip, archive.UpFromWeb); err != nil {
  125. return
  126. }
  127. // edit
  128. if err = s.arc.Edit(c, ap, ip); err != nil {
  129. return
  130. }
  131. g := &errgroup.Group{}
  132. ctx := context.TODO()
  133. g.Go(func() error {
  134. s.dealElec(ctx, ap.OpenElec, ap.Aid, mid, ip)
  135. return nil
  136. })
  137. g.Go(func() error {
  138. s.dealSubtitle(ctx, ap.Subtitle, ap.Aid, mid, ip)
  139. return nil
  140. })
  141. g.Go(func() error {
  142. // web端60天之后无脑调价 && web端开放过,在60天内,不允许修改价格,但是可以修改稿件其他信息
  143. if ap.Pay != nil && ap.Pay.Open == 1 && !ap.Pay.RefuseUpdate {
  144. if err = s.dealAdjustPay(ctx, ap.Pay, ap.Aid, mid, ip); err != nil {
  145. //异步重试队列
  146. s.asyncCh <- func() error {
  147. return s.dealAdjustPay(ctx, ap.Pay, ap.Aid, mid, ip)
  148. }
  149. }
  150. err = nil
  151. }
  152. return nil
  153. })
  154. g.Wait()
  155. return
  156. }
  157. // WebUpCover client upload cover.
  158. func (s *Service) WebUpCover(c context.Context, fileType string, body []byte, mid int64) (url string, err error) {
  159. if len(body) == 0 {
  160. err = ecode.FileNotExists
  161. return
  162. }
  163. if len(body) > s.c.Bfs.MaxFileSize {
  164. err = ecode.FileTooLarge
  165. return
  166. }
  167. url, err = s.bfs.Upload(c, fileType, bytes.NewReader(body))
  168. if err != nil {
  169. log.Error("s.bfs.Upload error(%v)", err)
  170. }
  171. return
  172. }
  173. // WebCmAdd add archive by web client and from business order.
  174. func (s *Service) WebCmAdd(c context.Context, mid int64, ap *archive.ArcParam) (aid int64, err error) {
  175. ip := metadata.String(c, metadata.RemoteIP)
  176. ap.IPv6 = net.ParseIP(ip)
  177. defer func() {
  178. if err != nil && err != ecode.VideoupCanotRepeat {
  179. s.acc.DelSubmitCache(c, ap.Mid, ap.Title)
  180. }
  181. }()
  182. if ap.TypeID != archive.AdvertisingTypeID {
  183. err = ecode.VideoupTypeidErr
  184. log.Error("ap.TypeID is not AdvertisingTypeID mid(%d),type(%d),err(%v) ", mid, ap.TypeID, err)
  185. return
  186. }
  187. if err = s.checkIdentify(c, mid, ip); err != nil {
  188. log.Error("s.CheckIdentify mid(%d) ap(%+v) error(%v)", mid, ap, err)
  189. return
  190. }
  191. // pre check
  192. if err = s.preAdd(c, mid, ap, ip, archive.UpFromCM); err != nil {
  193. return
  194. }
  195. // add
  196. if aid, err = s.arc.Add(c, ap, ip); err != nil || aid == 0 {
  197. return
  198. }
  199. g := &errgroup.Group{}
  200. ctx := context.TODO()
  201. g.Go(func() error {
  202. s.dealOrder(ctx, mid, aid, ap.OrderID, ip)
  203. return nil
  204. })
  205. g.Wait()
  206. return
  207. }