|
| 1 | +package controller |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "one-api/logger" |
| 8 | + "one-api/model" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/gin-gonic/gin" |
| 12 | +) |
| 13 | + |
| 14 | +func VideoProxy(c *gin.Context) { |
| 15 | + taskID := c.Param("task_id") |
| 16 | + if taskID == "" { |
| 17 | + c.JSON(http.StatusBadRequest, gin.H{ |
| 18 | + "error": gin.H{ |
| 19 | + "message": "task_id is required", |
| 20 | + "type": "invalid_request_error", |
| 21 | + }, |
| 22 | + }) |
| 23 | + return |
| 24 | + } |
| 25 | + |
| 26 | + task, exists, err := model.GetByOnlyTaskId(taskID) |
| 27 | + if err != nil { |
| 28 | + logger.LogError(c.Request.Context(), fmt.Sprintf("Failed to query task %s: %s", taskID, err.Error())) |
| 29 | + c.JSON(http.StatusInternalServerError, gin.H{ |
| 30 | + "error": gin.H{ |
| 31 | + "message": "Failed to query task", |
| 32 | + "type": "server_error", |
| 33 | + }, |
| 34 | + }) |
| 35 | + return |
| 36 | + } |
| 37 | + if !exists || task == nil { |
| 38 | + logger.LogError(c.Request.Context(), fmt.Sprintf("Failed to get task %s: %s", taskID, err.Error())) |
| 39 | + c.JSON(http.StatusNotFound, gin.H{ |
| 40 | + "error": gin.H{ |
| 41 | + "message": "Task not found", |
| 42 | + "type": "invalid_request_error", |
| 43 | + }, |
| 44 | + }) |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + if task.Status != model.TaskStatusSuccess { |
| 49 | + c.JSON(http.StatusBadRequest, gin.H{ |
| 50 | + "error": gin.H{ |
| 51 | + "message": fmt.Sprintf("Task is not completed yet, current status: %s", task.Status), |
| 52 | + "type": "invalid_request_error", |
| 53 | + }, |
| 54 | + }) |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + channel, err := model.CacheGetChannel(task.ChannelId) |
| 59 | + if err != nil { |
| 60 | + logger.LogError(c.Request.Context(), fmt.Sprintf("Failed to get channel %d: %s", task.ChannelId, err.Error())) |
| 61 | + c.JSON(http.StatusInternalServerError, gin.H{ |
| 62 | + "error": gin.H{ |
| 63 | + "message": "Failed to retrieve channel information", |
| 64 | + "type": "server_error", |
| 65 | + }, |
| 66 | + }) |
| 67 | + return |
| 68 | + } |
| 69 | + baseURL := channel.GetBaseURL() |
| 70 | + if baseURL == "" { |
| 71 | + baseURL = "https://api.openai.com" |
| 72 | + } |
| 73 | + videoURL := fmt.Sprintf("%s/v1/videos/%s/content", baseURL, task.TaskID) |
| 74 | + |
| 75 | + client := &http.Client{ |
| 76 | + Timeout: 60 * time.Second, |
| 77 | + } |
| 78 | + |
| 79 | + req, err := http.NewRequestWithContext(c.Request.Context(), http.MethodGet, videoURL, nil) |
| 80 | + if err != nil { |
| 81 | + logger.LogError(c.Request.Context(), fmt.Sprintf("Failed to create request for %s: %s", videoURL, err.Error())) |
| 82 | + c.JSON(http.StatusInternalServerError, gin.H{ |
| 83 | + "error": gin.H{ |
| 84 | + "message": "Failed to create proxy request", |
| 85 | + "type": "server_error", |
| 86 | + }, |
| 87 | + }) |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + req.Header.Set("Authorization", "Bearer "+channel.Key) |
| 92 | + |
| 93 | + resp, err := client.Do(req) |
| 94 | + if err != nil { |
| 95 | + logger.LogError(c.Request.Context(), fmt.Sprintf("Failed to fetch video from %s: %s", videoURL, err.Error())) |
| 96 | + c.JSON(http.StatusBadGateway, gin.H{ |
| 97 | + "error": gin.H{ |
| 98 | + "message": "Failed to fetch video content", |
| 99 | + "type": "server_error", |
| 100 | + }, |
| 101 | + }) |
| 102 | + return |
| 103 | + } |
| 104 | + defer resp.Body.Close() |
| 105 | + |
| 106 | + if resp.StatusCode != http.StatusOK { |
| 107 | + logger.LogError(c.Request.Context(), fmt.Sprintf("Upstream returned status %d for %s", resp.StatusCode, videoURL)) |
| 108 | + c.JSON(http.StatusBadGateway, gin.H{ |
| 109 | + "error": gin.H{ |
| 110 | + "message": fmt.Sprintf("Upstream service returned status %d", resp.StatusCode), |
| 111 | + "type": "server_error", |
| 112 | + }, |
| 113 | + }) |
| 114 | + return |
| 115 | + } |
| 116 | + |
| 117 | + for key, values := range resp.Header { |
| 118 | + for _, value := range values { |
| 119 | + c.Writer.Header().Add(key, value) |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + c.Writer.Header().Set("Cache-Control", "public, max-age=86400") // Cache for 24 hours |
| 124 | + c.Writer.WriteHeader(resp.StatusCode) |
| 125 | + _, err = io.Copy(c.Writer, resp.Body) |
| 126 | + if err != nil { |
| 127 | + logger.LogError(c.Request.Context(), fmt.Sprintf("Failed to stream video content: %s", err.Error())) |
| 128 | + } |
| 129 | +} |
0 commit comments