creator.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package service
  2. import (
  3. "bytes"
  4. "context"
  5. "go-common/library/net/metadata"
  6. "hash/crc32"
  7. "net"
  8. "strconv"
  9. "strings"
  10. "go-common/app/interface/main/videoup/model/archive"
  11. "go-common/library/ecode"
  12. "go-common/library/log"
  13. "go-common/library/sync/errgroup"
  14. )
  15. // CreatorEdit edit archive by creator.
  16. func (s *Service) CreatorEdit(c context.Context, mid int64, cp *archive.CreatorParam) (err error) {
  17. ip := metadata.String(c, metadata.RemoteIP)
  18. if err = s.checkIdentify(c, mid, ip); err != nil {
  19. log.Error("s.CheckIdentify mid(%d) ap(%+v) error(%v)", mid, cp, err)
  20. return
  21. }
  22. var (
  23. a = &archive.Archive{}
  24. vs = make([]*archive.Video, 0)
  25. )
  26. if a, vs, err = s.arc.View(c, cp.Aid, ip); err != nil {
  27. log.Error("s.arc.View err(%v) | aid(%d) ip(%s)", err, cp.Aid, ip)
  28. return
  29. }
  30. if a == nil || vs == nil {
  31. log.Error("s.arc.View(%d) not found", mid)
  32. err = ecode.ArchiveNotExist
  33. return
  34. }
  35. ap := &archive.ArcParam{
  36. Aid: cp.Aid,
  37. Tag: cp.Tag,
  38. Title: cp.Title,
  39. Desc: cp.Desc,
  40. OpenElec: cp.OpenElec,
  41. // ------ diff values ----- //
  42. Mid: a.Mid,
  43. Author: a.Author,
  44. TypeID: a.TypeID,
  45. Cover: coverURL(a.Cover),
  46. Copyright: a.Copyright,
  47. NoReprint: a.NoReprint,
  48. OrderID: a.OrderID,
  49. Source: a.Source,
  50. Attribute: a.Attribute,
  51. UpFrom: archive.UpFromCreator,
  52. DTime: a.DTime,
  53. DescFormatID: a.DescFormatID,
  54. Dynamic: a.Dynamic,
  55. IPv6: net.ParseIP(ip),
  56. MissionID: int(a.MissionID),
  57. }
  58. for _, vp := range vs {
  59. ap.Videos = append(ap.Videos, &archive.VideoParam{
  60. Title: vp.Title,
  61. Desc: vp.Desc,
  62. Filename: vp.Filename,
  63. })
  64. }
  65. if only := onlyChangeTagArc(cp, a); only {
  66. ap.Tag = s.removeDupTag(ap.Tag)
  67. if !s.allowTag(ap.Tag) {
  68. log.Error("s.allowTag mid(%d) ap.Tag(%s) tag name or number too large or Empty", mid, ap.Tag)
  69. err = ecode.VideoupTagErr
  70. return
  71. }
  72. if err = s.tagsCheck(c, mid, ap.Tag, ip); err != nil {
  73. log.Error("s.tagsCheck mid(%d) ap(%+v) error(%v)", mid, ap.Tag, err)
  74. return
  75. }
  76. if a.Tag != ap.Tag {
  77. s.arc.TagUp(c, ap.Aid, ap.Tag, ip)
  78. }
  79. s.dealTag(c, mid, ap.Aid, a.Tag, ap.Tag, ip, ap.TypeID)
  80. } else {
  81. if err = s.preEdit(c, mid, a, vs, ap, ip, archive.UpFromCreator); err != nil {
  82. log.Error("s.preCreatorEdit mid(%d) ap(%+v) error(%v)", mid, ap, err)
  83. return
  84. }
  85. if err = s.arc.Edit(c, ap, ip); err != nil {
  86. return
  87. }
  88. }
  89. s.dealElec(c, ap.OpenElec, ap.Aid, mid, ip)
  90. return
  91. }
  92. // 判断是否只在这种特殊的情况下,开放浏览/待审的稿件只修改了Tag信息
  93. func onlyChangeTagArc(cp *archive.CreatorParam, a *archive.Archive) (only bool) {
  94. st := a.State == archive.StateForbidSubmit ||
  95. a.State == archive.StateForbidUserDelay ||
  96. a.State == archive.StateOpen ||
  97. a.State == archive.StateOrange ||
  98. a.State == archive.StateForbidWait
  99. ch := a.Title == cp.Title &&
  100. a.Desc == cp.Desc &&
  101. a.Tag != cp.Tag
  102. if st && ch {
  103. only = true
  104. }
  105. return
  106. }
  107. // CreatorAdd add archive by creator.
  108. func (s *Service) CreatorAdd(c context.Context, mid int64, ap *archive.ArcParam) (aid int64, err error) {
  109. ip := metadata.String(c, metadata.RemoteIP)
  110. ap.IPv6 = net.ParseIP(ip)
  111. defer func() {
  112. if err != nil && err != ecode.VideoupCanotRepeat {
  113. s.acc.DelSubmitCache(c, ap.Mid, ap.Title)
  114. }
  115. }()
  116. if err = s.checkIdentify(c, mid, ip); err != nil {
  117. log.Error("s.CheckIdentify mid(%d) ap(%+v) error(%v)", mid, ap, err)
  118. return
  119. }
  120. // pre check
  121. if err = s.preAdd(c, mid, ap, ip, archive.UpFromCreator); err != nil {
  122. return
  123. }
  124. // add
  125. if aid, err = s.arc.Add(c, ap, ip); err != nil || aid == 0 {
  126. return
  127. }
  128. g := &errgroup.Group{}
  129. ctx := context.TODO()
  130. g.Go(func() error {
  131. s.dealOrder(ctx, mid, aid, ap.OrderID, ip)
  132. return nil
  133. })
  134. g.Go(func() error {
  135. s.dealElec(ctx, ap.OpenElec, ap.Aid, mid, ip)
  136. return nil
  137. })
  138. g.Wait()
  139. return
  140. }
  141. // CreatorUpCover creator upload cover.
  142. func (s *Service) CreatorUpCover(c context.Context, fileType string, body []byte, mid int64) (url string, err error) {
  143. if len(body) == 0 {
  144. err = ecode.FileNotExists
  145. return
  146. }
  147. if len(body) > s.c.Bfs.MaxFileSize {
  148. err = ecode.FileTooLarge
  149. return
  150. }
  151. url, err = s.bfs.Upload(c, fileType, bytes.NewReader(body))
  152. if err != nil {
  153. log.Error("s.bfs.Upload error(%v)", err)
  154. }
  155. return
  156. }
  157. // coverURL convert cover url to full url.
  158. func coverURL(uri string) (cover string) {
  159. if uri == "" {
  160. //cover = "http://static.hdslb.com/images/transparent.gif"
  161. return
  162. }
  163. cover = uri
  164. if strings.Index(uri, "http://") == 0 {
  165. return
  166. }
  167. if len(uri) >= 10 && uri[:10] == "/templets/" {
  168. return
  169. }
  170. if strings.HasPrefix(uri, "group1") {
  171. cover = "http://i0.hdslb.com/" + uri
  172. return
  173. }
  174. if pos := strings.Index(uri, "/uploads/"); pos != -1 && (pos == 0 || pos == 3) {
  175. cover = uri[pos+8:]
  176. }
  177. cover = strings.Replace(cover, "{IMG}", "", -1)
  178. cover = "http://i" + strconv.FormatInt(int64(crc32.ChecksumIEEE([]byte(cover)))%3, 10) + ".hdslb.com" + cover
  179. return
  180. }