video_proxy_gemini.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package controller
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "strconv"
  7. "strings"
  8. "github.com/QuantumNous/new-api/constant"
  9. "github.com/QuantumNous/new-api/model"
  10. "github.com/QuantumNous/new-api/relay"
  11. )
  12. func getGeminiVideoURL(channel *model.Channel, task *model.Task, apiKey string) (string, error) {
  13. if channel == nil || task == nil {
  14. return "", fmt.Errorf("invalid channel or task")
  15. }
  16. if url := extractGeminiVideoURLFromTaskData(task); url != "" {
  17. return ensureAPIKey(url, apiKey), nil
  18. }
  19. baseURL := constant.ChannelBaseURLs[channel.Type]
  20. if channel.GetBaseURL() != "" {
  21. baseURL = channel.GetBaseURL()
  22. }
  23. adaptor := relay.GetTaskAdaptor(constant.TaskPlatform(strconv.Itoa(channel.Type)))
  24. if adaptor == nil {
  25. return "", fmt.Errorf("gemini task adaptor not found")
  26. }
  27. if apiKey == "" {
  28. return "", fmt.Errorf("api key not available for task")
  29. }
  30. proxy := channel.GetSetting().Proxy
  31. resp, err := adaptor.FetchTask(baseURL, apiKey, map[string]any{
  32. "task_id": task.TaskID,
  33. "action": task.Action,
  34. }, proxy)
  35. if err != nil {
  36. return "", fmt.Errorf("fetch task failed: %w", err)
  37. }
  38. defer resp.Body.Close()
  39. body, err := io.ReadAll(resp.Body)
  40. if err != nil {
  41. return "", fmt.Errorf("read task response failed: %w", err)
  42. }
  43. taskInfo, parseErr := adaptor.ParseTaskResult(body)
  44. if parseErr == nil && taskInfo != nil && taskInfo.RemoteUrl != "" {
  45. return ensureAPIKey(taskInfo.RemoteUrl, apiKey), nil
  46. }
  47. if url := extractGeminiVideoURLFromPayload(body); url != "" {
  48. return ensureAPIKey(url, apiKey), nil
  49. }
  50. if parseErr != nil {
  51. return "", fmt.Errorf("parse task result failed: %w", parseErr)
  52. }
  53. return "", fmt.Errorf("gemini video url not found")
  54. }
  55. func extractGeminiVideoURLFromTaskData(task *model.Task) string {
  56. if task == nil || len(task.Data) == 0 {
  57. return ""
  58. }
  59. var payload map[string]any
  60. if err := json.Unmarshal(task.Data, &payload); err != nil {
  61. return ""
  62. }
  63. return extractGeminiVideoURLFromMap(payload)
  64. }
  65. func extractGeminiVideoURLFromPayload(body []byte) string {
  66. var payload map[string]any
  67. if err := json.Unmarshal(body, &payload); err != nil {
  68. return ""
  69. }
  70. return extractGeminiVideoURLFromMap(payload)
  71. }
  72. func extractGeminiVideoURLFromMap(payload map[string]any) string {
  73. if payload == nil {
  74. return ""
  75. }
  76. if uri, ok := payload["uri"].(string); ok && uri != "" {
  77. return uri
  78. }
  79. if resp, ok := payload["response"].(map[string]any); ok {
  80. if uri := extractGeminiVideoURLFromResponse(resp); uri != "" {
  81. return uri
  82. }
  83. }
  84. return ""
  85. }
  86. func extractGeminiVideoURLFromResponse(resp map[string]any) string {
  87. if resp == nil {
  88. return ""
  89. }
  90. if gvr, ok := resp["generateVideoResponse"].(map[string]any); ok {
  91. if uri := extractGeminiVideoURLFromGeneratedSamples(gvr); uri != "" {
  92. return uri
  93. }
  94. }
  95. if videos, ok := resp["videos"].([]any); ok {
  96. for _, video := range videos {
  97. if vm, ok := video.(map[string]any); ok {
  98. if uri, ok := vm["uri"].(string); ok && uri != "" {
  99. return uri
  100. }
  101. }
  102. }
  103. }
  104. if uri, ok := resp["video"].(string); ok && uri != "" {
  105. return uri
  106. }
  107. if uri, ok := resp["uri"].(string); ok && uri != "" {
  108. return uri
  109. }
  110. return ""
  111. }
  112. func extractGeminiVideoURLFromGeneratedSamples(gvr map[string]any) string {
  113. if gvr == nil {
  114. return ""
  115. }
  116. if samples, ok := gvr["generatedSamples"].([]any); ok {
  117. for _, sample := range samples {
  118. if sm, ok := sample.(map[string]any); ok {
  119. if video, ok := sm["video"].(map[string]any); ok {
  120. if uri, ok := video["uri"].(string); ok && uri != "" {
  121. return uri
  122. }
  123. }
  124. }
  125. }
  126. }
  127. return ""
  128. }
  129. func ensureAPIKey(uri, key string) string {
  130. if key == "" || uri == "" {
  131. return uri
  132. }
  133. if strings.Contains(uri, "key=") {
  134. return uri
  135. }
  136. if strings.Contains(uri, "?") {
  137. return fmt.Sprintf("%s&key=%s", uri, key)
  138. }
  139. return fmt.Sprintf("%s?key=%s", uri, key)
  140. }