-
Notifications
You must be signed in to change notification settings - Fork 38
/
api.go
159 lines (148 loc) · 3.84 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"io"
"luma-api/common"
"net/http"
)
// @Summary Submit luma generate video task
// @Schemes
// @Description
// @Accept json
// @Produce json
// @Param body body GenRequest true "submit generate video"
// @Success 200 {object} []VideoTask "generate result"
// @Router /luma/generations [post]
func Generations(c *gin.Context) {
doGeneration(c)
}
// @Summary Submit luma extend generate video task
// @Schemes
// @Description
// @Accept json
// @Produce json
// @Param task_id path string true "extend task id"
// @Param body body GenRequest true "submit generate video"
// @Success 200 {object} []VideoTask "generate result"
// @Router /luma/generations/:task_id/extend [post]
func ExtentGenerations(c *gin.Context) {
doGeneration(c)
}
// @Summary Get luma generate video task
// @Schemes
// @Description
// @Accept json
// @Produce json
// @Param task_id path string true "fetch single task by id"
// @Success 200 {object} VideoTask "video single task"
// @Router /luma/generations/{task_id} [get]
func FetchByID(c *gin.Context) {
id := c.Param("task_id")
url := fmt.Sprintf(common.BaseUrl+GetTaskEndpoint, "/"+id)
if c.Request.URL.RawQuery != "" {
url = fmt.Sprintf("%s?%s", url, c.Request.URL.RawQuery)
}
resp, err := DoRequest("GET", url, nil, nil)
if err != nil {
WrapperLumaError(c, err, http.StatusInternalServerError)
return
}
defer resp.Body.Close()
c.Writer.WriteHeader(resp.StatusCode)
for key, values := range resp.Header {
for _, value := range values {
c.Writer.Header().Add(key, value)
}
}
// 读取响应体
_, err = io.Copy(c.Writer, resp.Body)
if err != nil {
WrapperLumaError(c, err, http.StatusInternalServerError)
return
}
return
}
// @Summary Upload image to luma
// @Schemes
// @Description
// @Accept json
// @Produce json
// @Param body body UploadReq true "Upload image params"
// @Success 200 {object} []FileUploadResult "upload result"
// @Router /luma/generations/file_upload [post]
func Upload(c *gin.Context) {
var uploadParams UploadReq
err := c.BindJSON(&uploadParams)
if err != nil {
WrapperLumaError(c, err, http.StatusBadRequest)
return
}
res, relayErr := uploadFile(uploadParams.Url)
if relayErr != nil {
ReturnLumaError(c, relayErr.ErrorResp, relayErr.StatusCode)
return
}
c.JSON(http.StatusOK, res)
return
}
// @Summary Get video url without watermark
// @Schemes
// @Description
// @Accept json
// @Produce json
// @Param task_id path string true "fetch by id"
// @Success 200 {object} object "url"
// @Router /luma/generations/{task_id}/download_video_url [post]
func GetDownloadUrl(c *gin.Context) {
taskID := c.Param("task_id")
resp, err := DoRequest(http.MethodGet, fmt.Sprintf(common.BaseUrl+DownloadEndpoint, taskID), nil, nil)
if err != nil {
WrapperLumaError(c, err, http.StatusInternalServerError)
return
}
defer resp.Body.Close()
c.Writer.WriteHeader(resp.StatusCode)
for key, values := range resp.Header {
for _, value := range values {
c.Writer.Header().Add(key, value)
}
}
// 读取响应体
_, err = io.Copy(c.Writer, resp.Body)
if err != nil {
WrapperLumaError(c, err, http.StatusInternalServerError)
return
}
return
}
// @Summary Get current user info
// @Schemes
// @Description
// @Accept json
// @Produce json
// @Success 200 {object} object "user info"
// @Router /luma/users/me [get]
func Me(c *gin.Context) {
res, err := getMe()
if err != nil {
WrapperLumaError(c, err, http.StatusInternalServerError)
return
}
c.JSON(http.StatusOK, res)
}
// @Summary Get current user subscription usage
// @Schemes
// @Description
// @Accept json
// @Produce json
// @Success 200 {object} object "subscription info"
// @Router /luma/subscription/usage [get]
func Usage(c *gin.Context) {
res, err := getUsage()
if err != nil {
WrapperLumaError(c, err, http.StatusInternalServerError)
return
}
c.JSON(http.StatusOK, res)
}