Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

182 lignes
4.9 KiB

  1. package service
  2. import (
  3. "errors"
  4. "strings"
  5. "github.com/QuantumNous/new-api/common"
  6. "github.com/QuantumNous/new-api/constant"
  7. "github.com/QuantumNous/new-api/model"
  8. "gorm.io/gorm"
  9. )
  10. const DoubaoAssetChannelPageSize = 50
  11. const DoubaoAssetChannelType = constant.ChannelTypeDoubaoVideoCompatibleAiping
  12. var DoubaoAssetChannelTypes = []int{
  13. constant.ChannelTypeDoubaoVideoCompatibleAiping,
  14. constant.ChannelTypeDoubaoVideoCompatibleTianyiYun,
  15. }
  16. func ResolveDoubaoAssetChannel(userId int, tokenGroup string) (*model.Channel, error) {
  17. return ResolveDoubaoAssetChannelForModel(userId, tokenGroup, "")
  18. }
  19. func ResolveDoubaoAssetChannelForModel(userId int, tokenGroup string, modelName string) (*model.Channel, error) {
  20. channelType := DoubaoAssetChannelTypeForModel(modelName)
  21. channel, err := getBoundDoubaoAssetChannelByType(userId, tokenGroup, channelType)
  22. if err != nil {
  23. return nil, err
  24. }
  25. if channel != nil {
  26. return channel, nil
  27. }
  28. channel, err = autoMatchDoubaoAssetChannelByType(tokenGroup, channelType)
  29. if err != nil {
  30. return nil, err
  31. }
  32. if channel == nil {
  33. return nil, nil
  34. }
  35. if err = model.BindUserAssetChannel(userId, channel.Type, tokenGroup, channel.Id); err != nil {
  36. return nil, err
  37. }
  38. return channel, nil
  39. }
  40. func GetBoundDoubaoAssetChannel(userId int, tokenGroup string) (*model.Channel, error) {
  41. return getBoundDoubaoAssetChannelByType(userId, tokenGroup, DoubaoAssetChannelType)
  42. }
  43. func GetBoundDoubaoAssetChannelForModel(userId int, tokenGroup string, modelName string) (*model.Channel, error) {
  44. return getBoundDoubaoAssetChannelByType(userId, tokenGroup, DoubaoAssetChannelTypeForModel(modelName))
  45. }
  46. func getBoundDoubaoAssetChannelByType(userId int, tokenGroup string, channelType int) (*model.Channel, error) {
  47. binding, err := model.GetUserAssetChannel(userId, channelType, tokenGroup)
  48. if err != nil {
  49. return nil, err
  50. }
  51. if binding == nil {
  52. return nil, nil
  53. }
  54. channel, err := model.CacheGetChannel(binding.ChannelId)
  55. if err != nil {
  56. if errors.Is(err, gorm.ErrRecordNotFound) || common.MemoryCacheEnabled {
  57. return nil, nil
  58. }
  59. return nil, err
  60. }
  61. if !IsUsableDoubaoAssetChannel(channel, tokenGroup) {
  62. return nil, nil
  63. }
  64. return channel, nil
  65. }
  66. func HasDoubaoVideoChannelForGroupModel(tokenGroup string, modelName string) bool {
  67. if tokenGroup == "" || modelName == "" {
  68. return false
  69. }
  70. for _, channelType := range DoubaoAssetChannelTypes {
  71. for startIdx := 0; ; startIdx += DoubaoAssetChannelPageSize {
  72. candidates, err := model.GetChannelsByType(startIdx, DoubaoAssetChannelPageSize, true, channelType)
  73. if err != nil {
  74. return false
  75. }
  76. for _, candidate := range candidates {
  77. if candidate == nil || !MatchDoubaoAssetGroup(candidate.GetGroups(), tokenGroup) {
  78. continue
  79. }
  80. if model.IsChannelEnabledForGroupModel(tokenGroup, modelName, candidate.Id) {
  81. return true
  82. }
  83. }
  84. if len(candidates) < DoubaoAssetChannelPageSize {
  85. break
  86. }
  87. }
  88. }
  89. return false
  90. }
  91. func AutoMatchDoubaoAssetChannel(tokenGroup string) (*model.Channel, error) {
  92. return autoMatchDoubaoAssetChannelByType(tokenGroup, DoubaoAssetChannelType)
  93. }
  94. func autoMatchDoubaoAssetChannelByType(tokenGroup string, channelType int) (*model.Channel, error) {
  95. for startIdx := 0; ; startIdx += DoubaoAssetChannelPageSize {
  96. candidates, err := model.GetChannelsByType(startIdx, DoubaoAssetChannelPageSize, true, channelType)
  97. if err != nil {
  98. return nil, err
  99. }
  100. for _, candidate := range candidates {
  101. if candidate == nil || !MatchDoubaoAssetGroup(candidate.GetGroups(), tokenGroup) {
  102. continue
  103. }
  104. channel, err := model.CacheGetChannel(candidate.Id)
  105. if err != nil {
  106. if errors.Is(err, gorm.ErrRecordNotFound) || common.MemoryCacheEnabled {
  107. continue
  108. }
  109. return nil, err
  110. }
  111. if IsUsableDoubaoAssetChannel(channel, tokenGroup) {
  112. return channel, nil
  113. }
  114. }
  115. if len(candidates) < DoubaoAssetChannelPageSize {
  116. return nil, nil
  117. }
  118. }
  119. }
  120. func IsUsableDoubaoAssetChannel(channel *model.Channel, tokenGroup string) bool {
  121. if channel == nil {
  122. return false
  123. }
  124. if !IsDoubaoAssetChannelType(channel.Type) {
  125. return false
  126. }
  127. if channel.Status != common.ChannelStatusEnabled {
  128. return false
  129. }
  130. if strings.TrimSpace(channel.Key) == "" || len(channel.GetKeys()) == 0 {
  131. return false
  132. }
  133. return MatchDoubaoAssetGroup(channel.GetGroups(), tokenGroup)
  134. }
  135. func IsDoubaoAssetChannelType(channelType int) bool {
  136. for _, t := range DoubaoAssetChannelTypes {
  137. if channelType == t {
  138. return true
  139. }
  140. }
  141. return false
  142. }
  143. func DoubaoAssetChannelTypeForModel(modelName string) int {
  144. if strings.HasPrefix(strings.ToLower(strings.TrimSpace(modelName)), "cdance") {
  145. return constant.ChannelTypeDoubaoVideoCompatibleTianyiYun
  146. }
  147. return DoubaoAssetChannelType
  148. }
  149. func MatchDoubaoAssetGroup(channelGroups []string, tokenGroup string) bool {
  150. tokenGroup = strings.TrimSpace(tokenGroup)
  151. if tokenGroup == "" {
  152. return false
  153. }
  154. for _, group := range channelGroups {
  155. if strings.TrimSpace(group) == tokenGroup {
  156. return true
  157. }
  158. }
  159. return false
  160. }