subtitle_save.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package service
  2. import (
  3. "context"
  4. "crypto/sha1"
  5. "encoding/hex"
  6. "time"
  7. "go-common/app/interface/main/dm2/model"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. )
  11. func (s *Service) genSubtitleID(c context.Context) (subtitleID int64, err error) {
  12. subtitleID, err = s.seqRPC.ID(c, s.seqSubtitleArg)
  13. if err != nil {
  14. return
  15. }
  16. return
  17. }
  18. // SaveSubtitleDraft save subtitle
  19. func (s *Service) SaveSubtitleDraft(c context.Context, aid, oid int64, tp int32, mid int64, lan string, submit, sign bool, originSubtitleID int64, data []byte) (detectErrs []*model.SubtitleDetectError, err error) {
  20. var (
  21. subject *model.Subject
  22. draft *model.Subtitle
  23. authorID int64
  24. )
  25. if subject, err = s.subject(c, tp, oid); err != nil {
  26. log.Error("params(tp:%v, oid:%v).error(%v)", tp, oid, err)
  27. return
  28. }
  29. if err = s.checkSubtitleLan(c, lan); err != nil {
  30. return
  31. }
  32. if err = s.checkAidOid(c, aid, oid); err != nil {
  33. return
  34. }
  35. if err = s.checkSubtitlePermission(c, aid, oid, tp, mid, subject); err != nil {
  36. return
  37. }
  38. // TODO remove error
  39. if detectErrs, err = s.checkSubtitleData(c, aid, oid, data); err != nil {
  40. return
  41. }
  42. if len(detectErrs) > 0 {
  43. return
  44. }
  45. if err = s.checkSubtitleLocked(c, submit, oid, tp, lan, mid); err != nil {
  46. return
  47. }
  48. if authorID, err = s.checkSubtitleAuthor(c, oid, originSubtitleID, lan, mid); err != nil {
  49. return
  50. }
  51. status := model.SubtitleStatusDraft
  52. if submit {
  53. status = model.SubtitleStatusCheckToAudit
  54. if mid == subject.Mid {
  55. status = model.SubtitleStatusCheckPublish
  56. }
  57. }
  58. if draft, err = s.buildSubtitleDraft(c, oid, tp, mid, authorID, lan, data, status, sign); err != nil {
  59. return
  60. }
  61. if err = s.addSubtitleDraft(c, draft); err != nil {
  62. return
  63. }
  64. if status == model.SubtitleStatusCheckToAudit || status == model.SubtitleStatusCheckPublish {
  65. s.dao.SendSubtitleCheck(c, draft.CheckSum, &model.SubtitleCheckMsg{
  66. Oid: oid,
  67. SubtitleID: draft.ID,
  68. })
  69. }
  70. return
  71. }
  72. func (s *Service) addSubtitleDraft(c context.Context, draft *model.Subtitle) (err error) {
  73. if draft.ID > 0 {
  74. if err = s.updateSubtitle(c, draft); err != nil {
  75. return
  76. }
  77. } else {
  78. if draft.ID, err = s.genSubtitleID(c); err != nil {
  79. return
  80. }
  81. if _, err = s.addSubtitle(c, draft); err != nil {
  82. return
  83. }
  84. }
  85. return
  86. }
  87. // buildSubtitleDraft when save draft or save to submit
  88. func (s *Service) buildSubtitleDraft(c context.Context, oid int64, tp int32, mid, authorID int64, lan string, data []byte, status model.SubtitleStatus, sign bool) (draft *model.Subtitle, err error) {
  89. var (
  90. subtitleURL string
  91. checkSum string
  92. subject *model.Subject
  93. lanCode int64
  94. )
  95. if lanCode = s.subtitleLans.GetByLan(lan); lanCode <= 0 {
  96. err = ecode.SubtitleIllegalLanguage
  97. return
  98. }
  99. if draft, err = s.getSubtitlDraft(c, oid, tp, mid, uint8(lanCode)); err != nil {
  100. log.Error("params(oid:%v,tp:%v,mid:%v,lanCode:%v).error(%v)", oid, tp, mid, lanCode, err)
  101. return
  102. }
  103. if draft == nil {
  104. if subject, err = s.subject(c, tp, oid); err != nil {
  105. log.Error("params(oid:%v,tp:%v).error(%v)", oid, tp, err)
  106. return
  107. }
  108. draft = &model.Subtitle{
  109. Oid: oid,
  110. Type: tp,
  111. Mid: mid,
  112. Aid: subject.Pid,
  113. Lan: uint8(lanCode),
  114. AuthorID: mid,
  115. UpMid: subject.Mid,
  116. PubTime: 0,
  117. IsSign: sign,
  118. Status: model.SubtitleStatusDraft,
  119. }
  120. }
  121. if draft.Status != model.SubtitleStatusDraft && draft.Status != model.SubtitleStatusToAudit && draft.Status != model.SubtitleStatusCheckToAudit {
  122. err = ecode.SubtitlePermissionDenied
  123. return
  124. }
  125. sha := sha1.Sum(data)
  126. if checkSum = hex.EncodeToString(sha[:]); checkSum != draft.CheckSum {
  127. if subtitleURL, err = s.dao.UploadBfs(c, "", data); err != nil {
  128. log.Error("UploadBfs.error(%v)", err)
  129. return
  130. }
  131. draft.SubtitleURL = subtitleURL
  132. draft.CheckSum = checkSum
  133. }
  134. draft.Status = status
  135. draft.IsSign = sign
  136. if status == model.SubtitleStatusCheckPublish {
  137. draft.PubTime = time.Now().Unix()
  138. }
  139. draft.AuthorID = authorID
  140. draft.RejectComment = ""
  141. return
  142. }