44c5825cb0
- 在后端添加 AI 视频相关接口处理函数 - 扩展前端画布组件支持视频节点类型 - 实现视频上传、生成和播放功能 - 更新画布数据结构文档以包含视频节点 - 添加视频节点的操作工具栏和配置面板 - 集成视频存储和检索服务 - 优化视频节点的渲染和交互体验
162 lines
4.3 KiB
Go
162 lines
4.3 KiB
Go
package handler
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"io"
|
|
"log"
|
|
"mime"
|
|
"mime/multipart"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/basketikun/infinite-canvas/service"
|
|
)
|
|
|
|
func AIImagesGenerations(w http.ResponseWriter, r *http.Request) {
|
|
proxyAIRequest(w, r, "/images/generations")
|
|
}
|
|
|
|
func AIImagesEdits(w http.ResponseWriter, r *http.Request) {
|
|
proxyAIRequest(w, r, "/images/edits")
|
|
}
|
|
|
|
func AIChatCompletions(w http.ResponseWriter, r *http.Request) {
|
|
proxyAIRequest(w, r, "/chat/completions")
|
|
}
|
|
|
|
func AIVideos(w http.ResponseWriter, r *http.Request) {
|
|
proxyAIRequest(w, r, "/videos")
|
|
}
|
|
|
|
func AIVideo(w http.ResponseWriter, r *http.Request, id string) {
|
|
proxyAIGetRequest(w, r, "/videos/"+id)
|
|
}
|
|
|
|
func AIVideoContent(w http.ResponseWriter, r *http.Request, id string) {
|
|
proxyAIGetRequest(w, r, "/videos/"+id+"/content")
|
|
}
|
|
|
|
func proxyAIGetRequest(w http.ResponseWriter, r *http.Request, path string) {
|
|
modelName := r.URL.Query().Get("model")
|
|
if strings.TrimSpace(modelName) == "" {
|
|
modelName = "sora-2"
|
|
}
|
|
channel, err := service.SelectModelChannel(modelName)
|
|
if err != nil {
|
|
log.Printf("AI proxy select channel failed: model=%s err=%v", modelName, err)
|
|
Fail(w, "AI 接口请求失败")
|
|
return
|
|
}
|
|
request, err := http.NewRequest(http.MethodGet, service.BuildModelChannelURL(channel, path), nil)
|
|
if err != nil {
|
|
Fail(w, "AI 接口请求失败")
|
|
return
|
|
}
|
|
request.Header.Set("Authorization", "Bearer "+channel.APIKey)
|
|
copyAIResponse(w, request)
|
|
}
|
|
|
|
func proxyAIRequest(w http.ResponseWriter, r *http.Request, path string) {
|
|
body, contentType, modelName, err := readAIRequest(r)
|
|
if err != nil {
|
|
log.Printf("AI proxy request read failed: %v", err)
|
|
Fail(w, "AI 接口请求失败")
|
|
return
|
|
}
|
|
channel, err := service.SelectModelChannel(modelName)
|
|
if err != nil {
|
|
log.Printf("AI proxy select channel failed: model=%s err=%v", modelName, err)
|
|
Fail(w, "AI 接口请求失败")
|
|
return
|
|
}
|
|
request, err := http.NewRequest(http.MethodPost, service.BuildModelChannelURL(channel, path), bytes.NewReader(body))
|
|
if err != nil {
|
|
log.Printf("AI proxy build request failed: url=%s err=%v", service.BuildModelChannelURL(channel, path), err)
|
|
Fail(w, "AI 接口请求失败")
|
|
return
|
|
}
|
|
request.Header.Set("Authorization", "Bearer "+channel.APIKey)
|
|
if contentType != "" {
|
|
request.Header.Set("Content-Type", contentType)
|
|
}
|
|
copyAIResponse(w, request)
|
|
}
|
|
|
|
func copyAIResponse(w http.ResponseWriter, request *http.Request) {
|
|
response, err := http.DefaultClient.Do(request)
|
|
if err != nil {
|
|
log.Printf("AI proxy request failed: url=%s err=%v", request.URL.String(), err)
|
|
Fail(w, "AI 接口请求失败")
|
|
return
|
|
}
|
|
defer response.Body.Close()
|
|
|
|
if response.StatusCode >= http.StatusBadRequest {
|
|
payload, _ := io.ReadAll(io.LimitReader(response.Body, 4096))
|
|
log.Printf("AI upstream error: url=%s status=%d body=%s", request.URL.String(), response.StatusCode, strings.TrimSpace(string(payload)))
|
|
Fail(w, "AI 接口请求失败")
|
|
return
|
|
}
|
|
|
|
for key, values := range response.Header {
|
|
if strings.EqualFold(key, "Content-Length") {
|
|
continue
|
|
}
|
|
for _, value := range values {
|
|
w.Header().Add(key, value)
|
|
}
|
|
}
|
|
w.WriteHeader(response.StatusCode)
|
|
_, _ = io.Copy(w, response.Body)
|
|
}
|
|
|
|
func readAIRequest(r *http.Request) ([]byte, string, string, error) {
|
|
contentType := r.Header.Get("Content-Type")
|
|
body, err := io.ReadAll(r.Body)
|
|
if err != nil {
|
|
return nil, "", "", err
|
|
}
|
|
modelName := ""
|
|
if strings.HasPrefix(contentType, "multipart/form-data") {
|
|
modelName = readMultipartModel(body, contentType)
|
|
} else {
|
|
var payload struct {
|
|
Model string `json:"model"`
|
|
}
|
|
_ = json.Unmarshal(body, &payload)
|
|
modelName = payload.Model
|
|
}
|
|
if strings.TrimSpace(modelName) == "" {
|
|
return nil, "", "", errMissingModel
|
|
}
|
|
return body, contentType, modelName, nil
|
|
}
|
|
|
|
func readMultipartModel(body []byte, contentType string) string {
|
|
_, params, err := mime.ParseMediaType(contentType)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
reader := multipart.NewReader(bytes.NewReader(body), params["boundary"])
|
|
form, err := reader.ReadForm(32 << 20)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
defer form.RemoveAll()
|
|
if values := form.Value["model"]; len(values) > 0 {
|
|
return values[0]
|
|
}
|
|
return ""
|
|
}
|
|
|
|
var errMissingModel = &aiError{"缺少模型名称"}
|
|
|
|
type aiError struct {
|
|
message string
|
|
}
|
|
|
|
func (err *aiError) Error() string {
|
|
return err.message
|
|
}
|