feat(canvas): 画布能力 - 完善 LibTV 导入与资源预览
This commit is contained in:
1 parent
4b9d510129
commit
d2a089f1ec
36 files changed
+1722
-68
No files matched your search
@@ -81,6 +81,7 @@ func main() {
|
||||
handler.RegisterAdminAnalyticsRoutes(api, svc)
|
||||
handler.RegisterAnnouncementRoutes(api, svc)
|
||||
handler.RegisterFinanceRoutes(api, svc)
|
||||
handler.RegisterLibTVRoutes(api, svc)
|
||||
// 登录态模型目录代理:避免浏览器直连各上游时分别处理 CORS。
|
||||
handler.RegisterChannelModelRoutes(api, svc)
|
||||
handler.RegisterLogicalModelRoutes(api, svc)
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"infinite-canvas/backend/internal/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func RegisterLibTVRoutes(r *gin.RouterGroup, svc *service.Service) {
|
||||
r.GET("/admin/settings/libtv", func(c *gin.Context) {
|
||||
user, err := currentUser(c, svc)
|
||||
if err != nil {
|
||||
failService(c, err)
|
||||
return
|
||||
}
|
||||
setting, err := svc.AdminLibTVSetting(user)
|
||||
if err != nil {
|
||||
failService(c, err)
|
||||
return
|
||||
}
|
||||
ok(c, gin.H{"setting": setting})
|
||||
})
|
||||
r.PATCH("/admin/settings/libtv", func(c *gin.Context) {
|
||||
user, err := currentUser(c, svc)
|
||||
if err != nil {
|
||||
failService(c, err)
|
||||
return
|
||||
}
|
||||
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, 16<<10)
|
||||
var req service.LibTVSettingRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
fail(c, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
setting, err := svc.UpdateLibTVSetting(user, req)
|
||||
if err != nil {
|
||||
failService(c, err)
|
||||
return
|
||||
}
|
||||
ok(c, gin.H{"setting": setting})
|
||||
})
|
||||
r.POST("/admin/settings/libtv/test", func(c *gin.Context) {
|
||||
user, err := currentUser(c, svc)
|
||||
if err != nil {
|
||||
failService(c, err)
|
||||
return
|
||||
}
|
||||
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, 16<<10)
|
||||
var req service.LibTVImportRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
fail(c, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
if err := svc.TestLibTV(user, strings.TrimSpace(req.UUID)); err != nil {
|
||||
failService(c, err)
|
||||
return
|
||||
}
|
||||
ok(c, gin.H{"ok": true})
|
||||
})
|
||||
r.POST("/canvas-projects/:id/import/libtv", func(c *gin.Context) {
|
||||
user, err := currentUser(c, svc)
|
||||
if err != nil {
|
||||
failService(c, err)
|
||||
return
|
||||
}
|
||||
if user == nil {
|
||||
failService(c, service.Unauthorized("请先登录"))
|
||||
return
|
||||
}
|
||||
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, 16<<10)
|
||||
var req service.LibTVImportRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
fail(c, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
result, err := svc.ImportLibTV(user.ID, c.Param("id"), strings.TrimSpace(req.UUID))
|
||||
if err != nil {
|
||||
failService(c, err)
|
||||
return
|
||||
}
|
||||
ok(c, result)
|
||||
})
|
||||
}
|
||||
@@ -12,7 +12,7 @@ type Resource struct {
|
||||
Provider string `json:"provider" gorm:"size:24"`
|
||||
Endpoint string `json:"endpoint"`
|
||||
Bucket string `json:"bucket" gorm:"size:160"`
|
||||
// 用户 OSS 每次修改都会生成新版本,资源固定引用创建时的存储与密钥;同一存储位置的 CDN 域名可跟随当前配置。
|
||||
// 用户 OSS 每次修改都会生成新版本,资源固定引用创建时的存储与密钥;只有同一存储位置才可复用当前 CDN。
|
||||
StorageSettingID string `json:"-" gorm:"index;size:36"`
|
||||
ObjectKey string `json:"objectKey" gorm:"index"`
|
||||
PublicURL string `json:"publicUrl"`
|
||||
|
||||
@@ -781,6 +781,12 @@ func (r *Repository) LatestUserOSSSetting(userID string) (*model.UserOSSSetting,
|
||||
return &setting, nil
|
||||
}
|
||||
|
||||
func (r *Repository) UserOSSSettingsForUser(userID string) ([]model.UserOSSSetting, error) {
|
||||
var settings []model.UserOSSSetting
|
||||
err := r.db.Where("user_id = ?", userID).Order("created_at desc, id desc").Find(&settings).Error
|
||||
return settings, err
|
||||
}
|
||||
|
||||
func (r *Repository) UserOSSSettingForUser(userID string, id string) (*model.UserOSSSetting, error) {
|
||||
var setting model.UserOSSSetting
|
||||
if err := r.db.First(&setting, "id = ? AND user_id = ?", id, userID).Error; err != nil {
|
||||
|
||||
@@ -0,0 +1,358 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"infinite-canvas/backend/internal/model"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const (
|
||||
libTVSettingKey = "libtv"
|
||||
libTVDetailURL = "https://api.liblib.tv/api/canvas/project/detail"
|
||||
libTVMaxResponseBytes = 8 << 20
|
||||
)
|
||||
|
||||
type LibTVSettingRequest struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
Token string `json:"token"`
|
||||
ClearToken bool `json:"clearToken"`
|
||||
}
|
||||
|
||||
type PublicLibTVSetting struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
HasToken bool `json:"hasToken"`
|
||||
UpdatedAt time.Time `json:"updatedAt,omitempty"`
|
||||
}
|
||||
|
||||
type libTVSettingValue struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
type LibTVImportRequest struct {
|
||||
UUID string `json:"uuid"`
|
||||
}
|
||||
|
||||
type LibTVImportResult struct {
|
||||
BatchID string `json:"batchId"`
|
||||
BatchCreatedAt time.Time `json:"batchCreatedAt"`
|
||||
ProjectUUID string `json:"projectUuid"`
|
||||
ProjectName string `json:"projectName"`
|
||||
Nodes []LibTVCanvasNode `json:"nodes"`
|
||||
Connections []LibTVCanvasConnection `json:"connections"`
|
||||
ImportedNodeCount int `json:"importedNodeCount"`
|
||||
ImportedConnectionCount int `json:"importedConnectionCount"`
|
||||
SkippedNodes []LibTVImportIssue `json:"skippedNodes"`
|
||||
SkippedConnections []LibTVImportIssue `json:"skippedConnections"`
|
||||
Warnings []LibTVImportWarning `json:"warnings"`
|
||||
MultiResultNodeCount int `json:"multiResultNodeCount"`
|
||||
StaleNodeCount int `json:"staleNodeCount"`
|
||||
ReusedFailedNodeCount int `json:"reusedFailedNodeCount"`
|
||||
PlaceholderNodeCount int `json:"placeholderNodeCount"`
|
||||
ConvertedSpecialCount int `json:"convertedSpecialCount"`
|
||||
}
|
||||
|
||||
type LibTVCanvasNode struct {
|
||||
ID string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
Title string `json:"title"`
|
||||
X float64 `json:"x"`
|
||||
Y float64 `json:"y"`
|
||||
Width float64 `json:"width"`
|
||||
Height float64 `json:"height"`
|
||||
Content string `json:"content"`
|
||||
Prompt string `json:"prompt,omitempty"`
|
||||
Model string `json:"model,omitempty"`
|
||||
NaturalWidth int `json:"naturalWidth,omitempty"`
|
||||
NaturalHeight int `json:"naturalHeight,omitempty"`
|
||||
DurationMs int64 `json:"durationMs,omitempty"`
|
||||
MimeType string `json:"mimeType,omitempty"`
|
||||
Status string `json:"status,omitempty"`
|
||||
ErrorDetails string `json:"errorDetails,omitempty"`
|
||||
Metadata LibTVImportMetadata `json:"metadata"`
|
||||
}
|
||||
|
||||
type LibTVImportMetadata struct {
|
||||
Provider string `json:"provider"`
|
||||
ProjectUUID string `json:"projectUuid"`
|
||||
NodeKey string `json:"nodeKey"`
|
||||
BatchID string `json:"batchId"`
|
||||
SourceType string `json:"sourceType,omitempty"`
|
||||
StyleAssetUUID string `json:"styleAssetUuid,omitempty"`
|
||||
StyleVersionUUID string `json:"styleVersionUuid,omitempty"`
|
||||
StyleName string `json:"styleName,omitempty"`
|
||||
}
|
||||
|
||||
type LibTVCanvasConnection struct {
|
||||
ID string `json:"id"`
|
||||
FromNodeID string `json:"fromNodeId"`
|
||||
ToNodeID string `json:"toNodeId"`
|
||||
}
|
||||
|
||||
type LibTVImportIssue struct {
|
||||
ID string `json:"id,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
Reason string `json:"reason"`
|
||||
}
|
||||
|
||||
type LibTVImportWarning struct {
|
||||
ID string `json:"id,omitempty"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
type libTVEnvelope struct {
|
||||
Code int `json:"code"`
|
||||
Data json.RawMessage `json:"data"`
|
||||
Msg string `json:"msg"`
|
||||
}
|
||||
|
||||
type libTVDetail struct {
|
||||
ProjectMeta struct {
|
||||
UUID string `json:"uuid"`
|
||||
Name string `json:"name"`
|
||||
Effective struct {
|
||||
CanRead bool `json:"canRead"`
|
||||
CanCopy bool `json:"canCopy"`
|
||||
} `json:"effective"`
|
||||
} `json:"projectMeta"`
|
||||
NodeList []libTVRawNode `json:"nodeList"`
|
||||
ConnectionList []libTVRawConnection `json:"connectionList"`
|
||||
}
|
||||
|
||||
type libTVRawNode struct {
|
||||
NodeKey string `json:"nodeKey"`
|
||||
Name string `json:"name"`
|
||||
Data string `json:"data"`
|
||||
Position struct {
|
||||
X string `json:"positionX"`
|
||||
Y string `json:"positionY"`
|
||||
} `json:"position"`
|
||||
Measured struct {
|
||||
Width string `json:"width"`
|
||||
Height string `json:"height"`
|
||||
} `json:"measured"`
|
||||
TaskInfo struct {
|
||||
Status int `json:"status"`
|
||||
FailedReason string `json:"failedReason"`
|
||||
} `json:"taskInfo"`
|
||||
IsStale bool `json:"isStale"`
|
||||
}
|
||||
|
||||
type libTVRawConnection struct {
|
||||
ConnectionID string `json:"connectionId"`
|
||||
Source string `json:"source"`
|
||||
Target string `json:"target"`
|
||||
}
|
||||
|
||||
type libTVNodeData struct {
|
||||
Type string `json:"type"`
|
||||
URL []string `json:"url"`
|
||||
CoverURL string `json:"coverUrl"`
|
||||
StyleAssetUUID string `json:"styleAssetUuid"`
|
||||
StyleVersionUUID string `json:"styleVersionUuid"`
|
||||
StyleName string `json:"styleName"`
|
||||
Params map[string]any `json:"params"`
|
||||
TaskInfo struct {
|
||||
Status int `json:"status"`
|
||||
FailedReason string `json:"failedReason"`
|
||||
} `json:"taskInfo"`
|
||||
IsStale bool `json:"isStale"`
|
||||
ResourceMeta struct {
|
||||
Items []struct {
|
||||
Width int `json:"width"`
|
||||
Height int `json:"height"`
|
||||
DurationSec float64 `json:"durationSec"`
|
||||
} `json:"items"`
|
||||
} `json:"_resourceMeta"`
|
||||
}
|
||||
|
||||
func (s *Service) AdminLibTVSetting(actor *model.User) (*PublicLibTVSetting, error) {
|
||||
if err := s.RequireAdmin(actor); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
setting, value, err := s.readLibTVSetting()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return publicLibTVSetting(setting, value), nil
|
||||
}
|
||||
|
||||
func (s *Service) UpdateLibTVSetting(actor *model.User, req LibTVSettingRequest) (*PublicLibTVSetting, error) {
|
||||
if err := s.RequireAdmin(actor); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, current, err := s.readLibTVSetting()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
token := strings.TrimSpace(req.Token)
|
||||
if req.ClearToken {
|
||||
token = ""
|
||||
} else if token == "" {
|
||||
token = current.Token
|
||||
}
|
||||
if req.Enabled && token == "" {
|
||||
return nil, BadAuthRequest("启用 LibTV 前请先配置 Token")
|
||||
}
|
||||
protected, err := s.encryptSettingSecret(token)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
valueJSON, err := json.Marshal(libTVSettingValue{Enabled: req.Enabled, Token: protected})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
setting := &model.SystemSetting{Key: libTVSettingKey, ValueJSON: string(valueJSON), UpdatedBy: actor.ID}
|
||||
if existing, lookupErr := s.repo.SystemSetting(libTVSettingKey); lookupErr == nil {
|
||||
setting.CreatedAt = existing.CreatedAt
|
||||
} else if !errors.Is(lookupErr, gorm.ErrRecordNotFound) {
|
||||
return nil, lookupErr
|
||||
}
|
||||
if err := s.repo.SaveSystemSetting(setting); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return publicLibTVSetting(setting, libTVSettingValue{Enabled: req.Enabled, Token: token}), nil
|
||||
}
|
||||
|
||||
func (s *Service) TestLibTV(actor *model.User, projectUUID string) error {
|
||||
if actor == nil {
|
||||
return Unauthorized("请先登录")
|
||||
}
|
||||
if err := s.RequireAdmin(actor); err != nil {
|
||||
return err
|
||||
}
|
||||
_, value, err := s.readLibTVSetting()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if value.Token == "" {
|
||||
return BadAuthRequest("尚未配置 LibTV Token")
|
||||
}
|
||||
_, err = s.fetchLibTVDetail(strings.TrimSpace(projectUUID), value.Token)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Service) ImportLibTV(userID, canvasProjectID, projectUUID string) (*LibTVImportResult, error) {
|
||||
userID = strings.TrimSpace(userID)
|
||||
canvasProjectID = strings.TrimSpace(canvasProjectID)
|
||||
if userID == "" || canvasProjectID == "" {
|
||||
return nil, Unauthorized("请先打开已同步的影策画布")
|
||||
}
|
||||
if _, err := s.repo.CanvasProjectForUser(userID, canvasProjectID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, value, err := s.readLibTVSetting()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !value.Enabled || value.Token == "" {
|
||||
return nil, BadAuthRequest("LibTV 尚未启用或未配置 Token")
|
||||
}
|
||||
detail, err := s.fetchLibTVDetail(strings.TrimSpace(projectUUID), value.Token)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return adaptLibTVDetail(detail)
|
||||
}
|
||||
|
||||
func (s *Service) readLibTVSetting() (*model.SystemSetting, libTVSettingValue, error) {
|
||||
setting, err := s.repo.SystemSetting(libTVSettingKey)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, libTVSettingValue{}, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, libTVSettingValue{}, err
|
||||
}
|
||||
value := libTVSettingValue{}
|
||||
if err := json.Unmarshal([]byte(setting.ValueJSON), &value); err != nil {
|
||||
return nil, value, errors.New("LibTV 配置格式无效")
|
||||
}
|
||||
if value.Token != "" {
|
||||
plain, err := s.decryptSettingSecret(value.Token)
|
||||
if err != nil {
|
||||
return nil, value, err
|
||||
}
|
||||
value.Token = plain
|
||||
}
|
||||
return setting, value, nil
|
||||
}
|
||||
|
||||
func publicLibTVSetting(setting *model.SystemSetting, value libTVSettingValue) *PublicLibTVSetting {
|
||||
result := &PublicLibTVSetting{Enabled: value.Enabled, HasToken: strings.TrimSpace(value.Token) != ""}
|
||||
if setting != nil {
|
||||
result.UpdatedAt = setting.UpdatedAt
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (s *Service) fetchLibTVDetail(projectUUID, token string) (*libTVDetail, error) {
|
||||
projectUUID = strings.TrimSpace(projectUUID)
|
||||
if !isLibTVProjectUUID(projectUUID) {
|
||||
return nil, BadAuthRequest("LibTV 画布 UUID 格式无效")
|
||||
}
|
||||
u, err := url.Parse(libTVDetailURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
query := u.Query()
|
||||
query.Set("uuid", projectUUID)
|
||||
u.RawQuery = query.Encode()
|
||||
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Accept", "application/json")
|
||||
req.Header.Set("token", token)
|
||||
ApplyDefaultOutboundHeaders(req)
|
||||
client := OutboundHTTPClient(20 * time.Second)
|
||||
// Token 是 LibTV 专用凭证,禁止重定向以避免自定义请求头被带到其他主机。
|
||||
client.CheckRedirect = func(_ *http.Request, _ []*http.Request) error {
|
||||
return errors.New("LibTV API 不允许重定向")
|
||||
}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, errors.New("LibTV 请求失败,请检查网络或 Token")
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err := io.ReadAll(io.LimitReader(resp.Body, libTVMaxResponseBytes+1))
|
||||
if err != nil {
|
||||
return nil, errors.New("读取 LibTV 响应失败")
|
||||
}
|
||||
if len(body) > libTVMaxResponseBytes {
|
||||
return nil, errors.New("LibTV 响应过大")
|
||||
}
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
return nil, fmt.Errorf("LibTV 请求失败(HTTP %d)", resp.StatusCode)
|
||||
}
|
||||
var envelope libTVEnvelope
|
||||
if err := json.Unmarshal(body, &envelope); err != nil {
|
||||
return nil, errors.New("LibTV 响应格式无效")
|
||||
}
|
||||
if envelope.Code != 0 {
|
||||
msg := strings.TrimSpace(envelope.Msg)
|
||||
if msg == "" {
|
||||
msg = "LibTV 返回业务错误"
|
||||
}
|
||||
return nil, errors.New(msg)
|
||||
}
|
||||
var detail libTVDetail
|
||||
if err := json.Unmarshal(envelope.Data, &detail); err != nil {
|
||||
return nil, errors.New("LibTV 画布数据格式无效")
|
||||
}
|
||||
if strings.TrimSpace(detail.ProjectMeta.UUID) == "" {
|
||||
detail.ProjectMeta.UUID = projectUUID
|
||||
}
|
||||
if !detail.ProjectMeta.Effective.CanRead || !detail.ProjectMeta.Effective.CanCopy {
|
||||
return nil, errors.New("当前 LibTV 画布不允许复制")
|
||||
}
|
||||
return &detail, nil
|
||||
}
|
||||
@@ -0,0 +1,236 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func adaptLibTVDetail(detail *libTVDetail) (*LibTVImportResult, error) {
|
||||
if detail == nil {
|
||||
return nil, errors.New("LibTV 画布数据为空")
|
||||
}
|
||||
now := time.Now()
|
||||
batchID := strconv.FormatInt(now.UnixMilli(), 36) + "-" + strings.ReplaceAll(uuid.NewString(), "-", "")[:8]
|
||||
result := &LibTVImportResult{
|
||||
BatchID: batchID,
|
||||
BatchCreatedAt: now,
|
||||
ProjectUUID: detail.ProjectMeta.UUID,
|
||||
ProjectName: detail.ProjectMeta.Name,
|
||||
Nodes: make([]LibTVCanvasNode, 0),
|
||||
Connections: make([]LibTVCanvasConnection, 0),
|
||||
SkippedNodes: make([]LibTVImportIssue, 0),
|
||||
SkippedConnections: make([]LibTVImportIssue, 0),
|
||||
Warnings: make([]LibTVImportWarning, 0),
|
||||
}
|
||||
mapping := make(map[string]string, len(detail.NodeList))
|
||||
seenNodeKeys := make(map[string]struct{}, len(detail.NodeList))
|
||||
for _, raw := range detail.NodeList {
|
||||
nodeKey := strings.TrimSpace(raw.NodeKey)
|
||||
if nodeKey == "" {
|
||||
result.SkippedNodes = append(result.SkippedNodes, LibTVImportIssue{Name: raw.Name, Reason: "节点缺少 nodeKey"})
|
||||
continue
|
||||
}
|
||||
if _, exists := seenNodeKeys[nodeKey]; exists {
|
||||
result.SkippedNodes = append(result.SkippedNodes, LibTVImportIssue{ID: nodeKey, Name: raw.Name, Reason: "节点 nodeKey 重复"})
|
||||
continue
|
||||
}
|
||||
seenNodeKeys[nodeKey] = struct{}{}
|
||||
|
||||
var data libTVNodeData
|
||||
if err := json.Unmarshal([]byte(raw.Data), &data); err != nil {
|
||||
result.SkippedNodes = append(result.SkippedNodes, LibTVImportIssue{ID: nodeKey, Name: raw.Name, Reason: "节点数据格式无效"})
|
||||
continue
|
||||
}
|
||||
taskStatus := data.TaskInfo.Status
|
||||
if taskStatus == 0 && raw.TaskInfo.Status != 0 {
|
||||
taskStatus = raw.TaskInfo.Status
|
||||
}
|
||||
sourceType := strings.ToLower(strings.TrimSpace(data.Type))
|
||||
kind := sourceType
|
||||
if sourceType == "material-style" {
|
||||
// 风格素材没有普通生成 URL,但有可复用封面时可以降级为影策图片参考节点。
|
||||
kind = "image"
|
||||
} else if kind != "image" && kind != "video" {
|
||||
result.SkippedNodes = append(result.SkippedNodes, LibTVImportIssue{ID: nodeKey, Name: raw.Name, Reason: "暂不支持的节点类型"})
|
||||
continue
|
||||
}
|
||||
mediaURL, mediaIndex := firstLibTVMediaURL(data.URL)
|
||||
if mediaURL == "" && sourceType == "material-style" {
|
||||
mediaURL, mediaIndex = firstLibTVMediaURL([]string{data.CoverURL})
|
||||
}
|
||||
|
||||
x, okX := finiteFloat(raw.Position.X)
|
||||
y, okY := finiteFloat(raw.Position.Y)
|
||||
if !okX || !okY {
|
||||
result.SkippedNodes = append(result.SkippedNodes, LibTVImportIssue{ID: nodeKey, Name: raw.Name, Reason: "节点坐标无效"})
|
||||
continue
|
||||
}
|
||||
width, okWidth := positiveFiniteFloat(raw.Measured.Width)
|
||||
height, okHeight := positiveFiniteFloat(raw.Measured.Height)
|
||||
if !okWidth {
|
||||
width = 480
|
||||
}
|
||||
if !okHeight {
|
||||
height = 300
|
||||
}
|
||||
item := LibTVCanvasNode{
|
||||
ID: "libtv-" + batchID + "-" + nodeKey,
|
||||
Type: kind,
|
||||
Title: strings.TrimSpace(raw.Name),
|
||||
X: x,
|
||||
Y: y,
|
||||
Width: width,
|
||||
Height: height,
|
||||
Content: mediaURL,
|
||||
Prompt: mapString(data.Params, "prompt"),
|
||||
Model: mapString(data.Params, "model"),
|
||||
Metadata: LibTVImportMetadata{
|
||||
Provider: "libtv",
|
||||
ProjectUUID: result.ProjectUUID,
|
||||
NodeKey: nodeKey,
|
||||
BatchID: batchID,
|
||||
SourceType: sourceType,
|
||||
StyleAssetUUID: strings.TrimSpace(data.StyleAssetUUID),
|
||||
StyleVersionUUID: strings.TrimSpace(data.StyleVersionUUID),
|
||||
StyleName: strings.TrimSpace(data.StyleName),
|
||||
},
|
||||
}
|
||||
if item.Title == "" {
|
||||
item.Title = kind + " 节点"
|
||||
}
|
||||
if mediaIndex >= 0 && mediaIndex < len(data.ResourceMeta.Items) {
|
||||
resource := data.ResourceMeta.Items[mediaIndex]
|
||||
item.NaturalWidth = resource.Width
|
||||
item.NaturalHeight = resource.Height
|
||||
if resource.DurationSec > 0 && !math.IsNaN(resource.DurationSec) && !math.IsInf(resource.DurationSec, 0) {
|
||||
item.DurationMs = int64(math.Round(resource.DurationSec * 1000))
|
||||
}
|
||||
}
|
||||
item.MimeType = inferMediaMime(kind, mediaURL)
|
||||
if mediaURL == "" {
|
||||
result.PlaceholderNodeCount++
|
||||
if taskStatus == 3 {
|
||||
// 没有历史结果的失败节点仍保留拓扑,并以失败占位状态呈现。
|
||||
item.Status = "error"
|
||||
item.ErrorDetails = firstNonEmptyString(data.TaskInfo.FailedReason, raw.TaskInfo.FailedReason, "LibTV 生成任务失败")
|
||||
} else {
|
||||
item.Status = "idle"
|
||||
}
|
||||
} else {
|
||||
item.Status = "success"
|
||||
if taskStatus == 3 {
|
||||
// 最近一次任务失败不应覆盖节点中仍可用的历史资源。
|
||||
result.ReusedFailedNodeCount++
|
||||
}
|
||||
}
|
||||
mapping[nodeKey] = item.ID
|
||||
result.Nodes = append(result.Nodes, item)
|
||||
if sourceType == "material-style" {
|
||||
result.ConvertedSpecialCount++
|
||||
}
|
||||
if !okWidth || !okHeight {
|
||||
result.Warnings = append(result.Warnings, LibTVImportWarning{ID: nodeKey, Message: fmt.Sprintf("节点“%s”尺寸无效,已使用默认尺寸。", item.Title)})
|
||||
}
|
||||
if len(data.URL) > 1 && mediaURL != "" {
|
||||
result.MultiResultNodeCount++
|
||||
result.Warnings = append(result.Warnings, LibTVImportWarning{ID: nodeKey, Message: fmt.Sprintf("节点“%s”包含 %d 个结果,已导入第 %d 个。", item.Title, len(data.URL), mediaIndex+1)})
|
||||
}
|
||||
if data.IsStale || raw.IsStale {
|
||||
result.StaleNodeCount++
|
||||
result.Warnings = append(result.Warnings, LibTVImportWarning{ID: nodeKey, Message: fmt.Sprintf("节点“%s”已标记为过期,但现有资源仍已导入。", item.Title)})
|
||||
}
|
||||
}
|
||||
|
||||
seenConnectionIDs := make(map[string]struct{}, len(detail.ConnectionList))
|
||||
for _, raw := range detail.ConnectionList {
|
||||
connectionID := strings.TrimSpace(raw.ConnectionID)
|
||||
if connectionID == "" {
|
||||
result.SkippedConnections = append(result.SkippedConnections, LibTVImportIssue{Reason: "连接缺少 connectionId"})
|
||||
continue
|
||||
}
|
||||
if _, exists := seenConnectionIDs[connectionID]; exists {
|
||||
result.SkippedConnections = append(result.SkippedConnections, LibTVImportIssue{ID: connectionID, Reason: "连接 connectionId 重复"})
|
||||
continue
|
||||
}
|
||||
seenConnectionIDs[connectionID] = struct{}{}
|
||||
from, okFrom := mapping[strings.TrimSpace(raw.Source)]
|
||||
to, okTo := mapping[strings.TrimSpace(raw.Target)]
|
||||
if !okFrom || !okTo {
|
||||
result.SkippedConnections = append(result.SkippedConnections, LibTVImportIssue{ID: connectionID, Reason: "连接端点节点未导入"})
|
||||
continue
|
||||
}
|
||||
result.Connections = append(result.Connections, LibTVCanvasConnection{ID: "libtv-" + batchID + "-" + connectionID, FromNodeID: from, ToNodeID: to})
|
||||
}
|
||||
result.ImportedNodeCount = len(result.Nodes)
|
||||
result.ImportedConnectionCount = len(result.Connections)
|
||||
if result.ImportedNodeCount == 0 {
|
||||
return nil, errors.New("LibTV 画布没有可导入的有效节点")
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func mapString(values map[string]any, key string) string {
|
||||
value, _ := values[key].(string)
|
||||
return strings.TrimSpace(value)
|
||||
}
|
||||
|
||||
func isLibTVProjectUUID(value string) bool {
|
||||
if len(value) != 32 {
|
||||
return false
|
||||
}
|
||||
for _, char := range value {
|
||||
if !((char >= '0' && char <= '9') || (char >= 'a' && char <= 'f') || (char >= 'A' && char <= 'F')) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func firstLibTVMediaURL(values []string) (string, int) {
|
||||
for index, value := range values {
|
||||
parsed, err := url.Parse(strings.TrimSpace(value))
|
||||
if err != nil || parsed.Scheme != "https" || parsed.Hostname() == "" || parsed.User != nil {
|
||||
continue
|
||||
}
|
||||
return parsed.String(), index
|
||||
}
|
||||
return "", -1
|
||||
}
|
||||
|
||||
func finiteFloat(value string) (float64, bool) {
|
||||
parsed, err := strconv.ParseFloat(strings.TrimSpace(value), 64)
|
||||
if err != nil || math.IsNaN(parsed) || math.IsInf(parsed, 0) {
|
||||
return 0, false
|
||||
}
|
||||
return parsed, true
|
||||
}
|
||||
|
||||
func positiveFiniteFloat(value string) (float64, bool) {
|
||||
parsed, ok := finiteFloat(value)
|
||||
return parsed, ok && parsed > 0
|
||||
}
|
||||
|
||||
func inferMediaMime(kind, rawURL string) string {
|
||||
lower := strings.ToLower(rawURL)
|
||||
if strings.Contains(lower, ".png") {
|
||||
return "image/png"
|
||||
}
|
||||
if strings.Contains(lower, ".jpg") || strings.Contains(lower, ".jpeg") {
|
||||
return "image/jpeg"
|
||||
}
|
||||
if strings.Contains(lower, ".webp") {
|
||||
return "image/webp"
|
||||
}
|
||||
if kind == "video" {
|
||||
return "video/mp4"
|
||||
}
|
||||
return "image/*"
|
||||
}
|
||||
@@ -0,0 +1,201 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAdaptLibTVDetailSelectsFirstMediaAndReportsPartialImport(t *testing.T) {
|
||||
detail := &libTVDetail{}
|
||||
detail.ProjectMeta.UUID = "project-uuid"
|
||||
detail.ProjectMeta.Name = "测试画布"
|
||||
detail.NodeList = []libTVRawNode{
|
||||
{
|
||||
NodeKey: "image-1",
|
||||
Name: "多图节点",
|
||||
Data: `{"type":"image","url":["https://example.com/first.png","https://example.com/second.png"],"params":{"prompt":"prompt","model":"model"},"_resourceMeta":{"items":[{"width":1024,"height":768}]}}`,
|
||||
Position: struct {
|
||||
X string `json:"positionX"`
|
||||
Y string `json:"positionY"`
|
||||
}{X: "12", Y: "34"},
|
||||
Measured: struct {
|
||||
Width string `json:"width"`
|
||||
Height string `json:"height"`
|
||||
}{Width: "640", Height: "360"},
|
||||
},
|
||||
{
|
||||
NodeKey: "failed-video",
|
||||
Name: "失败视频",
|
||||
Data: `{"type":"video","url":[],"taskInfo":{"status":3,"failedReason":"生成失败"}}`,
|
||||
Position: struct {
|
||||
X string `json:"positionX"`
|
||||
Y string `json:"positionY"`
|
||||
}{X: "500", Y: "600"},
|
||||
},
|
||||
{
|
||||
NodeKey: "stale-video",
|
||||
Name: "过期视频",
|
||||
Data: `{"type":"video","url":["https://example.com/video.mp4"],"taskInfo":{"status":2},"isStale":true,"_resourceMeta":{"items":[{"width":1920,"height":1080,"durationSec":5.2506}]}}`,
|
||||
Position: struct {
|
||||
X string `json:"positionX"`
|
||||
Y string `json:"positionY"`
|
||||
}{X: "900", Y: "1200"},
|
||||
Measured: struct {
|
||||
Width string `json:"width"`
|
||||
Height string `json:"height"`
|
||||
}{Width: "640", Height: "360"},
|
||||
},
|
||||
}
|
||||
detail.ConnectionList = []libTVRawConnection{
|
||||
{ConnectionID: "valid", Source: "image-1", Target: "stale-video"},
|
||||
{ConnectionID: "dangling", Source: "image-1", Target: "failed-video"},
|
||||
}
|
||||
|
||||
result, err := adaptLibTVDetail(detail)
|
||||
if err != nil {
|
||||
t.Fatalf("adaptLibTVDetail() error = %v", err)
|
||||
}
|
||||
if result.ImportedNodeCount != 3 || result.ImportedConnectionCount != 2 {
|
||||
t.Fatalf("counts = (%d, %d), want (3, 2)", result.ImportedNodeCount, result.ImportedConnectionCount)
|
||||
}
|
||||
if len(result.SkippedNodes) != 0 || len(result.SkippedConnections) != 0 {
|
||||
t.Fatalf("unexpected skipped items: nodes=%#v connections=%#v", result.SkippedNodes, result.SkippedConnections)
|
||||
}
|
||||
if result.PlaceholderNodeCount != 1 || result.ReusedFailedNodeCount != 0 {
|
||||
t.Fatalf("failure handling counts = (placeholder=%d, reused=%d), want (1, 0)", result.PlaceholderNodeCount, result.ReusedFailedNodeCount)
|
||||
}
|
||||
if result.BatchCreatedAt.IsZero() {
|
||||
t.Fatal("batch created time must not be zero")
|
||||
}
|
||||
if got := result.Nodes[0]; got.Content != "https://example.com/first.png" || got.X != 12 || got.Y != 34 || got.Width != 640 || got.Height != 360 {
|
||||
t.Fatalf("mapped image = %#v", got)
|
||||
}
|
||||
if got := result.Nodes[0]; got.Prompt != "prompt" || got.Model != "model" || got.NaturalWidth != 1024 || got.NaturalHeight != 768 {
|
||||
t.Fatalf("mapped image metadata = %#v", got)
|
||||
}
|
||||
if got := result.Nodes[1]; got.DurationMs != 0 || got.MimeType != "video/mp4" || got.Status != "error" || got.ErrorDetails != "生成失败" {
|
||||
t.Fatalf("mapped video metadata = %#v", got)
|
||||
}
|
||||
if got := result.Nodes[0].Metadata.Provider; got != "libtv" {
|
||||
t.Fatalf("metadata provider = %q, want libtv", got)
|
||||
}
|
||||
if len(result.Warnings) != 3 {
|
||||
t.Fatalf("warnings = %#v", result.Warnings)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdaptLibTVDetailKeepsFailedNodeWithHistoricalMedia(t *testing.T) {
|
||||
detail := &libTVDetail{NodeList: []libTVRawNode{{
|
||||
NodeKey: "failed-image",
|
||||
Data: `{"type":"image","url":["https://example.com/history.png"],"taskInfo":{"status":3,"failedReason":"本次生成失败"}}`,
|
||||
Position: struct {
|
||||
X string `json:"positionX"`
|
||||
Y string `json:"positionY"`
|
||||
}{X: "10", Y: "20"},
|
||||
}}}
|
||||
|
||||
result, err := adaptLibTVDetail(detail)
|
||||
if err != nil {
|
||||
t.Fatalf("adaptLibTVDetail() error = %v", err)
|
||||
}
|
||||
if len(result.Nodes) != 1 || result.Nodes[0].Content != "https://example.com/history.png" {
|
||||
t.Fatalf("historical media node = %#v", result.Nodes)
|
||||
}
|
||||
if result.Nodes[0].Status != "success" || result.ReusedFailedNodeCount != 1 {
|
||||
t.Fatalf("failure reuse = node=%#v count=%d", result.Nodes[0], result.ReusedFailedNodeCount)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdaptLibTVDetailConvertsMaterialStyleToImage(t *testing.T) {
|
||||
detail := &libTVDetail{NodeList: []libTVRawNode{{
|
||||
NodeKey: "style-1",
|
||||
Name: "风格素材",
|
||||
Data: `{"type":"material-style","coverUrl":"https://example.com/style.png","styleAssetUuid":"asset-1","styleVersionUuid":"version-1","styleName":"电影感"}`,
|
||||
Position: struct {
|
||||
X string `json:"positionX"`
|
||||
Y string `json:"positionY"`
|
||||
}{X: "1", Y: "2"},
|
||||
}}}
|
||||
|
||||
result, err := adaptLibTVDetail(detail)
|
||||
if err != nil {
|
||||
t.Fatalf("adaptLibTVDetail() error = %v", err)
|
||||
}
|
||||
if len(result.Nodes) != 1 {
|
||||
t.Fatalf("nodes = %#v", result.Nodes)
|
||||
}
|
||||
node := result.Nodes[0]
|
||||
if node.Type != "image" || node.Content != "https://example.com/style.png" || result.ConvertedSpecialCount != 1 {
|
||||
t.Fatalf("converted style node = %#v count=%d", node, result.ConvertedSpecialCount)
|
||||
}
|
||||
if node.Metadata.StyleAssetUUID != "asset-1" || node.Metadata.StyleVersionUUID != "version-1" || node.Metadata.StyleName != "电影感" {
|
||||
t.Fatalf("style metadata = %#v", node.Metadata)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdaptLibTVDetailGeneratesDistinctBatchIDs(t *testing.T) {
|
||||
detail := &libTVDetail{}
|
||||
detail.ProjectMeta.UUID = "project-uuid"
|
||||
detail.NodeList = []libTVRawNode{{
|
||||
NodeKey: "image-1",
|
||||
Data: `{"type":"image","url":["https://example.com/image.png"]}`,
|
||||
Position: struct {
|
||||
X string `json:"positionX"`
|
||||
Y string `json:"positionY"`
|
||||
}{X: "0", Y: "0"},
|
||||
}}
|
||||
|
||||
first, err := adaptLibTVDetail(detail)
|
||||
if err != nil {
|
||||
t.Fatalf("first adaptLibTVDetail() error = %v", err)
|
||||
}
|
||||
second, err := adaptLibTVDetail(detail)
|
||||
if err != nil {
|
||||
t.Fatalf("second adaptLibTVDetail() error = %v", err)
|
||||
}
|
||||
if first.BatchID == second.BatchID {
|
||||
t.Fatalf("batch IDs are not unique: %q", first.BatchID)
|
||||
}
|
||||
if !strings.HasPrefix(first.Nodes[0].ID, "libtv-"+first.BatchID+"-") {
|
||||
t.Fatalf("node ID %q does not contain batch ID %q", first.Nodes[0].ID, first.BatchID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdaptLibTVDetailRejectsCanvasWithoutImportableNodes(t *testing.T) {
|
||||
detail := &libTVDetail{NodeList: []libTVRawNode{{
|
||||
NodeKey: "unsupported",
|
||||
Data: `{"type":"text","url":[]}`,
|
||||
}}}
|
||||
|
||||
if _, err := adaptLibTVDetail(detail); err == nil {
|
||||
t.Fatal("adaptLibTVDetail() error = nil, want an error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdaptLibTVDetailKeepsReportListsNonNilAndResourceIndex(t *testing.T) {
|
||||
detail := &libTVDetail{}
|
||||
detail.ProjectMeta.UUID = "project-uuid"
|
||||
detail.NodeList = []libTVRawNode{{
|
||||
NodeKey: "image-1",
|
||||
Data: `{"type":"image","url":["http://example.com/rejected.png","https://example.com/selected.png"],"_resourceMeta":{"items":[{"width":100,"height":100},{"width":200,"height":300}]}}`,
|
||||
Position: struct {
|
||||
X string `json:"positionX"`
|
||||
Y string `json:"positionY"`
|
||||
}{X: "10", Y: "20"},
|
||||
Measured: struct {
|
||||
Width string `json:"width"`
|
||||
Height string `json:"height"`
|
||||
}{Width: "200", Height: "300"},
|
||||
}}
|
||||
|
||||
result, err := adaptLibTVDetail(detail)
|
||||
if err != nil {
|
||||
t.Fatalf("adaptLibTVDetail() error = %v", err)
|
||||
}
|
||||
if result.SkippedNodes == nil || result.SkippedConnections == nil || result.Warnings == nil {
|
||||
t.Fatalf("report slices must not be nil: %#v", result)
|
||||
}
|
||||
if got := result.Nodes[0]; got.Content != "https://example.com/selected.png" || got.NaturalWidth != 200 || got.NaturalHeight != 300 {
|
||||
t.Fatalf("selected media metadata = %#v", got)
|
||||
}
|
||||
}
|
||||
@@ -695,29 +695,58 @@ func (s *Service) ossSettingForResource(userID string, resource *model.Resource)
|
||||
if currentErr != nil {
|
||||
return ossSettingValue{}, currentErr
|
||||
}
|
||||
// 密钥继续固定在历史版本;同一存储位置的 CDN 域名跟随当前配置,使已有资源也立即切换。
|
||||
// 密钥固定在资源绑定的历史版本;只有存储位置完全一致时,才允许沿用当前 CDN。
|
||||
if resourceStorageMatches(current, resource) {
|
||||
setting.CDNBaseURL = current.CDNBaseURL
|
||||
}
|
||||
}
|
||||
} else {
|
||||
_, setting, err = s.readOSSSetting()
|
||||
// 早期资源没有 StorageSettingID,但资源本身仍记录了 provider/endpoint/bucket。
|
||||
// 先从用户 OSS 历史版本中按存储位置反查,不能把当前七牛配置猜给历史阿里云对象。
|
||||
setting, err = s.userOSSSettingForResource(userID, resource)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
_, setting, err = s.readOSSSetting()
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return ossSettingValue{}, err
|
||||
}
|
||||
resourceProvider := strings.ToLower(strings.TrimSpace(resource.Provider))
|
||||
resourceMatchesSetting := resourceStorageMatches(setting, resource)
|
||||
setting, err = ossSettingForProvider(setting, firstNonEmpty(resource.Provider, setting.Provider))
|
||||
if err != nil {
|
||||
return ossSettingValue{}, err
|
||||
}
|
||||
setting.Endpoint = firstNonEmpty(resource.Endpoint, setting.Endpoint)
|
||||
setting.Bucket = firstNonEmpty(resource.Bucket, setting.Bucket)
|
||||
// CDN 是具体存储位置的出口,不得在 provider/endpoint/bucket 不匹配时继续沿用,
|
||||
// 否则切换到七牛后会把历史阿里云 objectKey 拼成七牛域名。
|
||||
if !resourceMatchesSetting || (resourceProvider != "" && resourceProvider != setting.Provider) {
|
||||
setting.CDNBaseURL = ""
|
||||
}
|
||||
if setting.AccessKeyID == "" || setting.AccessKeySecret == "" {
|
||||
return ossSettingValue{}, errors.New("对象存储访问密钥不可用")
|
||||
}
|
||||
return setting, nil
|
||||
}
|
||||
|
||||
func (s *Service) userOSSSettingForResource(userID string, resource *model.Resource) (ossSettingValue, error) {
|
||||
settings, err := s.repo.UserOSSSettingsForUser(userID)
|
||||
if err != nil {
|
||||
return ossSettingValue{}, err
|
||||
}
|
||||
for index := range settings {
|
||||
value, valueErr := s.userOSSSettingValue(&settings[index])
|
||||
if valueErr != nil {
|
||||
return ossSettingValue{}, valueErr
|
||||
}
|
||||
if resourceStorageMatches(value, resource) {
|
||||
return value, nil
|
||||
}
|
||||
}
|
||||
return ossSettingValue{}, gorm.ErrRecordNotFound
|
||||
}
|
||||
|
||||
func resourceStorageMatches(setting ossSettingValue, resource *model.Resource) bool {
|
||||
if resource == nil {
|
||||
return false
|
||||
|
||||
@@ -261,7 +261,7 @@ func TestTencentCOSSettingDerivesEndpointAndDoesNotReuseAliyunSecret(t *testing.
|
||||
_, err := ossSettingFromRequest(OSSSettingRequest{
|
||||
Enabled: true, Provider: tencentCOSProvider, Endpoint: server.URL, Bucket: "private-bucket-1250000000", AccessKeyID: "secret-id",
|
||||
}, ossSettingValue{Provider: aliyunOSSProvider, AccessKeySecret: "aliyun-secret"})
|
||||
if err == nil || !strings.Contains(err.Error(), "AccessKey Secret") {
|
||||
if err == nil || !strings.Contains(err.Error(), "访问密钥 SecretKey") {
|
||||
t.Fatalf("ossSettingFromRequest() error = %v", err)
|
||||
}
|
||||
next, err := ossSettingFromRequest(OSSSettingRequest{
|
||||
@@ -449,6 +449,101 @@ func TestCurrentUserCDNSettingAppliesToHistoricalResourcesInSameStorage(t *testi
|
||||
}
|
||||
}
|
||||
|
||||
func TestBoundHistoricalUserResourceDoesNotFollowCurrentProviderCDN(t *testing.T) {
|
||||
t.Setenv("CANVAS_ALLOW_PRIVATE_UPSTREAMS", "true")
|
||||
newTestServer := func() *httptest.Server {
|
||||
return httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}))
|
||||
}
|
||||
aliyunEndpoint := newTestServer()
|
||||
aliyunCDN := newTestServer()
|
||||
qiniuEndpoint := newTestServer()
|
||||
qiniuCDN := newTestServer()
|
||||
defer aliyunEndpoint.Close()
|
||||
defer aliyunCDN.Close()
|
||||
defer qiniuEndpoint.Close()
|
||||
defer qiniuCDN.Close()
|
||||
|
||||
svc := newResourceTestService(t)
|
||||
actor := &model.User{ID: "user-1"}
|
||||
if _, err := svc.UpdateUserOSSSetting(actor, OSSSettingRequest{
|
||||
Enabled: true, Provider: aliyunOSSProvider, Endpoint: aliyunEndpoint.URL, CDNBaseURL: aliyunCDN.URL, Bucket: "aliyun-bucket",
|
||||
AccessKeyID: "aliyun-access", AccessKeySecret: "aliyun-secret",
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
historical, _, err := svc.readUserOSSSetting(actor.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := svc.UpdateUserOSSSetting(actor, OSSSettingRequest{
|
||||
Enabled: true, Provider: qiniuKodoProvider, Endpoint: qiniuEndpoint.URL, CDNBaseURL: qiniuCDN.URL, Bucket: "qiniu-bucket",
|
||||
AccessKeyID: "qiniu-access", AccessKeySecret: "qiniu-secret",
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resource := model.Resource{
|
||||
ID: "resource-bound-historical-storage", UserID: actor.ID, Kind: "image", Status: model.ResourceStatusReady,
|
||||
Provider: aliyunOSSProvider, Endpoint: aliyunEndpoint.URL, Bucket: "aliyun-bucket", StorageSettingID: historical.ID,
|
||||
ObjectKey: "ai/users/user-1/image/bound.png", MimeType: "image/png",
|
||||
}
|
||||
if err := svc.repo.CreateResource(&resource); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
delivery, err := svc.PrepareResourceDelivery(actor.ID, resource.ID, ResourceDeliveryOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := aliyunCDN.URL + "/ai/users/user-1/image/bound.png"
|
||||
if delivery.RedirectURL != want || strings.Contains(delivery.RedirectURL, qiniuCDN.URL) {
|
||||
t.Fatalf("PrepareResourceDelivery(bound historical resource) = %q, want %q", delivery.RedirectURL, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHistoricalUserResourceWithoutStorageSettingIDKeepsItsProviderCDN(t *testing.T) {
|
||||
t.Setenv("CANVAS_ALLOW_PRIVATE_UPSTREAMS", "true")
|
||||
newTestServer := func() *httptest.Server {
|
||||
return httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}))
|
||||
}
|
||||
aliyunEndpoint := newTestServer()
|
||||
aliyunCDN := newTestServer()
|
||||
qiniuEndpoint := newTestServer()
|
||||
qiniuCDN := newTestServer()
|
||||
defer aliyunEndpoint.Close()
|
||||
defer aliyunCDN.Close()
|
||||
defer qiniuEndpoint.Close()
|
||||
defer qiniuCDN.Close()
|
||||
svc := newResourceTestService(t)
|
||||
actor := &model.User{ID: "user-1"}
|
||||
if _, err := svc.UpdateUserOSSSetting(actor, OSSSettingRequest{
|
||||
Enabled: true, Provider: aliyunOSSProvider, Endpoint: aliyunEndpoint.URL, CDNBaseURL: aliyunCDN.URL, Bucket: "aliyun-bucket",
|
||||
AccessKeyID: "aliyun-access", AccessKeySecret: "aliyun-secret",
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := svc.UpdateUserOSSSetting(actor, OSSSettingRequest{
|
||||
Enabled: true, Provider: qiniuKodoProvider, Endpoint: qiniuEndpoint.URL, CDNBaseURL: qiniuCDN.URL, Bucket: "qiniu-bucket",
|
||||
AccessKeyID: "qiniu-access", AccessKeySecret: "qiniu-secret",
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resource := model.Resource{
|
||||
ID: "resource-legacy-user-storage", UserID: actor.ID, Kind: "image", Status: model.ResourceStatusReady,
|
||||
Provider: aliyunOSSProvider, Endpoint: aliyunEndpoint.URL, Bucket: "aliyun-bucket",
|
||||
ObjectKey: "ai/users/user-1/image/legacy.png", MimeType: "image/png",
|
||||
}
|
||||
if err := svc.repo.CreateResource(&resource); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
delivery, err := svc.PrepareResourceDelivery(actor.ID, resource.ID, ResourceDeliveryOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := aliyunCDN.URL + "/ai/users/user-1/image/legacy.png"
|
||||
if delivery.RedirectURL != want || strings.Contains(delivery.RedirectURL, qiniuCDN.URL) {
|
||||
t.Fatalf("PrepareResourceDelivery(legacy user resource) = %q, want %q", delivery.RedirectURL, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrepareResourceDeliveryKeepsForcedOriginDirectWithoutCDN(t *testing.T) {
|
||||
svc := newResourceTestService(t)
|
||||
settingJSON, _ := json.Marshal(ossSettingValue{
|
||||
|
||||
Reference in new issue
Block a user