Skip to content

Commit

Permalink
fix: 修复docker、文档错误
Browse files Browse the repository at this point in the history
fix: 修复docker、文档错误

test

fix: doc

fix

feat: chat

feat: chat

feat: chat

feat: chat
  • Loading branch information
xyfacai authored and LumaAI-API committed Jun 18, 2024
1 parent 1baf3fb commit f36dff0
Show file tree
Hide file tree
Showing 22 changed files with 903 additions and 56 deletions.
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ FROM golang AS builder

ENV GO111MODULE=on \
GOOS=linux \
GOPROXY=https://goproxy.cn,direct
GOPROXY=https://goproxy.cn,direct \
CGO_ENABLED=0

WORKDIR /build
ADD go.mod go.sum ./
Expand All @@ -14,8 +15,7 @@ FROM alpine:latest

RUN apk update \
&& apk upgrade \
&& apk add --no-cache ca-certificates tzdata gcc \
&& update-ca-certificates 2>/dev/null || true
&& apk add --no-cache ca-certificates tzdata gcc

COPY --from=builder /build/lumaApi /

Expand Down
Binary file modified README.md
Binary file not shown.
Binary file modified README_ZH.md
Binary file not shown.
213 changes: 170 additions & 43 deletions api.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
package main

import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"io"
"luma-api/common"
"net/http"
"regexp"
"strings"
)

const (
SubmitEndpoint = "/api/photon/v1/generations/"
GetTaskEndpoint = "/api/photon/v1/generations%s"
FileUploadEndpoint = "/api/photon/v1/generations/file_upload"
)

var CommonHeaders = map[string]string{
Expand All @@ -25,42 +36,59 @@ var CommonHeaders = map[string]string{
// @Success 200 {object} []VideoTask "generate result"
// @Router /luma/generations [post]
func Generations(c *gin.Context) {
header := map[string]string{
"Cookie": common.COOKIE,
var genRequest GenRequest
err := c.BindJSON(&genRequest)
if err != nil {
common.WrapperLumaError(c, err, http.StatusInternalServerError)
return
}
for k, v := range CommonHeaders {
header[k] = v
if genRequest.ImageUrl != "" && !strings.HasPrefix(genRequest.ImageUrl, "https://storage.cdn-luma.com/app_data/photon") {
uploadRes, err := uploadFile(genRequest.ImageUrl)
if err != nil {
common.WrapperLumaError(c, err, http.StatusInternalServerError)
return
}
common.Logger.Infow("upload file success", "uploadRes", uploadRes)
genRequest.ImageUrl = uploadRes.PublicUrl
}

resp, err := DoRequest("POST", fmt.Sprintf(common.BaseUrl+"/api/photon/v1/generations/"), c.Request.Body, header)
reqData, _ := json.Marshal(genRequest)

resp, err := DoRequest("POST", fmt.Sprintf(common.BaseUrl+SubmitEndpoint), bytes.NewReader(reqData), nil)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"detail": map[string]any{
"reason": err.Error(),
"code": 1,
},
})
common.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)
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 {
common.WrapperLumaError(c, err, http.StatusInternalServerError)
return
}
}
// 读取响应体
_, err = io.Copy(c.Writer, resp.Body)

body, err := io.ReadAll(resp.Body)
if err != nil {
common.WrapperLumaError(c, err, http.StatusInternalServerError)
return
}
var res []any
err = json.Unmarshal(body, &res)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"detail": map[string]any{
"reason": err.Error(),
"code": 1,
},
})
common.WrapperLumaError(c, err, http.StatusInternalServerError)
return
}

c.JSON(resp.StatusCode, res[0])
}

// @Summary Submit luma generate video task
Expand All @@ -74,24 +102,13 @@ func Generations(c *gin.Context) {
func Fetch(c *gin.Context) {
action := c.Param("action")

header := map[string]string{
"Cookie": common.COOKIE,
}
for k, v := range CommonHeaders {
header[k] = v
}
url := fmt.Sprintf(common.BaseUrl+"/api/photon/v1/generations%s", action)
url := fmt.Sprintf(common.BaseUrl+GetTaskEndpoint, action)
if c.Request.URL.RawQuery != "" {
url = fmt.Sprintf("%s?%s", url, c.Request.URL.RawQuery)
}
resp, err := DoRequest("GET", url, nil, header)
resp, err := DoRequest("GET", url, nil, nil)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"detail": map[string]any{
"reason": err.Error(),
"code": 1,
},
})
common.WrapperLumaError(c, err, http.StatusInternalServerError)
return
}
defer resp.Body.Close()
Expand All @@ -105,13 +122,123 @@ func Fetch(c *gin.Context) {
// 读取响应体
_, err = io.Copy(c.Writer, resp.Body)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"detail": map[string]any{
"reason": err.Error(),
"code": 1,
},
})
common.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 {
common.WrapperLumaError(c, err, http.StatusInternalServerError)
return
}

res, err := uploadFile(uploadParams.Url)
if err != nil {
common.WrapperLumaError(c, err, http.StatusInternalServerError)
return
}

c.JSON(http.StatusOK, res)
return
}

// support base64\url
func uploadFile(imgFile string) (*FileUploadResult, error) {
signedUpload, err := getSignedUpload()
if err != nil {
return nil, err
}

presignedURL := signedUpload.PresignedUrl

file, err := readImage(imgFile)
if err != nil {
return nil, err
}

resp, err := DoRequest(http.MethodPut, presignedURL, file, map[string]string{
"Content-Type": "image/*",
})
if err != nil {
return nil, err
}

defer resp.Body.Close()

if resp.StatusCode == http.StatusOK {
return signedUpload, nil
}

return nil, fmt.Errorf("uploud file failed: %s", resp.Status)
}

func getSignedUpload() (*FileUploadResult, error) {
url := common.BaseUrl + FileUploadEndpoint + "?file_type=image&filename=file.jpg"

resp, err := DoRequest(http.MethodPost, url, nil, nil)
if err != nil {
return nil, err
}

defer resp.Body.Close()
if resp.StatusCode >= 400 {
return nil, fmt.Errorf(resp.Status)
}

body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}

var result FileUploadResult
err = json.Unmarshal(body, &result)
if err != nil {
return nil, err
}
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
}
Loading

0 comments on commit f36dff0

Please sign in to comment.