-
Notifications
You must be signed in to change notification settings - Fork 38
/
luma.go
291 lines (255 loc) · 7.25 KB
/
luma.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"luma-api/common"
"net/http"
"regexp"
"strings"
"sync"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
)
const (
SubmitEndpoint = "/api/photon/v1/generations/"
GetTaskEndpoint = "/api/photon/v1/generations%s"
FileUploadEndpoint = "/api/photon/v1/generations/file_upload"
UsageEndpoint = "/api/photon/v1/subscription/usage"
UserinfoEndpoint = "/api/users/v1/me"
DownloadEndpoint = "/api/photon/v1/generations/%s/download_video_url"
)
var CommonHeaders = map[string]string{
"Content-Type": "application/json",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36",
"Referer": "https://lumalabs.ai/",
"Origin": "https://lumalabs.ai",
"Accept": "*/*",
}
func WrapperLumaError(c *gin.Context, err error, statusCode int) {
common.Logger.Errorw("wrapper luma error", "statusCode", statusCode, "err", err)
c.JSON(statusCode, gin.H{
"detail": map[string]any{
"reason": err.Error(),
"code": 1,
},
})
}
func GenLumaError(err error, statusCode int) *WrapperErrorResp {
return &WrapperErrorResp{
StatusCode: statusCode,
ErrorResp: ErrorResp{
Detail: err.Error(),
},
}
}
func ReturnLumaError(c *gin.Context, err ErrorResp, statusCode int) {
common.Logger.Errorw("wrapper luma error", "statusCode", statusCode, "err", err)
c.JSON(statusCode, err)
}
func GetLumaAuth() string {
return common.COOKIE
}
var mu sync.Mutex
func HandleRespCookies(resp *http.Response) {
setCookies := resp.Header.Values("Set-Cookie")
if len(setCookies) == 0 {
return
}
mu.Lock()
defer mu.Unlock()
common.Logger.Infow("Luma账号触发返回cookie")
//get old cookies
cookies := make(map[string]string)
for _, cookie := range strings.Split(common.COOKIE, ";") {
kv := strings.Split(cookie, "=")
if len(kv) == 2 {
cookies[strings.TrimSpace(kv[0])] = strings.TrimSpace(kv[1])
}
}
for _, cookie := range setCookies {
names := strings.Split(cookie, "; ")
kv := strings.Split(names[0], "=")
if len(kv) == 2 {
cookies[strings.TrimSpace(kv[0])] = strings.TrimSpace(kv[1])
}
}
var cookieArr []string
for k, v := range cookies {
cookieArr = append(cookieArr, k+"="+v)
}
common.COOKIE = strings.Join(cookieArr, "; ")
return
}
func doGeneration(c *gin.Context) {
var genRequest GenRequest
err := c.BindJSON(&genRequest)
if err != nil {
WrapperLumaError(c, err, http.StatusBadRequest)
return
}
if genRequest.ImageUrl != "" && !strings.HasPrefix(genRequest.ImageUrl, "https://storage.cdn-luma.com/app_data/photon") {
uploadRes, relayErr := uploadFile(genRequest.ImageUrl)
if relayErr != nil {
ReturnLumaError(c, relayErr.ErrorResp, relayErr.StatusCode)
return
}
common.Logger.Infow("upload file success", "uploadRes", uploadRes)
genRequest.ImageUrl = uploadRes.PublicUrl
}
if genRequest.ImageEndUrl != "" && !strings.HasPrefix(genRequest.ImageEndUrl, "https://storage.cdn-luma.com/app_data/photon") {
uploadRes, relayErr := uploadFile(genRequest.ImageEndUrl)
if relayErr != nil {
ReturnLumaError(c, relayErr.ErrorResp, relayErr.StatusCode)
return
}
common.Logger.Infow("upload file success", "uploadRes", uploadRes)
genRequest.ImageEndUrl = uploadRes.PublicUrl
}
reqData, _ := json.Marshal(genRequest)
url := common.BaseUrl + SubmitEndpoint
if strings.HasSuffix(c.Request.URL.Path, "/extend") {
paths := strings.Split(c.Request.URL.Path, "/generations/")
url += paths[1]
}
resp, err := DoRequest("POST", url, bytes.NewReader(reqData), nil)
if err != nil {
WrapperLumaError(c, err, http.StatusInternalServerError)
return
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
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
}
}
body, err := io.ReadAll(resp.Body)
if err != nil {
WrapperLumaError(c, err, http.StatusInternalServerError)
return
}
var res []any
err = json.Unmarshal(body, &res)
if err != nil {
WrapperLumaError(c, err, http.StatusInternalServerError)
return
}
c.JSON(resp.StatusCode, res[0])
}
// support base64\url
func uploadFile(imgFile string) (*FileUploadResult, *WrapperErrorResp) {
signedUpload, relayErr := getSignedUpload()
if relayErr != nil {
return nil, relayErr
}
presignedURL := signedUpload.PresignedUrl
file, err := readImage(imgFile)
if err != nil {
return nil, GenLumaError(err, http.StatusInternalServerError)
}
resp, err := DoRequest(http.MethodPut, presignedURL, file, map[string]string{
"Content-Type": "image/*",
})
if err != nil {
return nil, GenLumaError(err, http.StatusInternalServerError)
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
return signedUpload, nil
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, GenLumaError(err, http.StatusInternalServerError)
}
return nil, GenLumaError(fmt.Errorf(string(body)), resp.StatusCode)
}
func getSignedUpload() (*FileUploadResult, *WrapperErrorResp) {
url := common.BaseUrl + FileUploadEndpoint + "?file_type=image&filename=file.jpg"
resp, err := DoRequest(http.MethodPost, url, nil, nil)
if err != nil {
return nil, GenLumaError(err, http.StatusInternalServerError)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, GenLumaError(err, http.StatusInternalServerError)
}
if resp.StatusCode >= 400 {
return nil, GenLumaError(fmt.Errorf(string(body)), resp.StatusCode)
}
var result FileUploadResult
err = json.Unmarshal(body, &result)
if err != nil {
return nil, GenLumaError(err, http.StatusInternalServerError)
}
return &result, nil
}
func readImage(image string) (io.Reader, error) {
if strings.HasPrefix(image, "data:image/") {
return getImageFromBase64(image)
}
return getImageFromUrl(image)
}
var (
reg = regexp.MustCompile(`data:image/([^;]+);base64,`)
)
func getImageFromBase64(encoded string) (data io.Reader, err error) {
decoded, err := base64.StdEncoding.DecodeString(reg.ReplaceAllString(encoded, ""))
if err != nil {
return nil, err
}
return bytes.NewReader(decoded), nil
}
func getImageFromUrl(url string) (data io.Reader, err error) {
resp, err := http.Get(url)
if err != nil {
return
}
defer resp.Body.Close()
dataBytes, err := io.ReadAll(resp.Body)
if err != nil {
return
}
return bytes.NewReader(dataBytes), nil
}
func getMe() (map[string]any, error) {
resp, err := DoRequest(http.MethodGet, common.BaseUrl+UserinfoEndpoint, nil, nil)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, errors.New(resp.Status)
}
var res = make(map[string]any)
err = json.NewDecoder(resp.Body).Decode(&res)
if err != nil {
return nil, err
}
return res, nil
}
func getUsage() (map[string]any, error) {
resp, err := DoRequest(http.MethodGet, common.BaseUrl+UsageEndpoint, nil, nil)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, errors.New(resp.Status)
}
var res = make(map[string]any)
err = json.NewDecoder(resp.Body).Decode(&res)
if err != nil {
return nil, err
}
return res, nil
}