video_proxy.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package controller
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "net/url"
  9. "strings"
  10. "time"
  11. "github.com/QuantumNous/new-api/constant"
  12. "github.com/QuantumNous/new-api/logger"
  13. "github.com/QuantumNous/new-api/model"
  14. "github.com/QuantumNous/new-api/service"
  15. "github.com/gin-gonic/gin"
  16. )
  17. // videoProxyError returns a standardized OpenAI-style error response.
  18. func videoProxyError(c *gin.Context, status int, errType, message string) {
  19. c.JSON(status, gin.H{
  20. "error": gin.H{
  21. "message": message,
  22. "type": errType,
  23. },
  24. })
  25. }
  26. func VideoProxy(c *gin.Context) {
  27. taskID := c.Param("task_id")
  28. if taskID == "" {
  29. videoProxyError(c, http.StatusBadRequest, "invalid_request_error", "task_id is required")
  30. return
  31. }
  32. userID := c.GetInt("id")
  33. task, exists, err := model.GetByTaskId(userID, taskID)
  34. if err != nil {
  35. logger.LogError(c.Request.Context(), fmt.Sprintf("Failed to query task %s: %s", taskID, err.Error()))
  36. videoProxyError(c, http.StatusInternalServerError, "server_error", "Failed to query task")
  37. return
  38. }
  39. if !exists || task == nil {
  40. videoProxyError(c, http.StatusNotFound, "invalid_request_error", "Task not found")
  41. return
  42. }
  43. if task.Status != model.TaskStatusSuccess {
  44. videoProxyError(c, http.StatusBadRequest, "invalid_request_error",
  45. fmt.Sprintf("Task is not completed yet, current status: %s", task.Status))
  46. return
  47. }
  48. channel, err := model.CacheGetChannel(task.ChannelId)
  49. if err != nil {
  50. logger.LogError(c.Request.Context(), fmt.Sprintf("Failed to get channel for task %s: %s", taskID, err.Error()))
  51. videoProxyError(c, http.StatusInternalServerError, "server_error", "Failed to retrieve channel information")
  52. return
  53. }
  54. baseURL := channel.GetBaseURL()
  55. if baseURL == "" {
  56. baseURL = "https://api.openai.com"
  57. }
  58. var videoURL string
  59. proxy := channel.GetSetting().Proxy
  60. client, err := service.GetHttpClientWithProxy(proxy)
  61. if err != nil {
  62. logger.LogError(c.Request.Context(), fmt.Sprintf("Failed to create proxy client for task %s: %s", taskID, err.Error()))
  63. videoProxyError(c, http.StatusInternalServerError, "server_error", "Failed to create proxy client")
  64. return
  65. }
  66. ctx, cancel := context.WithTimeout(c.Request.Context(), 60*time.Second)
  67. defer cancel()
  68. req, err := http.NewRequestWithContext(ctx, http.MethodGet, "", nil)
  69. if err != nil {
  70. logger.LogError(c.Request.Context(), fmt.Sprintf("Failed to create request: %s", err.Error()))
  71. videoProxyError(c, http.StatusInternalServerError, "server_error", "Failed to create proxy request")
  72. return
  73. }
  74. switch channel.Type {
  75. case constant.ChannelTypeGemini:
  76. apiKey := task.PrivateData.Key
  77. if apiKey == "" {
  78. logger.LogError(c.Request.Context(), fmt.Sprintf("Missing stored API key for Gemini task %s", taskID))
  79. videoProxyError(c, http.StatusInternalServerError, "server_error", "API key not stored for task")
  80. return
  81. }
  82. videoURL, err = getGeminiVideoURL(channel, task, apiKey)
  83. if err != nil {
  84. logger.LogError(c.Request.Context(), fmt.Sprintf("Failed to resolve Gemini video URL for task %s: %s", taskID, err.Error()))
  85. videoProxyError(c, http.StatusBadGateway, "server_error", "Failed to resolve Gemini video URL")
  86. return
  87. }
  88. req.Header.Set("x-goog-api-key", apiKey)
  89. case constant.ChannelTypeVertexAi:
  90. videoURL, err = getVertexVideoURL(channel, task)
  91. if err != nil {
  92. logger.LogError(c.Request.Context(), fmt.Sprintf("Failed to resolve Vertex video URL for task %s: %s", taskID, err.Error()))
  93. videoProxyError(c, http.StatusBadGateway, "server_error", "Failed to resolve Vertex video URL")
  94. return
  95. }
  96. case constant.ChannelTypeOpenAI, constant.ChannelTypeSora:
  97. videoURL = fmt.Sprintf("%s/v1/videos/%s/content", baseURL, task.GetUpstreamTaskID())
  98. req.Header.Set("Authorization", "Bearer "+channel.Key)
  99. default:
  100. // Video URL is stored in PrivateData.ResultURL (fallback to FailReason for old data)
  101. videoURL = task.GetResultURL()
  102. }
  103. videoURL = strings.TrimSpace(videoURL)
  104. if videoURL == "" {
  105. logger.LogError(c.Request.Context(), fmt.Sprintf("Video URL is empty for task %s", taskID))
  106. videoProxyError(c, http.StatusBadGateway, "server_error", "Failed to fetch video content")
  107. return
  108. }
  109. if strings.HasPrefix(videoURL, "data:") {
  110. if err := writeVideoDataURL(c, videoURL); err != nil {
  111. logger.LogError(c.Request.Context(), fmt.Sprintf("Failed to decode video data URL for task %s: %s", taskID, err.Error()))
  112. videoProxyError(c, http.StatusBadGateway, "server_error", "Failed to fetch video content")
  113. }
  114. return
  115. }
  116. req.URL, err = url.Parse(videoURL)
  117. if err != nil {
  118. logger.LogError(c.Request.Context(), fmt.Sprintf("Failed to parse URL %s: %s", videoURL, err.Error()))
  119. videoProxyError(c, http.StatusInternalServerError, "server_error", "Failed to create proxy request")
  120. return
  121. }
  122. resp, err := client.Do(req)
  123. if err != nil {
  124. logger.LogError(c.Request.Context(), fmt.Sprintf("Failed to fetch video from %s: %s", videoURL, err.Error()))
  125. videoProxyError(c, http.StatusBadGateway, "server_error", "Failed to fetch video content")
  126. return
  127. }
  128. defer resp.Body.Close()
  129. if resp.StatusCode != http.StatusOK {
  130. logger.LogError(c.Request.Context(), fmt.Sprintf("Upstream returned status %d for %s", resp.StatusCode, videoURL))
  131. videoProxyError(c, http.StatusBadGateway, "server_error",
  132. fmt.Sprintf("Upstream service returned status %d", resp.StatusCode))
  133. return
  134. }
  135. for key, values := range resp.Header {
  136. for _, value := range values {
  137. c.Writer.Header().Add(key, value)
  138. }
  139. }
  140. c.Writer.Header().Set("Cache-Control", "public, max-age=86400")
  141. c.Writer.WriteHeader(resp.StatusCode)
  142. if _, err = io.Copy(c.Writer, resp.Body); err != nil {
  143. logger.LogError(c.Request.Context(), fmt.Sprintf("Failed to stream video content: %s", err.Error()))
  144. }
  145. }
  146. func writeVideoDataURL(c *gin.Context, dataURL string) error {
  147. parts := strings.SplitN(dataURL, ",", 2)
  148. if len(parts) != 2 {
  149. return fmt.Errorf("invalid data url")
  150. }
  151. header := parts[0]
  152. payload := parts[1]
  153. if !strings.HasPrefix(header, "data:") || !strings.Contains(header, ";base64") {
  154. return fmt.Errorf("unsupported data url")
  155. }
  156. mimeType := strings.TrimPrefix(header, "data:")
  157. mimeType = strings.TrimSuffix(mimeType, ";base64")
  158. if mimeType == "" {
  159. mimeType = "video/mp4"
  160. }
  161. videoBytes, err := base64.StdEncoding.DecodeString(payload)
  162. if err != nil {
  163. videoBytes, err = base64.RawStdEncoding.DecodeString(payload)
  164. if err != nil {
  165. return err
  166. }
  167. }
  168. c.Writer.Header().Set("Content-Type", mimeType)
  169. c.Writer.Header().Set("Cache-Control", "public, max-age=86400")
  170. c.Writer.WriteHeader(http.StatusOK)
  171. _, err = c.Writer.Write(videoBytes)
  172. return err
  173. }