feat(assets): 文件夹主题 - 新增多主题皮肤并同步主线
This commit is contained in:
commit
536af9a69f
93 files changed
+4081
-1143
No files matched your search
@@ -1,6 +1,10 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"infinite-canvas/backend/internal/model"
|
||||
|
||||
"gorm.io/gorm"
|
||||
@@ -72,6 +76,9 @@ func MigrateSchema(db *gorm.DB) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := widenPostgresAssetIDColumns(db); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := db.AutoMigrate(Models()...); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -84,3 +91,44 @@ func MigrateSchema(db *gorm.DB) error {
|
||||
}
|
||||
return db.Exec("CREATE UNIQUE INDEX IF NOT EXISTS idx_users_email_nonempty ON users(lower(email)) WHERE email <> ''").Error
|
||||
}
|
||||
|
||||
type varcharColumnMigration struct {
|
||||
table string
|
||||
column string
|
||||
statement string
|
||||
}
|
||||
|
||||
var assetIDColumnMigrations = []varcharColumnMigration{
|
||||
{table: "assets", column: "id", statement: `ALTER TABLE "assets" ALTER COLUMN "id" TYPE varchar(80)`},
|
||||
{table: "project_asset_links", column: "asset_id", statement: `ALTER TABLE "project_asset_links" ALTER COLUMN "asset_id" TYPE varchar(80)`},
|
||||
{table: "project_asset_candidates", column: "resolved_asset_id", statement: `ALTER TABLE "project_asset_candidates" ALTER COLUMN "resolved_asset_id" TYPE varchar(80)`},
|
||||
{table: "asset_versions", column: "asset_id", statement: `ALTER TABLE "asset_versions" ALTER COLUMN "asset_id" TYPE varchar(80)`},
|
||||
}
|
||||
|
||||
// PostgreSQL Migrator 跳过主键列变更,素材 ID 扩容必须在 AutoMigrate 前显式执行。
|
||||
func widenPostgresAssetIDColumns(db *gorm.DB) error {
|
||||
if db.Dialector.Name() != "postgres" {
|
||||
return nil
|
||||
}
|
||||
for _, migration := range assetIDColumnMigrations {
|
||||
var currentLength sql.NullInt64
|
||||
err := db.Raw(
|
||||
"SELECT character_maximum_length FROM information_schema.columns WHERE table_schema = current_schema() AND table_name = ? AND column_name = ?",
|
||||
migration.table,
|
||||
migration.column,
|
||||
).Row().Scan(¤tLength)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("检查 PostgreSQL 素材 ID 列 %s.%s:%w", migration.table, migration.column, err)
|
||||
}
|
||||
if !currentLength.Valid || currentLength.Int64 >= model.AssetIDMaxLength {
|
||||
continue
|
||||
}
|
||||
if err := db.Exec(migration.statement).Error; err != nil {
|
||||
return fmt.Errorf("扩容 PostgreSQL 素材 ID 列 %s.%s:%w", migration.table, migration.column, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"infinite-canvas/backend/internal/model"
|
||||
)
|
||||
|
||||
func TestPostgresAssetIDMigration(t *testing.T) {
|
||||
dsn := strings.TrimSpace(os.Getenv("CANVAS_TEST_POSTGRES_DSN"))
|
||||
if dsn == "" {
|
||||
t.Skip("CANVAS_TEST_POSTGRES_DSN is not configured")
|
||||
}
|
||||
|
||||
base, err := Open(Config{Driver: "postgres", DSN: dsn})
|
||||
if err != nil {
|
||||
t.Fatalf("open postgres: %v", err)
|
||||
}
|
||||
baseSQL, err := base.DB()
|
||||
if err != nil {
|
||||
t.Fatalf("postgres sql db: %v", err)
|
||||
}
|
||||
defer baseSQL.Close()
|
||||
|
||||
schemaName := fmt.Sprintf("asset_id_migration_%d", time.Now().UnixNano())
|
||||
if err := base.Exec(`CREATE SCHEMA "` + schemaName + `"`).Error; err != nil {
|
||||
t.Fatalf("create test schema: %v", err)
|
||||
}
|
||||
defer func() {
|
||||
if err := base.Exec(`DROP SCHEMA IF EXISTS "` + schemaName + `" CASCADE`).Error; err != nil {
|
||||
t.Errorf("drop test schema: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
testDSN, err := postgresDSNWithSearchPath(dsn, schemaName)
|
||||
if err != nil {
|
||||
t.Fatalf("test postgres dsn: %v", err)
|
||||
}
|
||||
db, err := Open(Config{Driver: "postgres", DSN: testDSN})
|
||||
if err != nil {
|
||||
t.Fatalf("open test schema: %v", err)
|
||||
}
|
||||
dbSQL, err := db.DB()
|
||||
if err != nil {
|
||||
t.Fatalf("test schema sql db: %v", err)
|
||||
}
|
||||
defer dbSQL.Close()
|
||||
|
||||
for _, statement := range []string{
|
||||
`CREATE TABLE assets (id varchar(36) PRIMARY KEY)`,
|
||||
`CREATE TABLE project_asset_links (id varchar(36) PRIMARY KEY, asset_id varchar(36))`,
|
||||
`CREATE TABLE project_asset_candidates (id varchar(36) PRIMARY KEY, resolved_asset_id varchar(36))`,
|
||||
`CREATE TABLE asset_versions (id varchar(36) PRIMARY KEY, asset_id varchar(36))`,
|
||||
} {
|
||||
if err := db.Exec(statement).Error; err != nil {
|
||||
t.Fatalf("create legacy table: %v", err)
|
||||
}
|
||||
}
|
||||
if err := MigrateSchema(db); err != nil {
|
||||
t.Fatalf("migrate schema: %v", err)
|
||||
}
|
||||
|
||||
for _, migration := range assetIDColumnMigrations {
|
||||
var length int64
|
||||
if err := db.Raw(
|
||||
"SELECT character_maximum_length FROM information_schema.columns WHERE table_schema = current_schema() AND table_name = ? AND column_name = ?",
|
||||
migration.table,
|
||||
migration.column,
|
||||
).Scan(&length).Error; err != nil {
|
||||
t.Fatalf("read migrated column %s.%s: %v", migration.table, migration.column, err)
|
||||
}
|
||||
if length != model.AssetIDMaxLength {
|
||||
t.Fatalf("%s.%s length = %d, want %d", migration.table, migration.column, length, model.AssetIDMaxLength)
|
||||
}
|
||||
}
|
||||
|
||||
assetID := "generation_" + strings.Repeat("a", 64)
|
||||
if err := db.Create(&model.Asset{ID: assetID, UserID: "user-1", Kind: "image"}).Error; err != nil {
|
||||
t.Fatalf("insert deterministic generation asset: %v", err)
|
||||
}
|
||||
if err := db.Create(&model.ProjectAssetLink{ID: "link-1", ProjectID: "project-1", AssetID: assetID}).Error; err != nil {
|
||||
t.Fatalf("insert project asset link: %v", err)
|
||||
}
|
||||
if err := db.Create(&model.ProjectAssetCandidate{ID: "candidate-1", ProjectID: "project-1", ResolvedAssetID: assetID}).Error; err != nil {
|
||||
t.Fatalf("insert project asset candidate: %v", err)
|
||||
}
|
||||
if err := db.Create(&model.AssetVersion{ID: "version-1", AssetID: assetID, Version: 1}).Error; err != nil {
|
||||
t.Fatalf("insert asset version: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func postgresDSNWithSearchPath(dsn string, schemaName string) (string, error) {
|
||||
parsed, err := url.Parse(dsn)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
query := parsed.Query()
|
||||
query.Set("search_path", schemaName)
|
||||
parsed.RawQuery = query.Encode()
|
||||
return parsed.String(), nil
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"infinite-canvas/backend/internal/model"
|
||||
|
||||
"gorm.io/gorm/schema"
|
||||
)
|
||||
|
||||
func TestAssetIDColumnsUseSharedLimit(t *testing.T) {
|
||||
tests := []struct {
|
||||
value any
|
||||
field string
|
||||
}{
|
||||
{value: &model.Asset{}, field: "ID"},
|
||||
{value: &model.ProjectAssetLink{}, field: "AssetID"},
|
||||
{value: &model.ProjectAssetCandidate{}, field: "ResolvedAssetID"},
|
||||
{value: &model.AssetVersion{}, field: "AssetID"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
parsed, err := schema.Parse(test.value, &sync.Map{}, schema.NamingStrategy{})
|
||||
if err != nil {
|
||||
t.Fatalf("parse schema: %v", err)
|
||||
}
|
||||
field := parsed.LookUpField(test.field)
|
||||
if field == nil {
|
||||
t.Fatalf("field %s not found in %s", test.field, parsed.Table)
|
||||
}
|
||||
if field.Size != model.AssetIDMaxLength {
|
||||
t.Fatalf("%s.%s size = %d, want %d", parsed.Table, field.DBName, field.Size, model.AssetIDMaxLength)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresAssetIDMigrationsCoverEveryAssetIDColumn(t *testing.T) {
|
||||
want := map[string]bool{
|
||||
"assets.id": false,
|
||||
"project_asset_links.asset_id": false,
|
||||
"project_asset_candidates.resolved_asset_id": false,
|
||||
"asset_versions.asset_id": false,
|
||||
}
|
||||
for _, migration := range assetIDColumnMigrations {
|
||||
key := migration.table + "." + migration.column
|
||||
if _, exists := want[key]; !exists {
|
||||
t.Fatalf("unexpected asset ID migration %s", key)
|
||||
}
|
||||
if !strings.Contains(migration.statement, fmt.Sprintf("varchar(%d)", model.AssetIDMaxLength)) {
|
||||
t.Fatalf("asset ID migration %s does not use limit %d", key, model.AssetIDMaxLength)
|
||||
}
|
||||
want[key] = true
|
||||
}
|
||||
for key, covered := range want {
|
||||
if !covered {
|
||||
t.Fatalf("missing asset ID migration %s", key)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -590,11 +590,19 @@ func RegisterAdminRoutes(r *gin.RouterGroup, svc *service.Service) {
|
||||
failService(c, err)
|
||||
return
|
||||
}
|
||||
stream, err := svc.OpenAdminAPICallLogMediaRange(user, c.Param("id"), c.GetHeader("Range"))
|
||||
delivery, err := svc.PrepareAdminAPICallLogMediaDelivery(user, c.Param("id"), c.GetHeader("Range"))
|
||||
if err != nil {
|
||||
failService(c, err)
|
||||
return
|
||||
}
|
||||
if delivery.RedirectURL != "" {
|
||||
c.Header("Cache-Control", "private, no-store")
|
||||
c.Header("Referrer-Policy", "no-referrer")
|
||||
c.Header("X-Content-Type-Options", "nosniff")
|
||||
c.Redirect(http.StatusTemporaryRedirect, delivery.RedirectURL)
|
||||
return
|
||||
}
|
||||
stream := delivery.Stream
|
||||
defer stream.Body.Close()
|
||||
resource := stream.Resource
|
||||
mimeType := resource.MimeType
|
||||
@@ -707,8 +715,10 @@ func proxySystemRequest(c *gin.Context, svc *service.Service, user *model.User,
|
||||
modelName := proxyRequestModelForPath(path, c.GetHeader("Content-Type"), body)
|
||||
protocol := model.ChannelInterfaceType("")
|
||||
capability := "text"
|
||||
var channelModel *model.ChannelModel
|
||||
if !(c.Request.Method == http.MethodGet && path == "/models") {
|
||||
channelModel, modelErr := svc.SystemChannelModel(channel.ID, modelName)
|
||||
var modelErr error
|
||||
channelModel, modelErr = svc.SystemChannelModel(channel.ID, modelName)
|
||||
if modelErr != nil || channelModel.Protocol == "" {
|
||||
fail(c, http.StatusForbidden, errors.New("当前系统渠道未授权该模型或模型协议尚未配置"))
|
||||
return
|
||||
@@ -720,6 +730,13 @@ func proxySystemRequest(c *gin.Context, svc *service.Service, user *model.User,
|
||||
fail(c, http.StatusForbidden, err)
|
||||
return
|
||||
}
|
||||
if channelModel != nil && channelModel.BillingMode == "token" && protocol == model.ChannelInterfaceChatCompletion {
|
||||
body, err = service.EnsureChatCompletionStreamUsageRequest(body)
|
||||
if err != nil {
|
||||
fail(c, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
billingOrderID := ""
|
||||
query := c.Request.URL.Query()
|
||||
for _, key := range []string{"key", "api_key", "access_token", "token"} {
|
||||
|
||||
@@ -78,11 +78,21 @@ func RegisterCanvasShareRoutes(r *gin.RouterGroup, svc *service.Service) {
|
||||
if !enforceRateLimit(c, "public-canvas-resource:"+c.ClientIP(), 300, time.Minute) {
|
||||
return
|
||||
}
|
||||
stream, err := svc.OpenSharedCanvasResourceRange(c.Param("token"), c.Param("resourceId"), c.GetHeader("Range"))
|
||||
delivery, err := svc.PrepareSharedCanvasResourceDelivery(c.Param("token"), c.Param("resourceId"), c.GetHeader("Range"))
|
||||
if err != nil {
|
||||
fail(c, http.StatusNotFound, errors.New("分享资源不存在"))
|
||||
return
|
||||
}
|
||||
if delivery.RedirectURL != "" {
|
||||
c.Header("Cache-Control", "no-store")
|
||||
c.Header("Content-Security-Policy", "sandbox")
|
||||
c.Header("Referrer-Policy", "no-referrer")
|
||||
c.Header("X-Content-Type-Options", "nosniff")
|
||||
c.Header("X-Robots-Tag", "noindex, nofollow")
|
||||
c.Redirect(http.StatusTemporaryRedirect, delivery.RedirectURL)
|
||||
return
|
||||
}
|
||||
stream := delivery.Stream
|
||||
defer stream.Body.Close()
|
||||
resource := stream.Resource
|
||||
mimeType := resource.MimeType
|
||||
|
||||
@@ -214,24 +214,23 @@ func RegisterUserDataRoutes(r *gin.RouterGroup, svc *service.Service) {
|
||||
failService(c, err)
|
||||
return
|
||||
}
|
||||
resource, err := svc.Resource(user.ID, c.Param("id"))
|
||||
delivery, err := svc.PrepareResourceDelivery(user.ID, c.Param("id"), service.ResourceDeliveryOptions{
|
||||
ForceDirect: c.Query("direct") == "1",
|
||||
ForceProxy: c.Query("proxy") == "1",
|
||||
})
|
||||
if err != nil {
|
||||
fail(c, http.StatusNotFound, err)
|
||||
failService(c, err)
|
||||
return
|
||||
}
|
||||
if c.Query("direct") == "1" && resource.Provider != "local" {
|
||||
directURL, err := svc.DirectResourceURL(user.ID, resource.ID)
|
||||
if err != nil {
|
||||
failService(c, err)
|
||||
return
|
||||
}
|
||||
// 签名 URL 不进入应用、代理或浏览器缓存,也不作为后续请求的 Referer 泄露。
|
||||
if delivery.RedirectURL != "" {
|
||||
// CDN 或对象存储直连地址不进入应用缓存,也不作为后续请求的 Referer 泄露。
|
||||
c.Header("Cache-Control", "private, no-store")
|
||||
c.Header("Referrer-Policy", "no-referrer")
|
||||
c.Header("X-Content-Type-Options", "nosniff")
|
||||
c.Redirect(http.StatusTemporaryRedirect, directURL)
|
||||
c.Redirect(http.StatusTemporaryRedirect, delivery.RedirectURL)
|
||||
return
|
||||
}
|
||||
resource := delivery.Resource
|
||||
etag := resourceResponseETag(resource)
|
||||
// 私有资源允许浏览器保存响应,但每次复用前必须重新鉴权;304 会在读取 OSS 前返回。
|
||||
c.Header("Cache-Control", "private, no-cache")
|
||||
|
||||
@@ -2,6 +2,8 @@ package model
|
||||
|
||||
import "time"
|
||||
|
||||
const AssetIDMaxLength = 80
|
||||
|
||||
type Resource struct {
|
||||
ID string `json:"id" gorm:"primaryKey;size:36"`
|
||||
UserID string `json:"userId" gorm:"index;size:36;index:idx_resources_user_created,priority:1"`
|
||||
@@ -10,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 每次修改都会生成新版本,资源固定引用创建时的版本,避免历史资源因换密钥失效。
|
||||
// 用户 OSS 每次修改都会生成新版本,资源固定引用创建时的存储与密钥;同一存储位置的 CDN 域名可跟随当前配置。
|
||||
StorageSettingID string `json:"-" gorm:"index;size:36"`
|
||||
ObjectKey string `json:"objectKey" gorm:"index"`
|
||||
PublicURL string `json:"publicUrl"`
|
||||
@@ -26,7 +28,7 @@ type Resource struct {
|
||||
}
|
||||
|
||||
type Asset struct {
|
||||
ID string `json:"id" gorm:"primaryKey;size:36"`
|
||||
ID string `json:"id" gorm:"primaryKey;size:80"`
|
||||
UserID string `json:"userId" gorm:"index;size:36;index:idx_assets_user_updated,priority:1"`
|
||||
Kind string `json:"kind" gorm:"index;size:24"`
|
||||
Category AssetCategory `json:"category" gorm:"index;size:32"`
|
||||
@@ -41,7 +43,7 @@ type Asset struct {
|
||||
type ProjectAssetLink struct {
|
||||
ID string `json:"id" gorm:"primaryKey;size:36"`
|
||||
ProjectID string `json:"projectId" gorm:"index;size:36;uniqueIndex:idx_project_asset_links_unique,priority:1"`
|
||||
AssetID string `json:"assetId" gorm:"index;size:36;uniqueIndex:idx_project_asset_links_unique,priority:2"`
|
||||
AssetID string `json:"assetId" gorm:"index;size:80;uniqueIndex:idx_project_asset_links_unique,priority:2"`
|
||||
FolderID string `json:"folderId,omitempty" gorm:"index;size:36"`
|
||||
Position int `json:"position" gorm:"index"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
@@ -55,6 +57,7 @@ type ProjectAssetFolder struct {
|
||||
Name string `json:"name" gorm:"size:240"`
|
||||
NameKey string `json:"-" gorm:"size:240;uniqueIndex:idx_project_asset_folders_sibling_name,priority:3"`
|
||||
Style string `json:"style" gorm:"size:24"`
|
||||
Theme string `json:"theme" gorm:"size:24"`
|
||||
Position int `json:"position" gorm:"index"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
@@ -69,14 +72,14 @@ type ProjectAssetCandidate struct {
|
||||
Category AssetCategory `json:"category" gorm:"index;size:32"`
|
||||
Status string `json:"status" gorm:"index;size:32"`
|
||||
DetailsJSON string `json:"detailsJson" gorm:"type:text"`
|
||||
ResolvedAssetID string `json:"resolvedAssetId,omitempty" gorm:"index;size:36"`
|
||||
ResolvedAssetID string `json:"resolvedAssetId,omitempty" gorm:"index;size:80"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
}
|
||||
|
||||
type AssetVersion struct {
|
||||
ID string `json:"id" gorm:"primaryKey;size:36"`
|
||||
AssetID string `json:"assetId" gorm:"index;size:36;uniqueIndex:idx_asset_versions_number,priority:1"`
|
||||
AssetID string `json:"assetId" gorm:"index;size:80;uniqueIndex:idx_asset_versions_number,priority:1"`
|
||||
Version int `json:"version" gorm:"uniqueIndex:idx_asset_versions_number,priority:2"`
|
||||
Status AssetVersionStatus `json:"status" gorm:"index;size:24"`
|
||||
DefinitionJSON string `json:"definitionJson" gorm:"type:text"`
|
||||
|
||||
@@ -854,19 +854,7 @@ func (r *Repository) UpsertAsset(asset *model.Asset) error {
|
||||
}
|
||||
|
||||
func (r *Repository) DeleteAsset(userID string, id string) error {
|
||||
return r.db.Transaction(func(tx *gorm.DB) error {
|
||||
versionIDs := tx.Model(&model.AssetVersion{}).Select("id").Where("asset_id = ?", id)
|
||||
if err := tx.Where("asset_version_id IN (?)", versionIDs).Delete(&model.CharacterVoiceBinding{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Where("asset_version_id IN (?)", versionIDs).Delete(&model.AssetRepresentation{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Where("asset_id = ?", id).Delete(&model.AssetVersion{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Delete(&model.Asset{}, "id = ? AND user_id = ?", id, userID).Error
|
||||
})
|
||||
return r.DeleteAssetAndResources(userID, id, nil)
|
||||
}
|
||||
|
||||
func (r *Repository) ReplaceAssets(userID string, assets []model.Asset) error {
|
||||
@@ -1228,7 +1216,7 @@ func (r *Repository) UpdateProjectAssetFolder(folder *model.ProjectAssetFolder)
|
||||
return r.db.Transaction(func(tx *gorm.DB) error {
|
||||
result := tx.Model(&model.ProjectAssetFolder{}).
|
||||
Where("id = ? AND project_id = ?", folder.ID, folder.ProjectID).
|
||||
Updates(map[string]any{"parent_id": folder.ParentID, "name": folder.Name, "name_key": folder.NameKey, "style": folder.Style, "position": folder.Position, "updated_at": folder.UpdatedAt})
|
||||
Updates(map[string]any{"parent_id": folder.ParentID, "name": folder.Name, "name_key": folder.NameKey, "style": folder.Style, "theme": folder.Theme, "position": folder.Position, "updated_at": folder.UpdatedAt})
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
|
||||
@@ -0,0 +1,273 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"infinite-canvas/backend/internal/model"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// ResourceReferenceDocument 是资源删除校验使用的只读业务文档快照。
|
||||
// JSON 中的精确资源 ID 识别由 service 完成,repository 只负责缩小候选记录。
|
||||
type ResourceReferenceDocument struct {
|
||||
Kind string
|
||||
ID string
|
||||
Title string
|
||||
PrimaryJSON string
|
||||
SecondaryJSON string
|
||||
}
|
||||
|
||||
type ResourceDirectReference struct {
|
||||
Kind string
|
||||
ID string
|
||||
Title string
|
||||
ResourceID string
|
||||
}
|
||||
|
||||
type ResourceReferenceSnapshot struct {
|
||||
Documents []ResourceReferenceDocument
|
||||
Direct []ResourceDirectReference
|
||||
}
|
||||
|
||||
func (r *Repository) AssetResourceRecords(assetID string) ([]model.AssetVersion, []model.AssetRepresentation, error) {
|
||||
var versions []model.AssetVersion
|
||||
if err := r.db.Where("asset_id = ?", assetID).Find(&versions).Error; err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if len(versions) == 0 {
|
||||
return versions, nil, nil
|
||||
}
|
||||
versionIDs := make([]string, 0, len(versions))
|
||||
for _, version := range versions {
|
||||
versionIDs = append(versionIDs, version.ID)
|
||||
}
|
||||
var representations []model.AssetRepresentation
|
||||
if err := r.db.Where("asset_version_id IN ?", versionIDs).Find(&representations).Error; err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return versions, representations, nil
|
||||
}
|
||||
|
||||
func (r *Repository) ResourcesForUserIDs(userID string, resourceIDs []string) ([]model.Resource, error) {
|
||||
if len(resourceIDs) == 0 {
|
||||
return []model.Resource{}, nil
|
||||
}
|
||||
var resources []model.Resource
|
||||
err := r.db.Where("user_id = ? AND id IN ?", userID, resourceIDs).Find(&resources).Error
|
||||
return resources, err
|
||||
}
|
||||
|
||||
// ResourceStorageReferenceCount 返回未包含在 excludedResourceIDs 中、但指向同一物理对象的资源记录数。
|
||||
// 这用于兼容历史数据中多个 Resource 行复用一个对象路径的情况。
|
||||
func (r *Repository) ResourceStorageReferenceCount(resource *model.Resource, excludedResourceIDs []string) (int64, error) {
|
||||
if resource == nil {
|
||||
return 0, nil
|
||||
}
|
||||
query := r.db.Model(&model.Resource{}).
|
||||
Where("endpoint = ? AND bucket = ? AND object_key = ?", resource.Endpoint, resource.Bucket, resource.ObjectKey)
|
||||
if strings.TrimSpace(resource.Provider) == "" || strings.EqualFold(strings.TrimSpace(resource.Provider), "local") {
|
||||
query = query.Where("provider IN ?", []string{"", "local"})
|
||||
} else {
|
||||
query = query.Where("provider = ?", resource.Provider)
|
||||
}
|
||||
if len(excludedResourceIDs) > 0 {
|
||||
query = query.Where("id NOT IN ?", excludedResourceIDs)
|
||||
}
|
||||
var count int64
|
||||
err := query.Count(&count).Error
|
||||
return count, err
|
||||
}
|
||||
|
||||
func (r *Repository) ResourceReferenceSnapshot(userID string, excludingAssetID string, resourceIDs []string) (ResourceReferenceSnapshot, error) {
|
||||
snapshot := ResourceReferenceSnapshot{Documents: []ResourceReferenceDocument{}, Direct: []ResourceDirectReference{}}
|
||||
if len(resourceIDs) == 0 {
|
||||
return snapshot, nil
|
||||
}
|
||||
|
||||
var assets []model.Asset
|
||||
assetQuery := r.db.Where("user_id = ? AND id <> ?", userID, excludingAssetID)
|
||||
if err := resourceTextQuery(assetQuery, []string{"payload_json"}, resourceIDs).Find(&assets).Error; err != nil {
|
||||
return snapshot, err
|
||||
}
|
||||
for _, asset := range assets {
|
||||
snapshot.Documents = append(snapshot.Documents, ResourceReferenceDocument{Kind: "素材", ID: asset.ID, Title: asset.Title, PrimaryJSON: asset.PayloadJSON})
|
||||
}
|
||||
|
||||
var canvases []model.CanvasProject
|
||||
if err := resourceTextQuery(r.db.Where("user_id = ?", userID), []string{"payload_json"}, resourceIDs).Find(&canvases).Error; err != nil {
|
||||
return snapshot, err
|
||||
}
|
||||
for _, canvas := range canvases {
|
||||
snapshot.Documents = append(snapshot.Documents, ResourceReferenceDocument{Kind: "画布", ID: canvas.ID, Title: canvas.Title, PrimaryJSON: canvas.PayloadJSON})
|
||||
}
|
||||
|
||||
var tasks []model.Task
|
||||
if err := resourceTextQuery(r.db.Where("user_id = ?", userID), []string{"input_json", "result_json"}, resourceIDs).Find(&tasks).Error; err != nil {
|
||||
return snapshot, err
|
||||
}
|
||||
for _, task := range tasks {
|
||||
snapshot.Documents = append(snapshot.Documents, ResourceReferenceDocument{Kind: "任务", ID: task.ID, Title: task.Prompt, PrimaryJSON: task.InputJSON, SecondaryJSON: task.ResultJSON})
|
||||
}
|
||||
|
||||
var projects []model.Project
|
||||
if err := resourceTextQuery(r.db.Where("user_id = ?", userID), []string{"style_profile_json"}, resourceIDs).Find(&projects).Error; err != nil {
|
||||
return snapshot, err
|
||||
}
|
||||
for _, project := range projects {
|
||||
snapshot.Documents = append(snapshot.Documents, ResourceReferenceDocument{Kind: "项目", ID: project.ID, Title: project.Name, PrimaryJSON: project.StyleProfileJSON})
|
||||
}
|
||||
|
||||
var styles []model.StyleProfile
|
||||
if err := resourceTextQuery(r.db.Where("user_id = ?", userID), []string{"cover_url", "profile_json"}, resourceIDs).Find(&styles).Error; err != nil {
|
||||
return snapshot, err
|
||||
}
|
||||
for _, style := range styles {
|
||||
snapshot.Documents = append(snapshot.Documents, ResourceReferenceDocument{Kind: "风格", ID: style.ID, Title: style.Name, PrimaryJSON: style.CoverURL, SecondaryJSON: style.ProfileJSON})
|
||||
}
|
||||
|
||||
type joinedDocument struct {
|
||||
ID string
|
||||
Title string
|
||||
PrimaryJSON string
|
||||
SecondaryJSON string
|
||||
}
|
||||
var versions []joinedDocument
|
||||
versionQuery := r.db.Table("asset_versions").
|
||||
Select("asset_versions.id, assets.title, asset_versions.definition_json AS primary_json").
|
||||
Joins("JOIN assets ON assets.id = asset_versions.asset_id").
|
||||
Where("assets.user_id = ? AND assets.id <> ?", userID, excludingAssetID)
|
||||
if err := resourceTextQuery(versionQuery, []string{"asset_versions.definition_json"}, resourceIDs).Scan(&versions).Error; err != nil {
|
||||
return snapshot, err
|
||||
}
|
||||
for _, version := range versions {
|
||||
snapshot.Documents = append(snapshot.Documents, ResourceReferenceDocument{Kind: "素材", ID: version.ID, Title: version.Title, PrimaryJSON: version.PrimaryJSON})
|
||||
}
|
||||
|
||||
var candidates []joinedDocument
|
||||
candidateQuery := r.db.Table("project_asset_candidates").
|
||||
Select("project_asset_candidates.id, projects.name AS title, project_asset_candidates.details_json AS primary_json").
|
||||
Joins("JOIN projects ON projects.id = project_asset_candidates.project_id").
|
||||
Where("projects.user_id = ?", userID)
|
||||
if err := resourceTextQuery(candidateQuery, []string{"project_asset_candidates.details_json"}, resourceIDs).Scan(&candidates).Error; err != nil {
|
||||
return snapshot, err
|
||||
}
|
||||
for _, candidate := range candidates {
|
||||
snapshot.Documents = append(snapshot.Documents, ResourceReferenceDocument{Kind: "项目", ID: candidate.ID, Title: candidate.Title, PrimaryJSON: candidate.PrimaryJSON})
|
||||
}
|
||||
|
||||
var steps []joinedDocument
|
||||
stepQuery := r.db.Table("workflow_step_instances").
|
||||
Select("workflow_step_instances.id, workflow_step_instances.name AS title, workflow_step_instances.input_json AS primary_json, workflow_step_instances.output_json AS secondary_json").
|
||||
Joins("JOIN workflow_instances ON workflow_instances.id = workflow_step_instances.workflow_instance_id").
|
||||
Joins("JOIN projects ON projects.id = workflow_instances.project_id").
|
||||
Where("projects.user_id = ?", userID)
|
||||
if err := resourceTextQuery(stepQuery, []string{"workflow_step_instances.input_json", "workflow_step_instances.output_json"}, resourceIDs).Scan(&steps).Error; err != nil {
|
||||
return snapshot, err
|
||||
}
|
||||
for _, step := range steps {
|
||||
snapshot.Documents = append(snapshot.Documents, ResourceReferenceDocument{Kind: "工作流", ID: step.ID, Title: step.Title, PrimaryJSON: step.PrimaryJSON, SecondaryJSON: step.SecondaryJSON})
|
||||
}
|
||||
|
||||
type joinedRepresentation struct {
|
||||
ID string
|
||||
Title string
|
||||
ResourceID string
|
||||
}
|
||||
var representations []joinedRepresentation
|
||||
if err := r.db.Table("asset_representations").
|
||||
Select("asset_representations.id, assets.title, asset_representations.resource_id").
|
||||
Joins("JOIN asset_versions ON asset_versions.id = asset_representations.asset_version_id").
|
||||
Joins("JOIN assets ON assets.id = asset_versions.asset_id").
|
||||
Where("assets.user_id = ? AND assets.id <> ? AND asset_representations.resource_id IN ?", userID, excludingAssetID, resourceIDs).
|
||||
Scan(&representations).Error; err != nil {
|
||||
return snapshot, err
|
||||
}
|
||||
for _, representation := range representations {
|
||||
snapshot.Direct = append(snapshot.Direct, ResourceDirectReference{Kind: "素材", ID: representation.ID, Title: representation.Title, ResourceID: representation.ResourceID})
|
||||
}
|
||||
|
||||
var voices []model.VoiceProfile
|
||||
if err := r.db.Where("user_id = ? AND sample_resource_id IN ?", userID, resourceIDs).Find(&voices).Error; err != nil {
|
||||
return snapshot, err
|
||||
}
|
||||
for _, voice := range voices {
|
||||
snapshot.Direct = append(snapshot.Direct, ResourceDirectReference{Kind: "声音", ID: voice.ID, Title: voice.Name, ResourceID: voice.SampleResourceID})
|
||||
}
|
||||
return snapshot, nil
|
||||
}
|
||||
|
||||
func (r *Repository) AssetBusinessReferences(userID string, assetID string) ([]ResourceDirectReference, error) {
|
||||
type projectReference struct {
|
||||
ID string
|
||||
Title string
|
||||
}
|
||||
var projects []projectReference
|
||||
if err := r.db.Table("project_asset_links").
|
||||
Distinct("projects.id, projects.name AS title").
|
||||
Joins("JOIN projects ON projects.id = project_asset_links.project_id").
|
||||
Where("projects.user_id = ? AND project_asset_links.asset_id = ?", userID, assetID).
|
||||
Scan(&projects).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var shotProjects []projectReference
|
||||
if err := r.db.Table("shot_asset_references").
|
||||
Distinct("projects.id, projects.name AS title").
|
||||
Joins("JOIN asset_versions ON asset_versions.id = shot_asset_references.asset_version_id").
|
||||
Joins("JOIN shots ON shots.id = shot_asset_references.shot_id").
|
||||
Joins("JOIN projects ON projects.id = shots.project_id").
|
||||
Where("projects.user_id = ? AND asset_versions.asset_id = ?", userID, assetID).
|
||||
Scan(&shotProjects).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var candidateProjects []projectReference
|
||||
if err := r.db.Table("project_asset_candidates").
|
||||
Distinct("projects.id, projects.name AS title").
|
||||
Joins("JOIN projects ON projects.id = project_asset_candidates.project_id").
|
||||
Where("projects.user_id = ? AND project_asset_candidates.resolved_asset_id = ?", userID, assetID).
|
||||
Scan(&candidateProjects).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := make([]ResourceDirectReference, 0, len(projects)+len(shotProjects)+len(candidateProjects))
|
||||
for _, project := range append(append(projects, shotProjects...), candidateProjects...) {
|
||||
result = append(result, ResourceDirectReference{Kind: "项目", ID: project.ID, Title: project.Title})
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (r *Repository) DeleteAssetAndResources(userID string, assetID string, resourceIDs []string) error {
|
||||
return r.db.Transaction(func(tx *gorm.DB) error {
|
||||
versionIDs := tx.Model(&model.AssetVersion{}).Select("id").Where("asset_id = ?", assetID)
|
||||
if err := tx.Where("asset_version_id IN (?)", versionIDs).Delete(&model.CharacterVoiceBinding{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Where("asset_version_id IN (?)", versionIDs).Delete(&model.AssetRepresentation{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Where("asset_id = ?", assetID).Delete(&model.AssetVersion{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Delete(&model.Asset{}, "id = ? AND user_id = ?", assetID, userID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if len(resourceIDs) == 0 {
|
||||
return nil
|
||||
}
|
||||
return tx.Where("user_id = ? AND id IN ?", userID, resourceIDs).Delete(&model.Resource{}).Error
|
||||
})
|
||||
}
|
||||
|
||||
func resourceTextQuery(query *gorm.DB, columns []string, resourceIDs []string) *gorm.DB {
|
||||
conditions := make([]string, 0, len(columns)*len(resourceIDs))
|
||||
args := make([]any, 0, len(columns)*len(resourceIDs))
|
||||
for _, column := range columns {
|
||||
for _, resourceID := range resourceIDs {
|
||||
conditions = append(conditions, column+" LIKE ?")
|
||||
args = append(args, "%"+resourceID+"%")
|
||||
}
|
||||
}
|
||||
if len(conditions) == 0 {
|
||||
return query
|
||||
}
|
||||
return query.Where("("+strings.Join(conditions, " OR ")+")", args...)
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"encoding/csv"
|
||||
"encoding/json"
|
||||
@@ -280,33 +281,58 @@ func (s *Service) decorateAPICallLogs(logs []model.ApiCallLog) error {
|
||||
|
||||
// 管理员媒体读取必须同时校验日志、任务和资源归属,不能绕过用户资源边界按资源 ID 任意读取。
|
||||
func (s *Service) OpenAdminAPICallLogMediaRange(actor *model.User, logID string, rangeHeader string) (*ResourceStream, error) {
|
||||
if err := s.RequireAdmin(actor); err != nil {
|
||||
userID, resource, err := s.adminAPICallLogMediaResource(actor, logID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.openResourceRange(userID, resource, rangeHeader)
|
||||
}
|
||||
|
||||
func (s *Service) PrepareAdminAPICallLogMediaDelivery(actor *model.User, logID string, rangeHeader string) (*ResourceDelivery, error) {
|
||||
userID, resource, err := s.adminAPICallLogMediaResource(actor, logID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
delivery, err := s.prepareResourceDelivery(userID, resource, ResourceDeliveryOptions{})
|
||||
if err != nil || delivery.RedirectURL != "" {
|
||||
return delivery, err
|
||||
}
|
||||
stream, err := s.openResourceRange(userID, resource, rangeHeader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
delivery.Stream = stream
|
||||
return delivery, nil
|
||||
}
|
||||
|
||||
func (s *Service) adminAPICallLogMediaResource(actor *model.User, logID string) (string, *model.Resource, error) {
|
||||
if err := s.RequireAdmin(actor); err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
log, err := s.repo.APICallLog(strings.TrimSpace(logID))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return "", nil, err
|
||||
}
|
||||
if log.TaskID == "" || (log.Capability != "image" && log.Capability != "video") {
|
||||
return nil, BadAuthRequest("该请求没有可预览媒体")
|
||||
return "", nil, BadAuthRequest("该请求没有可预览媒体")
|
||||
}
|
||||
task, err := s.repo.Task(log.TaskID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return "", nil, err
|
||||
}
|
||||
if task.UserID != log.UserID {
|
||||
return nil, BadAuthRequest("请求与媒体归属不一致")
|
||||
return "", nil, BadAuthRequest("请求与媒体归属不一致")
|
||||
}
|
||||
previewURL, _ := taskMediaPreview(task.ResultJSON, task.Type)
|
||||
resourceID := canvasResourceID(previewURL)
|
||||
if resourceID == "" {
|
||||
return nil, BadAuthRequest("该请求没有已持久化媒体")
|
||||
return "", nil, BadAuthRequest("该请求没有已持久化媒体")
|
||||
}
|
||||
resource, err := s.repo.ResourceForUser(log.UserID, resourceID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return "", nil, err
|
||||
}
|
||||
return s.openResourceRange(log.UserID, resource, rangeHeader)
|
||||
return log.UserID, resource, nil
|
||||
}
|
||||
|
||||
func (s *Service) AdminAPICallLog(actor *model.User, id string) (*model.ApiCallLog, error) {
|
||||
@@ -914,13 +940,16 @@ func (s *Service) EnrichAPICallLog(log *model.ApiCallLog, responseBody []byte) {
|
||||
if log.ProviderRequestID == "" {
|
||||
log.ProviderRequestID = providerRequestIDFromPath(log.Path)
|
||||
}
|
||||
if len(responseBody) == 0 || !json.Valid(responseBody) {
|
||||
payloads := providerResponsePayloads(responseBody)
|
||||
if len(payloads) == 0 {
|
||||
return
|
||||
}
|
||||
var payload map[string]any
|
||||
if json.Unmarshal(responseBody, &payload) != nil {
|
||||
return
|
||||
for _, payload := range payloads {
|
||||
s.enrichAPICallLogPayload(log, payload)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) enrichAPICallLogPayload(log *model.ApiCallLog, payload map[string]any) {
|
||||
if data, ok := payload["data"].(map[string]any); ok {
|
||||
for key, value := range data {
|
||||
if _, exists := payload[key]; !exists {
|
||||
@@ -928,6 +957,14 @@ func (s *Service) EnrichAPICallLog(log *model.ApiCallLog, responseBody []byte) {
|
||||
}
|
||||
}
|
||||
}
|
||||
// Responses API 的终态 SSE 把实际响应(包括 usage)放在 response 字段中。
|
||||
if response, ok := payload["response"].(map[string]any); ok {
|
||||
for key, value := range response {
|
||||
if _, exists := payload[key]; !exists {
|
||||
payload[key] = value
|
||||
}
|
||||
}
|
||||
}
|
||||
if log.Status == model.ApiCallStatusFailed {
|
||||
errorCode, errorMessage := providerFailureDetails(payload)
|
||||
log.ErrorCode = errorCode
|
||||
@@ -937,16 +974,24 @@ func (s *Service) EnrichAPICallLog(log *model.ApiCallLog, responseBody []byte) {
|
||||
}
|
||||
usage, _ := payload["usage"].(map[string]any)
|
||||
if usage != nil {
|
||||
log.UsageAvailable = true
|
||||
log.InputTokens = firstInt64(usage, "input_tokens", "prompt_tokens")
|
||||
log.OutputTokens = firstInt64(usage, "output_tokens", "completion_tokens")
|
||||
inputTokens, inputAvailable := firstInt64Value(usage, "input_tokens", "prompt_tokens")
|
||||
outputTokens, outputAvailable := firstInt64Value(usage, "output_tokens", "completion_tokens")
|
||||
if inputAvailable {
|
||||
log.InputTokens = inputTokens
|
||||
}
|
||||
if outputAvailable {
|
||||
log.OutputTokens = outputTokens
|
||||
}
|
||||
if inputAvailable || outputAvailable {
|
||||
log.UsageAvailable = true
|
||||
}
|
||||
// 火山方舟视频的输入 Token 恒为 0;查询任务以 completion_tokens 为实际用量,
|
||||
// 兼容只返回 total_tokens 的同协议中转实现。
|
||||
if log.Capability == "video" && strings.Contains(log.Path, "/contents/generations/tasks") && log.OutputTokens == 0 {
|
||||
log.OutputTokens = firstInt64(usage, "total_tokens")
|
||||
}
|
||||
if log.Capability == "video" && strings.Contains(log.Path, "/contents/generations/tasks") && log.OutputTokens <= 0 {
|
||||
log.UsageAvailable = false
|
||||
if log.Capability == "video" && strings.Contains(log.Path, "/contents/generations/tasks") {
|
||||
if log.OutputTokens == 0 {
|
||||
log.OutputTokens = firstInt64(usage, "total_tokens")
|
||||
}
|
||||
log.UsageAvailable = log.OutputTokens > 0
|
||||
}
|
||||
if details, ok := usage["input_tokens_details"].(map[string]any); ok {
|
||||
log.CachedTokens = firstInt64(details, "cached_tokens")
|
||||
@@ -956,9 +1001,17 @@ func (s *Service) EnrichAPICallLog(log *model.ApiCallLog, responseBody []byte) {
|
||||
}
|
||||
}
|
||||
if usageMetadata, ok := payload["usageMetadata"].(map[string]any); ok {
|
||||
log.UsageAvailable = true
|
||||
log.InputTokens = firstInt64(usageMetadata, "promptTokenCount")
|
||||
log.OutputTokens = firstInt64(usageMetadata, "candidatesTokenCount")
|
||||
inputTokens, inputAvailable := firstInt64Value(usageMetadata, "promptTokenCount")
|
||||
outputTokens, outputAvailable := firstInt64Value(usageMetadata, "candidatesTokenCount")
|
||||
if inputAvailable {
|
||||
log.InputTokens = inputTokens
|
||||
}
|
||||
if outputAvailable {
|
||||
log.OutputTokens = outputTokens
|
||||
}
|
||||
if inputAvailable || outputAvailable {
|
||||
log.UsageAvailable = true
|
||||
}
|
||||
log.CachedTokens = firstInt64(usageMetadata, "cachedContentTokenCount")
|
||||
}
|
||||
log.ProviderRequestID = firstNonEmpty(stringField(payload, "task_id"), stringField(payload, "id"), stringField(payload, "request_id"), stringField(payload, "name"), log.ProviderRequestID)
|
||||
@@ -978,6 +1031,45 @@ func (s *Service) EnrichAPICallLog(log *model.ApiCallLog, responseBody []byte) {
|
||||
}
|
||||
}
|
||||
|
||||
func providerResponsePayloads(responseBody []byte) []map[string]any {
|
||||
if len(responseBody) == 0 {
|
||||
return nil
|
||||
}
|
||||
var payload map[string]any
|
||||
if json.Unmarshal(responseBody, &payload) == nil {
|
||||
return []map[string]any{payload}
|
||||
}
|
||||
|
||||
// 流式文本的用量只出现在最后一个 SSE data 事件中,不能把整段响应当作 JSON。
|
||||
result := make([]map[string]any, 0)
|
||||
scanner := bufio.NewScanner(bytes.NewReader(responseBody))
|
||||
scanner.Buffer(make([]byte, 64<<10), max(len(responseBody)+1, 64<<10))
|
||||
dataLines := make([]string, 0, 1)
|
||||
flush := func() {
|
||||
raw := strings.TrimSpace(strings.Join(dataLines, "\n"))
|
||||
dataLines = dataLines[:0]
|
||||
if raw == "" || raw == "[DONE]" {
|
||||
return
|
||||
}
|
||||
var event map[string]any
|
||||
if json.Unmarshal([]byte(raw), &event) == nil {
|
||||
result = append(result, event)
|
||||
}
|
||||
}
|
||||
for scanner.Scan() {
|
||||
line := strings.TrimSuffix(scanner.Text(), "\r")
|
||||
if line == "" {
|
||||
flush()
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(line, "data:") {
|
||||
dataLines = append(dataLines, strings.TrimPrefix(strings.TrimPrefix(line, "data:"), " "))
|
||||
}
|
||||
}
|
||||
flush()
|
||||
return result
|
||||
}
|
||||
|
||||
func providerRequestIDFromPath(path string) string {
|
||||
parts := strings.Split(strings.Trim(strings.TrimSpace(path), "/"), "/")
|
||||
for index := len(parts) - 1; index >= 0; index-- {
|
||||
@@ -994,18 +1086,23 @@ func providerRequestIDFromPath(path string) string {
|
||||
}
|
||||
|
||||
func firstInt64(values map[string]any, keys ...string) int64 {
|
||||
value, _ := firstInt64Value(values, keys...)
|
||||
return value
|
||||
}
|
||||
|
||||
func firstInt64Value(values map[string]any, keys ...string) (int64, bool) {
|
||||
for _, key := range keys {
|
||||
switch value := values[key].(type) {
|
||||
case float64:
|
||||
return int64(value)
|
||||
return int64(value), true
|
||||
case int64:
|
||||
return value
|
||||
return value, true
|
||||
case json.Number:
|
||||
parsed, _ := value.Int64()
|
||||
return parsed
|
||||
parsed, err := value.Int64()
|
||||
return parsed, err == nil
|
||||
}
|
||||
}
|
||||
return 0
|
||||
return 0, false
|
||||
}
|
||||
|
||||
func (s *Service) recordActivity(userID string, event string, count int) {
|
||||
|
||||
@@ -70,6 +70,10 @@ func BadAuthRequest(message string) *AuthError {
|
||||
return &AuthError{Status: 400, Message: message}
|
||||
}
|
||||
|
||||
func NotFound(message string) *AuthError {
|
||||
return &AuthError{Status: 404, Message: message}
|
||||
}
|
||||
|
||||
func Unauthorized(message string) *AuthError {
|
||||
return &AuthError{Status: 401, Message: message}
|
||||
}
|
||||
|
||||
@@ -137,18 +137,47 @@ func (s *Service) OpenSharedCanvasResource(token string, resourceID string) (*mo
|
||||
}
|
||||
|
||||
func (s *Service) OpenSharedCanvasResourceRange(token string, resourceID string, rangeHeader string) (*ResourceStream, error) {
|
||||
share, project, err := s.sharedCanvasProject(token)
|
||||
userID, resource, err := s.sharedCanvasResource(token, resourceID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.openResourceRange(userID, resource, rangeHeader)
|
||||
}
|
||||
|
||||
func (s *Service) PrepareSharedCanvasResourceDelivery(token string, resourceID string, rangeHeader string) (*ResourceDelivery, error) {
|
||||
userID, resource, err := s.sharedCanvasResource(token, resourceID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
delivery, err := s.prepareResourceDelivery(userID, resource, ResourceDeliveryOptions{})
|
||||
if err != nil || delivery.RedirectURL != "" {
|
||||
return delivery, err
|
||||
}
|
||||
stream, err := s.openResourceRange(userID, resource, rangeHeader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
delivery.Stream = stream
|
||||
return delivery, nil
|
||||
}
|
||||
|
||||
func (s *Service) sharedCanvasResource(token string, resourceID string) (string, *model.Resource, error) {
|
||||
share, project, err := s.sharedCanvasProject(token)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
_, allowedResources, err := publicCanvasProject(project, token)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return "", nil, err
|
||||
}
|
||||
if !allowedResources[resourceID] {
|
||||
return nil, gorm.ErrRecordNotFound
|
||||
return "", nil, gorm.ErrRecordNotFound
|
||||
}
|
||||
return s.OpenResourceRange(share.UserID, resourceID, rangeHeader)
|
||||
resource, err := s.repo.ResourceForUser(share.UserID, resourceID)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
return share.UserID, resource, nil
|
||||
}
|
||||
|
||||
func (s *Service) sharedCanvasProject(token string) (*model.CanvasShare, *model.CanvasProject, error) {
|
||||
|
||||
@@ -29,9 +29,28 @@ type channelModelsPayload struct {
|
||||
}
|
||||
|
||||
type channelModelItem struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
SupportedEndpointTypes []string `json:"supported_endpoint_types"`
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
DisplayName string `json:"display_name"`
|
||||
ModelType string `json:"model_type"`
|
||||
SupportedEndpointTypes []string `json:"supported_endpoint_types"`
|
||||
DefaultParameters channelModelCatalogParameters `json:"default_parameters"`
|
||||
Options channelModelCatalogOptions `json:"options"`
|
||||
SupportsImages *bool `json:"supports_images"`
|
||||
MinImages *int `json:"min_images"`
|
||||
MaxImages *int `json:"max_images"`
|
||||
}
|
||||
|
||||
type channelModelCatalogParameters struct {
|
||||
AspectRatio string `json:"aspect_ratio"`
|
||||
DurationSeconds string `json:"duration_seconds"`
|
||||
Resolution string `json:"resolution"`
|
||||
}
|
||||
|
||||
type channelModelCatalogOptions struct {
|
||||
AspectRatio []ChannelModelCatalogOption `json:"aspect_ratio"`
|
||||
DurationSeconds []ChannelModelCatalogOption `json:"duration_seconds"`
|
||||
Resolution []ChannelModelCatalogOption `json:"resolution"`
|
||||
}
|
||||
|
||||
func (s *Service) FetchChannelModels(ctx context.Context, actor *model.User, input ChannelModelsRequest) ([]string, error) {
|
||||
@@ -54,10 +73,34 @@ func (s *Service) FetchChannelModels(ctx context.Context, actor *model.User, inp
|
||||
}
|
||||
|
||||
// ChannelModelCatalogItem 是前端自定义渠道拉取模型目录后的最小合同;
|
||||
// supportedEndpointTypes 由上游 /models 返回,前端据此推导能力与协议,不依赖模型名猜测。
|
||||
// 协议、能力和可选参数均来自上游公开元数据,不展开供应商内部兼容模型。
|
||||
type ChannelModelCatalogItem struct {
|
||||
ID string `json:"id"`
|
||||
SupportedEndpointTypes []string `json:"supportedEndpointTypes,omitempty"`
|
||||
ID string `json:"id"`
|
||||
DisplayName string `json:"displayName,omitempty"`
|
||||
ModelType string `json:"modelType,omitempty"`
|
||||
SupportedEndpointTypes []string `json:"supportedEndpointTypes,omitempty"`
|
||||
DefaultParameters ChannelModelCatalogDefaultParameters `json:"defaultParameters,omitempty"`
|
||||
Options ChannelModelCatalogOptions `json:"options,omitempty"`
|
||||
SupportsImages *bool `json:"supportsImages,omitempty"`
|
||||
MinImages *int `json:"minImages,omitempty"`
|
||||
MaxImages *int `json:"maxImages,omitempty"`
|
||||
}
|
||||
|
||||
type ChannelModelCatalogDefaultParameters struct {
|
||||
AspectRatio string `json:"aspectRatio,omitempty"`
|
||||
DurationSeconds string `json:"durationSeconds,omitempty"`
|
||||
Resolution string `json:"resolution,omitempty"`
|
||||
}
|
||||
|
||||
type ChannelModelCatalogOptions struct {
|
||||
AspectRatio []ChannelModelCatalogOption `json:"aspectRatio,omitempty"`
|
||||
DurationSeconds []ChannelModelCatalogOption `json:"durationSeconds,omitempty"`
|
||||
Resolution []ChannelModelCatalogOption `json:"resolution,omitempty"`
|
||||
}
|
||||
|
||||
type ChannelModelCatalogOption struct {
|
||||
Value string `json:"value"`
|
||||
Label string `json:"label,omitempty"`
|
||||
}
|
||||
|
||||
func (s *Service) FetchChannelModelCatalog(ctx context.Context, actor *model.User, input ChannelModelsRequest) ([]ChannelModelCatalogItem, error) {
|
||||
@@ -134,7 +177,25 @@ func (s *Service) FetchChannelModelCatalog(ctx context.Context, actor *model.Use
|
||||
continue
|
||||
}
|
||||
seen[name] = true
|
||||
catalog = append(catalog, ChannelModelCatalogItem{ID: name, SupportedEndpointTypes: normalizeCatalogEndpointTypes(item.SupportedEndpointTypes)})
|
||||
catalog = append(catalog, ChannelModelCatalogItem{
|
||||
ID: name,
|
||||
DisplayName: strings.TrimSpace(item.DisplayName),
|
||||
ModelType: normalizeCatalogModelType(item.ModelType),
|
||||
SupportedEndpointTypes: normalizeCatalogEndpointTypes(item.SupportedEndpointTypes),
|
||||
DefaultParameters: ChannelModelCatalogDefaultParameters{
|
||||
AspectRatio: strings.TrimSpace(item.DefaultParameters.AspectRatio),
|
||||
DurationSeconds: strings.TrimSpace(item.DefaultParameters.DurationSeconds),
|
||||
Resolution: strings.TrimSpace(item.DefaultParameters.Resolution),
|
||||
},
|
||||
Options: ChannelModelCatalogOptions{
|
||||
AspectRatio: normalizeCatalogOptions(item.Options.AspectRatio),
|
||||
DurationSeconds: normalizeCatalogOptions(item.Options.DurationSeconds),
|
||||
Resolution: normalizeCatalogOptions(item.Options.Resolution),
|
||||
},
|
||||
SupportsImages: item.SupportsImages,
|
||||
MinImages: item.MinImages,
|
||||
MaxImages: item.MaxImages,
|
||||
})
|
||||
}
|
||||
sort.Slice(catalog, func(left int, right int) bool {
|
||||
return catalog[left].ID < catalog[right].ID
|
||||
@@ -142,6 +203,30 @@ func (s *Service) FetchChannelModelCatalog(ctx context.Context, actor *model.Use
|
||||
return catalog, nil
|
||||
}
|
||||
|
||||
func normalizeCatalogModelType(value string) string {
|
||||
normalized := strings.ToLower(strings.TrimSpace(value))
|
||||
switch normalized {
|
||||
case "text", "image", "video", "audio":
|
||||
return normalized
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeCatalogOptions(options []ChannelModelCatalogOption) []ChannelModelCatalogOption {
|
||||
seen := make(map[string]bool, len(options))
|
||||
normalized := make([]ChannelModelCatalogOption, 0, len(options))
|
||||
for _, option := range options {
|
||||
value := strings.TrimSpace(option.Value)
|
||||
if value == "" || seen[value] {
|
||||
continue
|
||||
}
|
||||
seen[value] = true
|
||||
normalized = append(normalized, ChannelModelCatalogOption{Value: value, Label: strings.TrimSpace(option.Label)})
|
||||
}
|
||||
return normalized
|
||||
}
|
||||
|
||||
func normalizeCatalogEndpointTypes(values []string) []string {
|
||||
seen := make(map[string]bool, len(values))
|
||||
normalized := make([]string, 0, len(values))
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -101,6 +102,66 @@ func TestCustomModelCatalogConsumesAllowLocalChannelAndServerCapability(t *testi
|
||||
}
|
||||
}
|
||||
|
||||
func TestCustomModelCatalogPreservesPublicCapabilityMetadataWithoutCompatibilityIDs(t *testing.T) {
|
||||
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"data":[
|
||||
{"id":"image-fast","display_name":"Image Fast","model_type":"image"},
|
||||
{"id":"image-quality","display_name":"Image Quality","model_type":"image"},
|
||||
{"id":"omni","capability_id":"omni-flash","display_name":"Omni Flash","model_type":"video","supports_images":false,"min_images":0,"max_images":0,"default_parameters":{"aspect_ratio":"16:9","duration_seconds":"10"},"options":{"aspect_ratio":[{"value":"16:9"},{"value":"9:16"}],"duration_seconds":[{"value":"8"},{"value":"10"}]},"compatibility_map":[{"parameters":{"aspect_ratio":"9:16","duration_seconds":"10"},"model_id":"omni_portrait_10s"}]},
|
||||
{"id":"veo-lite","display_name":"Veo Lite","model_type":"video"},
|
||||
{"id":"veo-fast","display_name":"Veo Fast","model_type":"video"},
|
||||
{"id":"veo-quality","display_name":"Veo Quality","model_type":"video"}
|
||||
]}`))
|
||||
}))
|
||||
defer upstream.Close()
|
||||
|
||||
svc := &Service{runtimeCapabilities: RuntimeCapabilities{desktopLocalChannels: true}}
|
||||
catalog, err := svc.FetchChannelModelCatalog(context.Background(), &model.User{ID: "user-1"}, ChannelModelsRequest{
|
||||
BaseURL: upstream.URL, APIKey: "test-key", APIFormat: "openai", AllowLocalChannel: true,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("FetchChannelModelCatalog() error = %v", err)
|
||||
}
|
||||
if len(catalog) != 6 {
|
||||
t.Fatalf("catalog length = %d; want 6", len(catalog))
|
||||
}
|
||||
var omni *ChannelModelCatalogItem
|
||||
for index := range catalog {
|
||||
if catalog[index].ID == "omni" {
|
||||
omni = &catalog[index]
|
||||
break
|
||||
}
|
||||
}
|
||||
if omni == nil {
|
||||
t.Fatal("catalog is missing public omni capability")
|
||||
}
|
||||
if omni.DisplayName != "Omni Flash" || omni.ModelType != "video" {
|
||||
t.Fatalf("omni metadata = %#v", omni)
|
||||
}
|
||||
if omni.DefaultParameters.AspectRatio != "16:9" || omni.DefaultParameters.DurationSeconds != "10" {
|
||||
t.Fatalf("omni defaults = %#v", omni.DefaultParameters)
|
||||
}
|
||||
if got := optionValues(omni.Options.DurationSeconds); len(got) != 2 || got[0] != "8" || got[1] != "10" {
|
||||
t.Fatalf("omni duration options = %#v", got)
|
||||
}
|
||||
encoded, err := json.Marshal(catalog)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.Contains(string(encoded), "omni_portrait_10s") || strings.Contains(string(encoded), "compatibility") {
|
||||
t.Fatalf("ordinary catalog exposed compatibility IDs: %s", encoded)
|
||||
}
|
||||
}
|
||||
|
||||
func optionValues(options []ChannelModelCatalogOption) []string {
|
||||
values := make([]string, 0, len(options))
|
||||
for _, option := range options {
|
||||
values = append(values, option.Value)
|
||||
}
|
||||
return values
|
||||
}
|
||||
|
||||
func TestSystemWorkerRestartReevaluatesStoredLocalFlagAgainstServerCapability(t *testing.T) {
|
||||
db, err := gorm.Open(sqlite.Open("file:"+newID()+"?mode=memory&cache=shared"), &gorm.Config{})
|
||||
if err != nil {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"infinite-canvas/backend/internal/model"
|
||||
@@ -83,3 +84,49 @@ func TestEnrichAPICallLogReadsArkVideoUsage(t *testing.T) {
|
||||
t.Fatalf("EnrichAPICallLog() accepted empty Ark usage: %#v", missing)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnrichAPICallLogReadsChatCompletionStreamUsage(t *testing.T) {
|
||||
log := &model.ApiCallLog{Capability: "text", Path: "/v1/chat/completions"}
|
||||
stream := []byte("data: {\"id\":\"chatcmpl-test\",\"choices\":[{\"delta\":{\"content\":\"ok\"}}]}\n\n" +
|
||||
"data: {\"id\":\"chatcmpl-test\",\"choices\":[],\"usage\":{\"prompt_tokens\":23,\"completion_tokens\":7,\"prompt_tokens_details\":{\"cached_tokens\":5}}}\n\n" +
|
||||
"data: [DONE]\n\n")
|
||||
(&Service{}).EnrichAPICallLog(log, stream)
|
||||
if !log.UsageAvailable || log.InputTokens != 23 || log.OutputTokens != 7 || log.CachedTokens != 5 {
|
||||
t.Fatalf("EnrichAPICallLog() = %#v", log)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnrichAPICallLogReadsResponsesStreamUsage(t *testing.T) {
|
||||
log := &model.ApiCallLog{Capability: "text", Path: "/v1/responses"}
|
||||
stream := []byte("event: response.output_text.delta\n" +
|
||||
"data: {\"type\":\"response.output_text.delta\",\"delta\":\"ok\"}\n\n" +
|
||||
"event: response.completed\n" +
|
||||
"data: {\"type\":\"response.completed\",\"response\":{\"id\":\"resp-test\",\"status\":\"completed\",\"usage\":{\"input_tokens\":31,\"output_tokens\":11,\"input_tokens_details\":{\"cached_tokens\":9}}}}\n\n")
|
||||
(&Service{}).EnrichAPICallLog(log, stream)
|
||||
if !log.UsageAvailable || log.InputTokens != 31 || log.OutputTokens != 11 || log.CachedTokens != 9 || log.ProviderRequestID != "resp-test" {
|
||||
t.Fatalf("EnrichAPICallLog() = %#v", log)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnrichAPICallLogRejectsEmptyTextUsage(t *testing.T) {
|
||||
log := &model.ApiCallLog{Capability: "text", Path: "/v1/chat/completions"}
|
||||
(&Service{}).EnrichAPICallLog(log, []byte(`{"usage":{}}`))
|
||||
if log.UsageAvailable {
|
||||
t.Fatalf("EnrichAPICallLog() accepted empty text usage: %#v", log)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureChatCompletionStreamUsageRequest(t *testing.T) {
|
||||
data, err := EnsureChatCompletionStreamUsageRequest([]byte(`{"model":"gpt-test","stream":true,"stream_options":{"custom":true}}`))
|
||||
if err != nil {
|
||||
t.Fatalf("EnsureChatCompletionStreamUsageRequest() error = %v", err)
|
||||
}
|
||||
var payload map[string]any
|
||||
if err := json.Unmarshal(data, &payload); err != nil {
|
||||
t.Fatalf("json.Unmarshal() error = %v", err)
|
||||
}
|
||||
options, _ := payload["stream_options"].(map[string]any)
|
||||
if options["include_usage"] != true || options["custom"] != true {
|
||||
t.Fatalf("stream_options = %#v", options)
|
||||
}
|
||||
}
|
||||
@@ -67,6 +67,7 @@ type VideoCapabilityConfig struct {
|
||||
|
||||
type VideoReferenceConfig struct {
|
||||
PromptMaxChars int `json:"promptMaxChars"`
|
||||
MinImages int `json:"minImages"`
|
||||
MaxImages int `json:"maxImages"`
|
||||
MaxImageBytes int64 `json:"maxImageBytes"`
|
||||
MaxVideos int `json:"maxVideos"`
|
||||
@@ -154,11 +155,11 @@ func defaultImageSizeValues() []string {
|
||||
|
||||
func DefaultModelCapabilityConfigForModel(protocol string, modelName string) *ModelCapabilityConfig {
|
||||
video := &VideoCapabilityConfig{
|
||||
References: VideoReferenceConfig{PromptMaxChars: 1000, MaxImages: 9, MaxImageBytes: 30 * 1024 * 1024, MaxVideos: 0, MaxVideoBytes: 0, MaxVideoDuration: 0, MaxAudios: 0, MaxAudioBytes: 0, MaxAudioDuration: 0},
|
||||
References: VideoReferenceConfig{PromptMaxChars: 1000, MinImages: 0, MaxImages: 9, MaxImageBytes: 30 * 1024 * 1024, MaxVideos: 0, MaxVideoBytes: 0, MaxVideoDuration: 0, MaxAudios: 0, MaxAudioBytes: 0, MaxAudioDuration: 0},
|
||||
Duration: VideoDurationConfig{Selection: "range", Min: 1, Max: 15, Step: 1, Default: 6},
|
||||
Ratios: []string{"16:9", "9:16", "1:1", "4:3", "3:4", "21:9"},
|
||||
DefaultRatio: "16:9",
|
||||
Resolutions: []string{"480p", "720p", "1080p", "2160p"},
|
||||
Resolutions: []string{"480p", "720p", "1080p", "1440p", "2160p"},
|
||||
DefaultResolution: "720p",
|
||||
GenerateAudio: VideoBooleanConfig{Supported: false, Default: false},
|
||||
Watermark: VideoBooleanConfig{Supported: false, Default: false},
|
||||
@@ -178,11 +179,15 @@ func DefaultModelCapabilityConfigForModel(protocol string, modelName string) *Mo
|
||||
video.References.MaxVideoDuration, video.References.MaxAudioDuration = 15, 15
|
||||
video.GenerateAudio = VideoBooleanConfig{Supported: true, Default: true}
|
||||
video.Watermark = VideoBooleanConfig{Supported: true, Default: false}
|
||||
video.Resolutions = []string{"480p", "720p", "1080p"}
|
||||
case model.ChannelInterfaceNewAPIChannel1, model.ChannelInterfaceNewAPIChannel2:
|
||||
video.References.MaxVideos, video.References.MaxAudios = 3, 3
|
||||
video.References.MaxVideoBytes, video.References.MaxAudioBytes = 200*1024*1024, 15*1024*1024
|
||||
video.References.MaxVideoDuration, video.References.MaxAudioDuration = 15, 15
|
||||
video.GenerateAudio = VideoBooleanConfig{Supported: true, Default: true}
|
||||
if model.ChannelInterfaceType(protocol) == model.ChannelInterfaceNewAPIChannel1 {
|
||||
video.Resolutions = []string{"480p", "720p", "1080p"}
|
||||
}
|
||||
case model.ChannelInterfaceNewAPIVideo, model.ChannelInterfaceXAIVideo:
|
||||
video.GenerateAudio = VideoBooleanConfig{Supported: false, Default: false}
|
||||
case model.ChannelInterfaceNovitaVideo:
|
||||
@@ -273,11 +278,14 @@ func validateVideoCapabilityConfig(value *VideoCapabilityConfig) error {
|
||||
if value.References.PromptMaxChars < 1 || value.References.PromptMaxChars > 1000000 {
|
||||
return BadAuthRequest("提示词最大字符数必须在 1-1000000 之间")
|
||||
}
|
||||
for name, number := range map[string]int{"最大图片引用数": value.References.MaxImages, "最大视频引用数": value.References.MaxVideos, "最大音频引用数": value.References.MaxAudios} {
|
||||
for name, number := range map[string]int{"最少图片引用数": value.References.MinImages, "最大图片引用数": value.References.MaxImages, "最大视频引用数": value.References.MaxVideos, "最大音频引用数": value.References.MaxAudios} {
|
||||
if number < 0 || number > 100 {
|
||||
return BadAuthRequest(name + "必须在 0-100 之间")
|
||||
}
|
||||
}
|
||||
if value.References.MinImages > value.References.MaxImages {
|
||||
return BadAuthRequest("最少图片引用数不能超过最大图片引用数")
|
||||
}
|
||||
if value.References.MaxImageBytes < 0 || value.References.MaxVideoBytes < 0 || value.References.MaxAudioBytes < 0 || value.References.MaxVideoDuration < 0 || value.References.MaxAudioDuration < 0 {
|
||||
return BadAuthRequest("引用素材限制不能小于 0")
|
||||
}
|
||||
@@ -287,8 +295,12 @@ func validateVideoCapabilityConfig(value *VideoCapabilityConfig) error {
|
||||
if len(value.Ratios) == 0 || strings.TrimSpace(value.DefaultRatio) == "" || !containsCapabilityString(value.Ratios, value.DefaultRatio) {
|
||||
return BadAuthRequest("请至少配置一个画面比例,并选择默认比例")
|
||||
}
|
||||
if len(value.Resolutions) == 0 || strings.TrimSpace(value.DefaultResolution) == "" || !containsCapabilityString(value.Resolutions, value.DefaultResolution) {
|
||||
return BadAuthRequest("请至少配置一个输出分辨率,并选择默认分辨率")
|
||||
if len(value.Resolutions) == 0 {
|
||||
if strings.TrimSpace(value.DefaultResolution) != "" {
|
||||
return BadAuthRequest("未配置输出分辨率时不能设置默认分辨率")
|
||||
}
|
||||
} else if strings.TrimSpace(value.DefaultResolution) == "" || !containsCapabilityString(value.Resolutions, value.DefaultResolution) {
|
||||
return BadAuthRequest("默认输出分辨率必须属于支持值")
|
||||
}
|
||||
if len(value.Operations) == 0 || strings.TrimSpace(value.DefaultOperation) == "" || !containsCapabilityString(value.Operations, value.DefaultOperation) {
|
||||
return BadAuthRequest("请至少配置一个生成模式,并选择默认模式")
|
||||
@@ -373,6 +385,9 @@ func validateVideoTask(profile *VideoCapabilityConfig, input canvasGenerationInp
|
||||
if len(input.ReferenceImages) > profile.References.MaxImages || len(input.ReferenceVideos) > profile.References.MaxVideos || len(input.ReferenceAudios) > profile.References.MaxAudios {
|
||||
return BadAuthRequest("参考素材数量超过当前模型限制")
|
||||
}
|
||||
if len(input.ReferenceImages) < profile.References.MinImages {
|
||||
return BadAuthRequest(fmt.Sprintf("当前视频模型至少需要 %d 张参考图", profile.References.MinImages))
|
||||
}
|
||||
for _, media := range input.ReferenceImages {
|
||||
if profile.References.MaxImageBytes > 0 && media.Bytes > profile.References.MaxImageBytes {
|
||||
return BadAuthRequest("参考图片文件超过当前模型大小限制")
|
||||
@@ -401,7 +416,7 @@ func validateVideoTask(profile *VideoCapabilityConfig, input canvasGenerationInp
|
||||
if input.Config.Size != "" && !videoRatioAllowed(profile.Ratios, input.Config.Size) {
|
||||
return BadAuthRequest("画面比例不在当前模型支持范围内")
|
||||
}
|
||||
if input.Config.VQuality != "" && !containsCapabilityString(profile.Resolutions, normalizeResolution(input.Config.VQuality)) {
|
||||
if len(profile.Resolutions) > 0 && !isAutomaticVideoResolution(input.Config.VQuality) && videoResolutionNameRequest(profile, input.Config.VQuality) == "" {
|
||||
return BadAuthRequest("输出分辨率不在当前模型支持范围内")
|
||||
}
|
||||
operation := metadataString(input.Metadata, "videoEditOperation")
|
||||
@@ -536,6 +551,9 @@ func ratioValue(value string) float64 {
|
||||
func normalizeResolution(value string) string {
|
||||
value = strings.ToLower(strings.TrimSpace(value))
|
||||
value = strings.TrimSuffix(value, "p")
|
||||
if value == "2k" {
|
||||
return "1440p"
|
||||
}
|
||||
if value == "4k" {
|
||||
return "2160p"
|
||||
}
|
||||
|
||||
@@ -62,3 +62,86 @@ func TestValidateImageTaskEnforcesGPTImage2CustomSizeLimits(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultVideoCapabilityUsesProtocolSpecificResolutionTiers(t *testing.T) {
|
||||
tests := map[string][]string{
|
||||
"newapi-channel-2": {"480p", "720p", "1080p", "1440p", "2160p"},
|
||||
"volcengine-ark-video": {"480p", "720p", "1080p"},
|
||||
"volcengine-jimeng-video": {"720p"},
|
||||
"gemini-veo": {"720p", "1080p"},
|
||||
}
|
||||
for protocol, want := range tests {
|
||||
t.Run(protocol, func(t *testing.T) {
|
||||
profile := DefaultModelCapabilityConfigForModel(protocol, "")
|
||||
if profile == nil || profile.Video == nil {
|
||||
t.Fatalf("DefaultModelCapabilityConfigForModel(%q) video profile = nil", protocol)
|
||||
}
|
||||
if fmt.Sprint(profile.Video.Resolutions) != fmt.Sprint(want) {
|
||||
t.Fatalf("resolutions = %v, want %v", profile.Video.Resolutions, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeResolutionSupportsCommonAliases(t *testing.T) {
|
||||
tests := map[string]string{
|
||||
"1440": "1440p",
|
||||
"1440p": "1440p",
|
||||
"2K": "1440p",
|
||||
"4K": "2160p",
|
||||
"768P": "768p",
|
||||
}
|
||||
for input, want := range tests {
|
||||
if got := normalizeResolution(input); got != want {
|
||||
t.Fatalf("normalizeResolution(%q) = %q, want %q", input, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateVideoTaskIgnoresGlobalResolutionWhenCatalogDeclaresNone(t *testing.T) {
|
||||
profile := DefaultModelCapabilityConfigForModel("newapi", "omni").Video
|
||||
profile.Duration = VideoDurationConfig{Selection: "enum", Values: []int{8, 10}, Default: 10}
|
||||
profile.Ratios = []string{"16:9", "9:16"}
|
||||
profile.DefaultRatio = "16:9"
|
||||
profile.Resolutions = nil
|
||||
profile.DefaultResolution = ""
|
||||
profile.References.MaxImages = 0
|
||||
profile.Operations = []string{"text_to_video"}
|
||||
profile.DefaultOperation = "text_to_video"
|
||||
|
||||
err := validateVideoTask(profile, canvasGenerationInput{
|
||||
Config: providerConfig{Model: "omni", VideoSeconds: "10", Size: "16:9", VQuality: "720"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("validateVideoTask() error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeVideoCapabilityAllowsOmittedResolution(t *testing.T) {
|
||||
profile := DefaultModelCapabilityConfigForModel("newapi-channel-2", "endpoint-video").Video
|
||||
profile.Resolutions = nil
|
||||
profile.DefaultResolution = ""
|
||||
|
||||
result, err := NormalizeModelCapabilityConfig("video", "newapi-channel-2", &ModelCapabilityConfig{Version: 1, Video: profile})
|
||||
if err != nil {
|
||||
t.Fatalf("NormalizeModelCapabilityConfig() error = %v", err)
|
||||
}
|
||||
if result.Video == nil || len(result.Video.Resolutions) != 0 || result.Video.DefaultResolution != "" {
|
||||
t.Fatalf("normalized video resolution = %#v", result.Video)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateVideoTaskRequiresDeclaredMinimumImages(t *testing.T) {
|
||||
profile := DefaultModelCapabilityConfigForModel("newapi-channel-2", "image-required-video").Video
|
||||
profile.References.MinImages = 1
|
||||
profile.References.MaxImages = 2
|
||||
profile.Operations = []string{"image_to_video"}
|
||||
profile.DefaultOperation = "image_to_video"
|
||||
|
||||
err := validateVideoTask(profile, canvasGenerationInput{
|
||||
Config: providerConfig{Model: "image-required-video", VideoSeconds: "6", Size: "16:9", VQuality: "720"},
|
||||
})
|
||||
if err == nil || !strings.Contains(err.Error(), "至少需要 1 张参考图") {
|
||||
t.Fatalf("validateVideoTask() error = %v", err)
|
||||
}
|
||||
}
|
||||
@@ -15,12 +15,14 @@ type CreateProjectAssetFolderRequest struct {
|
||||
Name string `json:"name"`
|
||||
ParentID string `json:"parentId"`
|
||||
Style string `json:"style"`
|
||||
Theme string `json:"theme"`
|
||||
}
|
||||
|
||||
type UpdateProjectAssetFolderRequest struct {
|
||||
Name *string `json:"name"`
|
||||
ParentID *string `json:"parentId"`
|
||||
Style *string `json:"style"`
|
||||
Theme *string `json:"theme"`
|
||||
}
|
||||
|
||||
func (s *Service) ProjectAssetFolders(userID string, projectID string) ([]model.ProjectAssetFolder, error) {
|
||||
@@ -55,7 +57,11 @@ func (s *Service) CreateProjectAssetFolder(userID string, projectID string, req
|
||||
if err != nil {
|
||||
return model.ProjectAssetFolder{}, err
|
||||
}
|
||||
folder := model.ProjectAssetFolder{ID: newID(), ProjectID: projectID, ParentID: parentID, Name: name, NameKey: projectAssetFolderNameKey(name), Style: style, Position: position, CreatedAt: now, UpdatedAt: now}
|
||||
theme, err := validateProjectAssetFolderTheme(req.Theme)
|
||||
if err != nil {
|
||||
return model.ProjectAssetFolder{}, err
|
||||
}
|
||||
folder := model.ProjectAssetFolder{ID: newID(), ProjectID: projectID, ParentID: parentID, Name: name, NameKey: projectAssetFolderNameKey(name), Style: style, Theme: theme, Position: position, CreatedAt: now, UpdatedAt: now}
|
||||
if err := s.repo.CreateProjectAssetFolder(&folder); err != nil {
|
||||
if isProjectAssetFolderNameConflict(err) {
|
||||
return model.ProjectAssetFolder{}, BadAuthRequest("同级目录下已存在同名文件夹")
|
||||
@@ -102,6 +108,13 @@ func (s *Service) UpdateProjectAssetFolder(userID string, projectID string, fold
|
||||
}
|
||||
folder.Style = style
|
||||
}
|
||||
if req.Theme != nil {
|
||||
theme, themeErr := validateProjectAssetFolderTheme(*req.Theme)
|
||||
if themeErr != nil {
|
||||
return model.ProjectAssetFolder{}, themeErr
|
||||
}
|
||||
folder.Theme = theme
|
||||
}
|
||||
if projectAssetFolderNameExists(folders, folder.ParentID, folder.Name, folder.ID) {
|
||||
return model.ProjectAssetFolder{}, BadAuthRequest("同级目录下已存在同名文件夹")
|
||||
}
|
||||
@@ -150,6 +163,19 @@ func validateProjectAssetFolderStyle(value string) (string, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func validateProjectAssetFolderTheme(value string) (string, error) {
|
||||
theme := strings.TrimSpace(value)
|
||||
if theme == "" {
|
||||
return "aurora", nil
|
||||
}
|
||||
switch theme {
|
||||
case "aurora", "obsidian", "ember", "pearl":
|
||||
return theme, nil
|
||||
default:
|
||||
return "", BadAuthRequest("不支持的文件夹主题")
|
||||
}
|
||||
}
|
||||
|
||||
func validateProjectAssetFolderName(value string) (string, error) {
|
||||
name := strings.TrimSpace(value)
|
||||
if name == "" {
|
||||
|
||||
@@ -48,3 +48,15 @@ func TestProjectAssetFolderNameIsUniqueWithinSiblingOnly(t *testing.T) {
|
||||
t.Fatal("did not expect a duplicate across different parents")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateProjectAssetFolderTheme(t *testing.T) {
|
||||
if theme, err := validateProjectAssetFolderTheme(""); err != nil || theme != "aurora" {
|
||||
t.Fatalf("expected empty theme to use aurora, got %q: %v", theme, err)
|
||||
}
|
||||
if theme, err := validateProjectAssetFolderTheme(" pearl "); err != nil || theme != "pearl" {
|
||||
t.Fatalf("expected pearl theme, got %q: %v", theme, err)
|
||||
}
|
||||
if _, err := validateProjectAssetFolderTheme("content-image"); err == nil {
|
||||
t.Fatal("expected arbitrary content image theme to fail")
|
||||
}
|
||||
}
|
||||
@@ -113,6 +113,7 @@ type providerAnalyticsContext struct {
|
||||
UserID string
|
||||
TaskID string
|
||||
BillingOrderID string
|
||||
BillingMode string
|
||||
Capability string
|
||||
Operation string
|
||||
ChannelID string
|
||||
@@ -125,6 +126,12 @@ type providerAnalyticsContext struct {
|
||||
|
||||
func withProviderAnalytics(ctx context.Context, service *Service, task model.Task) context.Context {
|
||||
metadata := providerAnalyticsContext{Service: service, UserID: task.UserID, TaskID: task.ID, BillingOrderID: task.BillingOrderID, Capability: capabilityFromTaskType(task.Type), Operation: task.Operation, Model: task.Model, ProviderRequestID: task.ProviderRequestID}
|
||||
// 账单模式随请求上下文传递,流式协议据此只为 Token 计费开启 usage 终态块。
|
||||
if service != nil && task.BillingOrderID != "" {
|
||||
if order, err := service.repo.BillingOrder(task.BillingOrderID); err == nil {
|
||||
metadata.BillingMode = order.BillingMode
|
||||
}
|
||||
}
|
||||
var input struct {
|
||||
Mode string `json:"mode"`
|
||||
Config providerConfig `json:"config"`
|
||||
@@ -874,18 +881,49 @@ func runVolcengineArkImageTask(ctx context.Context, input canvasGenerationInput)
|
||||
if err := postJSON(ctx, input.Config, "/images/generations", body, &payload); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
images, err := imageDataURLs(payload)
|
||||
images, err := volcengineArkImageDataURLs(ctx, input.Config, payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]interface{}{"mode": "image", "images": images}, nil
|
||||
}
|
||||
|
||||
func volcengineArkImageDataURLs(ctx context.Context, config providerConfig, payload imageResponse) ([]map[string]string, error) {
|
||||
images, err := imageDataURLs(payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, image := range images {
|
||||
value := strings.TrimSpace(image["dataUrl"])
|
||||
if strings.HasPrefix(value, "data:image/") {
|
||||
continue
|
||||
}
|
||||
if !isPublicMediaURL(value) {
|
||||
return nil, errors.New("火山方舟图片接口没有返回可下载的图片")
|
||||
}
|
||||
// 方舟默认返回临时 CDN 地址。必须由后端下载成内联结果,后续资源持久化才能
|
||||
// 原子地写入服务器或用户配置的对象存储,且不依赖浏览器跨域访问方舟 CDN。
|
||||
data, mimeType, err := getProviderExternalBinary(withProviderRequestKind(ctx, "download"), config, value)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("火山方舟图片结果下载失败:%w", err)
|
||||
}
|
||||
detected := strings.ToLower(strings.TrimSpace(strings.Split(http.DetectContentType(data), ";")[0]))
|
||||
mimeType = strings.ToLower(normalizedMediaMimeType(mimeType, data))
|
||||
if len(data) == 0 || strings.Contains(detected, "json") || strings.HasPrefix(detected, "text/") || !strings.HasPrefix(mimeType, "image/") {
|
||||
return nil, fmt.Errorf("火山方舟图片结果无效:%s", defaultString(detected, mimeType))
|
||||
}
|
||||
image["dataUrl"] = dataURL(mimeType, data)
|
||||
image["mimeType"] = mimeType
|
||||
}
|
||||
return images, nil
|
||||
}
|
||||
|
||||
func volcengineArkImageBody(input canvasGenerationInput) (map[string]interface{}, error) {
|
||||
body := map[string]interface{}{
|
||||
"model": input.Config.Model,
|
||||
"prompt": withSystemPrompt(input.Config, input.Prompt),
|
||||
"n": 1,
|
||||
"model": input.Config.Model,
|
||||
"prompt": withSystemPrompt(input.Config, input.Prompt),
|
||||
"n": 1,
|
||||
"watermark": false,
|
||||
}
|
||||
if key, value := imageSizeParameter(input.ImageCapability, input.Config.Size); value != "" {
|
||||
if key == "size" {
|
||||
@@ -1413,7 +1451,9 @@ func runVideoTask(ctx context.Context, input canvasGenerationInput) (map[string]
|
||||
if size := normalizeVideoSize(input.Config.Size); size != "" {
|
||||
writeField(writer, "size", size)
|
||||
}
|
||||
writeField(writer, "resolution_name", normalizeVideoResolution(input.Config.VQuality))
|
||||
if resolution := videoResolutionNameRequest(input.VideoCapability, input.Config.VQuality); resolution != "" {
|
||||
writeField(writer, "resolution_name", resolution)
|
||||
}
|
||||
writeField(writer, "preset", "normal")
|
||||
if shouldSendNewAPIVideoImages(input) {
|
||||
for _, image := range input.ReferenceImages {
|
||||
@@ -1933,7 +1973,10 @@ func newAPIChannel2VideoRequestBody(input canvasGenerationInput) (newAPIVideoReq
|
||||
seconds = 6
|
||||
}
|
||||
ratio := normalizeNewAPIChannel2Ratio(input.Config.Size, modelName)
|
||||
resolution := normalizeNewAPIChannel2Resolution(input.Config.VQuality, modelName)
|
||||
resolution := videoResolutionNameRequest(input.VideoCapability, input.Config.VQuality)
|
||||
if modelName == "grok-video-1.5-1080p" {
|
||||
resolution = "1080p"
|
||||
}
|
||||
body := newAPIVideoRequest{
|
||||
Model: input.Config.Model,
|
||||
Prompt: strings.TrimSpace(input.Prompt),
|
||||
@@ -2025,16 +2068,22 @@ func normalizeNewAPIChannel2Resolution(value string, modelName string) string {
|
||||
if modelName == "grok-video-1.5-1080p" {
|
||||
return "1080p"
|
||||
}
|
||||
switch strings.ToLower(strings.TrimSpace(value)) {
|
||||
normalized := strings.ToLower(strings.TrimSpace(value))
|
||||
switch normalized {
|
||||
case "480", "480p", "low":
|
||||
return "480p"
|
||||
case "1080", "1080p":
|
||||
return "1080p"
|
||||
case "1440", "1440p", "2k":
|
||||
return "1440p"
|
||||
case "2160", "2160p", "4k":
|
||||
return "2160p"
|
||||
default:
|
||||
return "720p"
|
||||
}
|
||||
numeric := strings.TrimSuffix(normalized, "p")
|
||||
if resolution, err := strconv.Atoi(numeric); err == nil && resolution > 0 {
|
||||
return strconv.Itoa(resolution) + "p"
|
||||
}
|
||||
return "720p"
|
||||
}
|
||||
|
||||
func runNewAPIChannel1VideoTask(ctx context.Context, input canvasGenerationInput) (map[string]interface{}, error) {
|
||||
@@ -2411,6 +2460,12 @@ func runSeedanceAgentPlanVideoTask(ctx context.Context, input canvasGenerationIn
|
||||
|
||||
func requestTextProvider(ctx context.Context, config providerConfig, path string, body map[string]interface{}, protocol string, stream bool) (string, error) {
|
||||
if stream {
|
||||
metadata, _ := ctx.Value(providerAnalyticsKey{}).(providerAnalyticsContext)
|
||||
if protocol == "chat-completion" && metadata.BillingMode == "token" {
|
||||
if err := ensureChatCompletionStreamUsage(body); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
return postStreamingText(ctx, config, path, body, protocol)
|
||||
}
|
||||
var payload map[string]interface{}
|
||||
|
||||
@@ -19,7 +19,7 @@ type newAPIVideoRequest struct {
|
||||
Prompt string `json:"prompt"`
|
||||
Seconds string `json:"seconds"`
|
||||
AspectRatio string `json:"aspect_ratio"`
|
||||
Resolution string `json:"resolution"`
|
||||
Resolution string `json:"resolution,omitempty"`
|
||||
GenerateAudio *bool `json:"generate_audio,omitempty"`
|
||||
ImageURLs []string `json:"image_urls,omitempty"`
|
||||
VideoURLs []string `json:"video_urls,omitempty"`
|
||||
|
||||
@@ -116,6 +116,22 @@ func TestProviderHTTPErrorWarnsAboutUncertain524Billing(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeNewAPIChannel2ResolutionPreservesDeclaredTiers(t *testing.T) {
|
||||
tests := map[string]string{
|
||||
"1440": "1440p",
|
||||
"2k": "1440p",
|
||||
"4K": "2160p",
|
||||
"768p": "768p",
|
||||
}
|
||||
for input, want := range tests {
|
||||
t.Run(input, func(t *testing.T) {
|
||||
if got := normalizeNewAPIChannel2Resolution(input, "custom-video-model"); got != want {
|
||||
t.Fatalf("normalizeNewAPIChannel2Resolution(%q) = %q, want %q", input, got, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestVolcengineArkImageBodyUsesJSONReferencesAndDownscalesSize(t *testing.T) {
|
||||
body, err := volcengineArkImageBody(canvasGenerationInput{
|
||||
Prompt: "combine the references",
|
||||
@@ -135,6 +151,9 @@ func TestVolcengineArkImageBodyUsesJSONReferencesAndDownscalesSize(t *testing.T)
|
||||
if body["prompt"] != "keep the subject\n\ncombine the references" {
|
||||
t.Fatalf("prompt = %q", body["prompt"])
|
||||
}
|
||||
if watermark, ok := body["watermark"].(bool); !ok || watermark {
|
||||
t.Fatalf("watermark = %#v, want false", body["watermark"])
|
||||
}
|
||||
size, _ := body["size"].(string)
|
||||
parts := strings.Split(size, "x")
|
||||
if len(parts) != 2 {
|
||||
@@ -168,6 +187,40 @@ func TestVolcengineArkImageBodyUpscalesPresetBelowMinimumPixels(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestVolcengineArkImageDataURLsDownloadsRemoteResult(t *testing.T) {
|
||||
t.Setenv("CANVAS_ALLOW_PRIVATE_UPSTREAMS", "true")
|
||||
imageBytes := []byte{0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00}
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.Header().Set("Content-Type", "image/png")
|
||||
_, _ = w.Write(imageBytes)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
images, err := volcengineArkImageDataURLs(context.Background(), providerConfig{}, imageResponse{
|
||||
Data: []map[string]interface{}{{"url": server.URL + "/generated.png"}},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("volcengineArkImageDataURLs() error = %v", err)
|
||||
}
|
||||
if len(images) != 1 || !strings.HasPrefix(images[0]["dataUrl"], "data:image/png;base64,") {
|
||||
t.Fatalf("images = %#v", images)
|
||||
}
|
||||
|
||||
svc := newResourceTestService(t)
|
||||
stored, err := svc.persistGeneratedMediaResult("user-1", map[string]interface{}{"mode": "image", "images": images})
|
||||
if err != nil {
|
||||
t.Fatalf("persistGeneratedMediaResult() error = %v", err)
|
||||
}
|
||||
storedImages, ok := stored["images"].([]interface{})
|
||||
if !ok || len(storedImages) != 1 {
|
||||
t.Fatalf("stored images = %#v", stored["images"])
|
||||
}
|
||||
storedImage, ok := storedImages[0].(map[string]interface{})
|
||||
if !ok || !strings.HasPrefix(stringField(storedImage, "storageKey"), "resource:") || !strings.HasPrefix(stringField(storedImage, "dataUrl"), "/api/resources/") {
|
||||
t.Fatalf("stored image = %#v", storedImages[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestVolcengineArkImageRejectsMaskBeforeRequest(t *testing.T) {
|
||||
_, err := runImageTask(context.Background(), canvasGenerationInput{
|
||||
Prompt: "edit only the masked area",
|
||||
@@ -624,6 +677,64 @@ func TestRunVideoTaskUsesNewAPIForAnyVideoModel(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunVideoTaskSendsOnlyDeclaredResolutionName(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
resolutions []string
|
||||
quality string
|
||||
withoutProfile bool
|
||||
want string
|
||||
}{
|
||||
{name: "catalog omits resolution capability", quality: "720"},
|
||||
{name: "auto never invents 720p", resolutions: []string{"720p", "1080p"}, quality: "auto"},
|
||||
{name: "legacy auto never invents 720p", quality: "auto", withoutProfile: true},
|
||||
{name: "missing profile explicit 720 never invents resolution", quality: "720", withoutProfile: true},
|
||||
{name: "declared HD resolution", resolutions: []string{"1080p"}, quality: "1080", want: "1080p"},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
t.Setenv("CANVAS_ALLOW_PRIVATE_UPSTREAMS", "true")
|
||||
var got string
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method + " " + r.URL.Path {
|
||||
case "POST /v1/videos":
|
||||
if err := r.ParseMultipartForm(1 << 20); err != nil {
|
||||
t.Fatalf("ParseMultipartForm() error = %v", err)
|
||||
}
|
||||
got = r.FormValue("resolution_name")
|
||||
_, _ = w.Write([]byte(`{"id":"video-resolution","status":"queued"}`))
|
||||
case "GET /v1/videos/video-resolution":
|
||||
_, _ = w.Write([]byte(`{"id":"video-resolution","status":"completed"}`))
|
||||
case "GET /v1/videos/video-resolution/content":
|
||||
w.Header().Set("Content-Type", "video/mp4")
|
||||
_, _ = w.Write([]byte("video"))
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
profile := DefaultModelCapabilityConfigForModel("newapi", "public-video").Video
|
||||
profile.Resolutions = test.resolutions
|
||||
profile.DefaultResolution = ""
|
||||
if test.withoutProfile {
|
||||
profile = nil
|
||||
}
|
||||
_, err := runVideoTask(context.Background(), canvasGenerationInput{
|
||||
Prompt: "synthetic prompt",
|
||||
Config: providerConfig{BaseURL: server.URL + "/v1", APIKey: "test-key", Model: "public-video", VideoSeconds: "8", Size: "16:9", VQuality: test.quality},
|
||||
VideoCapability: profile,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("runVideoTask() error = %v", err)
|
||||
}
|
||||
if got != test.want {
|
||||
t.Fatalf("resolution_name = %q; want %q", got, test.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunVideoTaskUsesNestedURLBeforeResultURL(t *testing.T) {
|
||||
t.Setenv("CANVAS_ALLOW_PRIVATE_UPSTREAMS", "true")
|
||||
paths := make([]string, 0, 3)
|
||||
@@ -1167,9 +1278,12 @@ func TestRunNewAPIChannel2VideoTaskDownloadsTemporaryResult(t *testing.T) {
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
profile := DefaultModelCapabilityConfigForModel("newapi-channel-2", "grok-image-video").Video
|
||||
profile.Resolutions = []string{"720p"}
|
||||
profile.DefaultResolution = "720p"
|
||||
result, err := runVideoTask(context.Background(), canvasGenerationInput{
|
||||
Prompt: "make it move",
|
||||
Config: providerConfig{BaseURL: server.URL, APIKey: "test-key", Model: "grok-image-video", InterfaceType: "newapi-channel-2", VideoSeconds: "15", Size: "720x1280", VQuality: "high"},
|
||||
Config: providerConfig{BaseURL: server.URL, APIKey: "test-key", Model: "grok-image-video", InterfaceType: "newapi-channel-2", VideoSeconds: "15", Size: "720x1280", VQuality: "720"},
|
||||
ReferenceImages: []providerMedia{
|
||||
{ID: "image-1", DataURL: testReferenceImageDataURL},
|
||||
{ID: "image-2", DataURL: testReferenceImageDataURL},
|
||||
@@ -1177,6 +1291,7 @@ func TestRunNewAPIChannel2VideoTaskDownloadsTemporaryResult(t *testing.T) {
|
||||
ReferenceVideos: []providerMedia{{ID: "video-1", URL: server.URL + "/reference.mp4"}},
|
||||
ReferenceAudios: []providerMedia{{ID: "audio-1", URL: server.URL + "/reference.mp3"}},
|
||||
Metadata: map[string]interface{}{"videoEditOperation": "image_to_video"},
|
||||
VideoCapability: profile,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("runVideoTask() error = %v", err)
|
||||
@@ -1243,6 +1358,50 @@ func TestNewAPIChannel2SingleImageModelsRequireOneReference(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewAPIChannel2SendsOnlyDeclaredResolution(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
model string
|
||||
quality string
|
||||
resolutions []string
|
||||
want string
|
||||
}{
|
||||
{name: "catalog omits resolution", model: "endpoint-video", quality: "720"},
|
||||
{name: "declared 2K alias", model: "declared-video", quality: "2K", resolutions: []string{"1440p"}, want: "1440p"},
|
||||
{name: "fixed grok 1080p model", model: "grok-video-1.5-1080p", quality: "720", want: "1080p"},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
profile := DefaultModelCapabilityConfigForModel("newapi-channel-2", test.model).Video
|
||||
profile.Resolutions = test.resolutions
|
||||
profile.DefaultResolution = ""
|
||||
input := canvasGenerationInput{
|
||||
Config: providerConfig{Model: test.model, VideoSeconds: "6", Size: "16:9", VQuality: test.quality},
|
||||
VideoCapability: profile,
|
||||
}
|
||||
if strings.HasPrefix(test.model, "grok-video-1.5") {
|
||||
input.ReferenceImages = []providerMedia{{ID: "image-1", DataURL: testReferenceImageDataURL}}
|
||||
}
|
||||
|
||||
body, err := newAPIChannel2VideoRequestBody(input)
|
||||
if err != nil {
|
||||
t.Fatalf("newAPIChannel2VideoRequestBody() error = %v", err)
|
||||
}
|
||||
if body.Resolution != test.want {
|
||||
t.Fatalf("resolution = %q, want %q", body.Resolution, test.want)
|
||||
}
|
||||
mapped, err := requestAsMap(body)
|
||||
if err != nil {
|
||||
t.Fatalf("requestAsMap() error = %v", err)
|
||||
}
|
||||
_, hasResolution := mapped["resolution"]
|
||||
if hasResolution != (test.want != "") {
|
||||
t.Fatalf("resolution presence = %v, want %v; body = %#v", hasResolution, test.want != "", mapped)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewAPIChannel2RejectsAudioWithoutReferenceVideo(t *testing.T) {
|
||||
_, err := newAPIChannel2VideoRequestBody(canvasGenerationInput{
|
||||
Config: providerConfig{Model: "grok-image-video", VideoSeconds: "6"},
|
||||
|
||||
@@ -160,12 +160,52 @@ func normalizeVideoResolution(value string) string {
|
||||
if strings.EqualFold(value, "4k") {
|
||||
return "2160p"
|
||||
}
|
||||
if strings.EqualFold(value, "2k") {
|
||||
return "1440p"
|
||||
}
|
||||
if strings.HasSuffix(value, "p") {
|
||||
return value
|
||||
}
|
||||
return value + "p"
|
||||
}
|
||||
|
||||
func videoResolutionNameRequest(profile *VideoCapabilityConfig, value string) string {
|
||||
requested := strings.ToLower(strings.TrimSpace(value))
|
||||
if requested == "" || requested == "auto" || requested == "default" || requested == "medium" || requested == "high" {
|
||||
return ""
|
||||
}
|
||||
if profile == nil {
|
||||
return ""
|
||||
}
|
||||
if len(profile.Resolutions) == 0 {
|
||||
return ""
|
||||
}
|
||||
candidates := []string{requested, strings.ToLower(normalizeVideoResolution(requested))}
|
||||
if requested == "4k" {
|
||||
candidates = append(candidates, "2160p")
|
||||
}
|
||||
if requested == "2160" || requested == "2160p" {
|
||||
candidates = append(candidates, "4k")
|
||||
}
|
||||
for _, supported := range profile.Resolutions {
|
||||
for _, candidate := range candidates {
|
||||
if strings.EqualFold(strings.TrimSpace(supported), candidate) {
|
||||
return strings.TrimSpace(supported)
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func isAutomaticVideoResolution(value string) bool {
|
||||
switch strings.ToLower(strings.TrimSpace(value)) {
|
||||
case "", "auto", "default", "medium", "high":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeXAIVideoDuration(value string) int {
|
||||
duration, err := strconv.Atoi(strings.TrimSpace(value))
|
||||
if err != nil || duration <= 0 {
|
||||
@@ -180,6 +220,8 @@ func normalizeXAIVideoResolution(value string) string {
|
||||
return "480p"
|
||||
case "1080", "1080p":
|
||||
return "1080p"
|
||||
case "1440", "1440p", "2k":
|
||||
return "1440p"
|
||||
case "2160", "2160p", "4k":
|
||||
return "2160p"
|
||||
default:
|
||||
|
||||
@@ -29,6 +29,7 @@ import (
|
||||
"infinite-canvas/backend/internal/model"
|
||||
|
||||
cos "github.com/tencentyun/cos-go-sdk-v5"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const providerResourceURLTTL = 4 * time.Hour
|
||||
@@ -45,6 +46,17 @@ type ResourceStream struct {
|
||||
AcceptRanges string
|
||||
}
|
||||
|
||||
type ResourceDeliveryOptions struct {
|
||||
ForceDirect bool
|
||||
ForceProxy bool
|
||||
}
|
||||
|
||||
type ResourceDelivery struct {
|
||||
Resource *model.Resource
|
||||
Stream *ResourceStream
|
||||
RedirectURL string
|
||||
}
|
||||
|
||||
func (s *Service) Resources(userID string, limit int) ([]model.Resource, error) {
|
||||
resources, err := s.repo.Resources(userID, limit)
|
||||
for index := range resources {
|
||||
@@ -90,6 +102,48 @@ func (s *Service) directResourceURL(resource *model.Resource, expiresAt time.Tim
|
||||
return signedOSSObjectURL(setting, resource.ObjectKey, expiresAt)
|
||||
}
|
||||
|
||||
// PrepareResourceDelivery 统一决定浏览器资源出口:配置 CDN 时默认直连 CDN,显式代理仅用于需要同源 Blob 的内部读取。
|
||||
func (s *Service) PrepareResourceDelivery(userID string, id string, options ResourceDeliveryOptions) (*ResourceDelivery, error) {
|
||||
resource, err := s.repo.ResourceForUser(userID, id)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, NotFound("资源不存在")
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return s.prepareResourceDelivery(userID, resource, options)
|
||||
}
|
||||
|
||||
func (s *Service) prepareResourceDelivery(userID string, resource *model.Resource, options ResourceDeliveryOptions) (*ResourceDelivery, error) {
|
||||
if resource == nil {
|
||||
return nil, errors.New("资源不存在")
|
||||
}
|
||||
if resource.Status != model.ResourceStatusReady {
|
||||
return nil, BadAuthRequest("资源尚未上传完成")
|
||||
}
|
||||
if resource.Provider != "local" && !options.ForceProxy {
|
||||
setting, err := s.ossSettingForResource(userID, resource)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if setting.CDNBaseURL != "" {
|
||||
redirectURL, err := ossCDNObjectURL(setting.CDNBaseURL, resource.ObjectKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &ResourceDelivery{Resource: resource, RedirectURL: redirectURL}, nil
|
||||
}
|
||||
if options.ForceDirect {
|
||||
redirectURL, err := signedOSSObjectURL(setting, resource.ObjectKey, time.Now().Add(directResourceURLTTL))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &ResourceDelivery{Resource: resource, RedirectURL: redirectURL}, nil
|
||||
}
|
||||
}
|
||||
return &ResourceDelivery{Resource: resource}, nil
|
||||
}
|
||||
|
||||
func (s *Service) signedPublicResourceURL(resourceID string, expiresAt time.Time) (string, error) {
|
||||
baseURL, err := s.publicResourceBaseURL()
|
||||
if err != nil {
|
||||
@@ -634,6 +688,16 @@ func (s *Service) ossSettingForResource(userID string, resource *model.Resource)
|
||||
var err error
|
||||
if resource.StorageSettingID != "" {
|
||||
_, setting, err = s.readUserOSSSettingByID(userID, resource.StorageSettingID)
|
||||
if err == nil {
|
||||
_, current, currentErr := s.readUserOSSSetting(userID)
|
||||
if currentErr != nil {
|
||||
return ossSettingValue{}, currentErr
|
||||
}
|
||||
// 密钥继续固定在历史版本;同一存储位置的 CDN 域名跟随当前配置,使已有资源也立即切换。
|
||||
if resourceStorageMatches(current, resource) {
|
||||
setting.CDNBaseURL = current.CDNBaseURL
|
||||
}
|
||||
}
|
||||
} else {
|
||||
_, setting, err = s.readOSSSetting()
|
||||
}
|
||||
@@ -652,6 +716,16 @@ func (s *Service) ossSettingForResource(userID string, resource *model.Resource)
|
||||
return setting, nil
|
||||
}
|
||||
|
||||
func resourceStorageMatches(setting ossSettingValue, resource *model.Resource) bool {
|
||||
if resource == nil {
|
||||
return false
|
||||
}
|
||||
setting = normalizeOSSSetting(setting)
|
||||
return setting.Provider == strings.ToLower(strings.TrimSpace(resource.Provider)) &&
|
||||
setting.Endpoint == strings.TrimRight(strings.TrimSpace(resource.Endpoint), "/") &&
|
||||
setting.Bucket == strings.TrimSpace(resource.Bucket)
|
||||
}
|
||||
|
||||
func ossSettingForProvider(setting ossSettingValue, provider string) (ossSettingValue, error) {
|
||||
setting = normalizeOSSSetting(setting)
|
||||
provider = strings.ToLower(strings.TrimSpace(provider))
|
||||
|
||||
@@ -0,0 +1,369 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"infinite-canvas/backend/internal/model"
|
||||
)
|
||||
|
||||
func (s *Service) deleteUserAssetWithResources(userID string, assetID string) error {
|
||||
asset, err := s.repo.AssetForUser(userID, assetID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
assetReferences, err := s.repo.AssetBusinessReferences(userID, assetID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
versions, representations, err := s.repo.AssetResourceRecords(assetID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
resourceIDs := map[string]struct{}{}
|
||||
originTaskIDs := map[string]struct{}{}
|
||||
if err := collectOwnedAssetDocumentReferences(asset.PayloadJSON, resourceIDs, originTaskIDs); err != nil {
|
||||
return BadAuthRequest("素材数据无法解析,已停止删除以避免误删文件")
|
||||
}
|
||||
for _, version := range versions {
|
||||
if err := collectOwnedAssetDocumentReferences(version.DefinitionJSON, resourceIDs, originTaskIDs); err != nil {
|
||||
return BadAuthRequest("素材版本数据无法解析,已停止删除以避免误删文件")
|
||||
}
|
||||
}
|
||||
for _, representation := range representations {
|
||||
if resourceID := validCanvasResourceID(representation.ResourceID); resourceID != "" {
|
||||
resourceIDs[resourceID] = struct{}{}
|
||||
}
|
||||
if taskID := validCanvasResourceID(representation.TaskID); taskID != "" {
|
||||
originTaskIDs[taskID] = struct{}{}
|
||||
}
|
||||
if err := collectOwnedAssetDocumentReferences(representation.MetadataJSON, resourceIDs, originTaskIDs); err != nil {
|
||||
return BadAuthRequest("素材表现数据无法解析,已停止删除以避免误删文件")
|
||||
}
|
||||
}
|
||||
|
||||
candidateIDs := sortedReferenceIDs(resourceIDs)
|
||||
resources, err := s.repo.ResourcesForUserIDs(userID, candidateIDs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ownedIDs := make([]string, 0, len(resources))
|
||||
ownedIDSet := make(map[string]struct{}, len(resources))
|
||||
for _, resource := range resources {
|
||||
ownedIDs = append(ownedIDs, resource.ID)
|
||||
ownedIDSet[resource.ID] = struct{}{}
|
||||
}
|
||||
|
||||
usages := make([]resourceUsage, 0, len(assetReferences))
|
||||
for _, reference := range assetReferences {
|
||||
usages = append(usages, resourceUsage{Kind: reference.Kind, ID: reference.ID, Title: reference.Title})
|
||||
}
|
||||
if len(ownedIDs) > 0 {
|
||||
snapshot, snapshotErr := s.repo.ResourceReferenceSnapshot(userID, assetID, ownedIDs)
|
||||
if snapshotErr != nil {
|
||||
return snapshotErr
|
||||
}
|
||||
for _, reference := range snapshot.Direct {
|
||||
if _, exists := ownedIDSet[reference.ResourceID]; exists {
|
||||
usages = append(usages, resourceUsage{Kind: reference.Kind, ID: reference.ID, Title: reference.Title})
|
||||
}
|
||||
}
|
||||
for _, document := range snapshot.Documents {
|
||||
primaryReferenced := documentReferencesResources(document.PrimaryJSON, ownedIDSet)
|
||||
secondaryReferenced := documentReferencesResources(document.SecondaryJSON, ownedIDSet)
|
||||
if document.Kind == "任务" {
|
||||
_, originTask := originTaskIDs[document.ID]
|
||||
secondaryReferenced = secondaryReferenced && !originTask
|
||||
}
|
||||
if primaryReferenced || secondaryReferenced {
|
||||
usages = append(usages, resourceUsage{Kind: document.Kind, ID: document.ID, Title: document.Title})
|
||||
}
|
||||
}
|
||||
}
|
||||
if message := resourceOccupiedMessage(usages); message != "" {
|
||||
return BadAuthRequest(message)
|
||||
}
|
||||
|
||||
// 所有引用校验必须先完成;任何资源仍被占用时,一个物理文件都不会开始删除。
|
||||
physicalObjects := map[string]*model.Resource{}
|
||||
for index := range resources {
|
||||
resource := &resources[index]
|
||||
sharedCount, countErr := s.repo.ResourceStorageReferenceCount(resource, ownedIDs)
|
||||
if countErr != nil {
|
||||
return countErr
|
||||
}
|
||||
if sharedCount > 0 {
|
||||
continue
|
||||
}
|
||||
physicalObjects[resourceStorageIdentity(resource)] = resource
|
||||
}
|
||||
physicalKeys := make([]string, 0, len(physicalObjects))
|
||||
for key := range physicalObjects {
|
||||
physicalKeys = append(physicalKeys, key)
|
||||
}
|
||||
sort.Strings(physicalKeys)
|
||||
for _, key := range physicalKeys {
|
||||
resource := physicalObjects[key]
|
||||
if err := s.deleteStoredResourceObject(userID, resource); err != nil {
|
||||
return fmt.Errorf("素材文件删除失败,素材记录已保留:%w", err)
|
||||
}
|
||||
}
|
||||
if err := s.repo.DeleteAssetAndResources(userID, assetID, ownedIDs); err != nil {
|
||||
return fmt.Errorf("素材记录删除失败,请重试:%w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type resourceUsage struct {
|
||||
Kind string
|
||||
ID string
|
||||
Title string
|
||||
}
|
||||
|
||||
func resourceOccupiedMessage(usages []resourceUsage) string {
|
||||
seen := map[string]struct{}{}
|
||||
labels := make([]string, 0, len(usages))
|
||||
for _, usage := range usages {
|
||||
key := usage.Kind + "\x00" + usage.ID
|
||||
if _, exists := seen[key]; exists {
|
||||
continue
|
||||
}
|
||||
seen[key] = struct{}{}
|
||||
title := strings.TrimSpace(usage.Title)
|
||||
if title == "" {
|
||||
title = usage.ID
|
||||
}
|
||||
title = truncateRunes(title, 32)
|
||||
labels = append(labels, usage.Kind+"「"+title+"」")
|
||||
}
|
||||
if len(labels) == 0 {
|
||||
return ""
|
||||
}
|
||||
sort.Strings(labels)
|
||||
visible := labels
|
||||
if len(visible) > 3 {
|
||||
visible = append(append([]string{}, visible[:3]...), fmt.Sprintf("等 %d 处", len(labels)))
|
||||
}
|
||||
return "素材仍被" + strings.Join(visible, "、") + "引用,请先在对应画布、任务或业务记录中解除引用后再删除"
|
||||
}
|
||||
|
||||
func collectOwnedAssetDocumentReferences(raw string, resourceIDs map[string]struct{}, taskIDs map[string]struct{}) error {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
return nil
|
||||
}
|
||||
var value any
|
||||
if err := json.Unmarshal([]byte(raw), &value); err != nil {
|
||||
return err
|
||||
}
|
||||
walkReferenceDocument(value, "", resourceIDs, taskIDs)
|
||||
return nil
|
||||
}
|
||||
|
||||
func walkReferenceDocument(value any, parentKey string, resourceIDs map[string]struct{}, taskIDs map[string]struct{}) {
|
||||
switch item := value.(type) {
|
||||
case map[string]any:
|
||||
for key, child := range item {
|
||||
walkReferenceDocument(child, key, resourceIDs, taskIDs)
|
||||
}
|
||||
case []any:
|
||||
for _, child := range item {
|
||||
walkReferenceDocument(child, parentKey, resourceIDs, taskIDs)
|
||||
}
|
||||
case string:
|
||||
key := normalizeReferenceKey(parentKey)
|
||||
if isResourceLocatorKey(key) {
|
||||
if resourceID := canvasResourceID(item); resourceID != "" {
|
||||
resourceIDs[resourceID] = struct{}{}
|
||||
}
|
||||
}
|
||||
if isBareResourceIDKey(key) {
|
||||
if resourceID := validCanvasResourceID(item); resourceID != "" {
|
||||
resourceIDs[resourceID] = struct{}{}
|
||||
}
|
||||
}
|
||||
if key == "taskid" || key == "taskids" {
|
||||
if taskID := validCanvasResourceID(item); taskID != "" {
|
||||
taskIDs[taskID] = struct{}{}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeReferenceKey(key string) string {
|
||||
key = strings.ToLower(strings.TrimSpace(key))
|
||||
return strings.NewReplacer("_", "", "-", "").Replace(key)
|
||||
}
|
||||
|
||||
func isBareResourceIDKey(key string) bool {
|
||||
return key == "resourceid" || key == "resourceids" || key == "sampleresourceid" || key == "referenceresourceid" || key == "referenceresourceids"
|
||||
}
|
||||
|
||||
func isResourceLocatorKey(key string) bool {
|
||||
if key == "" || strings.HasSuffix(key, "url") || strings.HasSuffix(key, "urls") || strings.HasSuffix(key, "storagekey") {
|
||||
return true
|
||||
}
|
||||
switch key {
|
||||
case "content", "data", "image", "images", "video", "videos", "audio", "audios", "media", "references", "result", "output", "artifactref", "providerartifactref":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func documentReferencesResources(raw string, resourceIDs map[string]struct{}) bool {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" || len(resourceIDs) == 0 {
|
||||
return false
|
||||
}
|
||||
var value any
|
||||
if err := json.Unmarshal([]byte(raw), &value); err == nil {
|
||||
found := map[string]struct{}{}
|
||||
walkReferenceDocument(value, "", found, map[string]struct{}{})
|
||||
for resourceID := range found {
|
||||
if _, exists := resourceIDs[resourceID]; exists {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
// cover_url 等字段可以直接保存资源 URL,而不是 JSON。候选记录已经包含完整 ID,保守阻止删除。
|
||||
for resourceID := range resourceIDs {
|
||||
if strings.Contains(raw, resourceID) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func sortedReferenceIDs(values map[string]struct{}) []string {
|
||||
result := make([]string, 0, len(values))
|
||||
for value := range values {
|
||||
result = append(result, value)
|
||||
}
|
||||
sort.Strings(result)
|
||||
return result
|
||||
}
|
||||
|
||||
func resourceStorageIdentity(resource *model.Resource) string {
|
||||
if resource == nil {
|
||||
return ""
|
||||
}
|
||||
provider := strings.ToLower(strings.TrimSpace(resource.Provider))
|
||||
if provider == "" {
|
||||
provider = "local"
|
||||
}
|
||||
return strings.Join([]string{provider, resource.Endpoint, resource.Bucket, resource.ObjectKey}, "\x00")
|
||||
}
|
||||
|
||||
func (s *Service) deleteStoredResourceObject(userID string, resource *model.Resource) error {
|
||||
if resource == nil {
|
||||
return errors.New("资源记录为空")
|
||||
}
|
||||
if strings.TrimSpace(resource.ObjectKey) == "" {
|
||||
return fmt.Errorf("资源 %s 的存储路径为空", resource.ID)
|
||||
}
|
||||
switch strings.ToLower(strings.TrimSpace(resource.Provider)) {
|
||||
case "", "local":
|
||||
return s.deleteLocalResourceObject(resource.ObjectKey)
|
||||
case aliyunOSSProvider:
|
||||
setting, err := s.ossSettingForResource(userID, resource)
|
||||
if err != nil {
|
||||
return fmt.Errorf("无法读取阿里云 OSS 配置:%w", err)
|
||||
}
|
||||
return deleteAliyunOSSObject(setting, resource.ObjectKey)
|
||||
case tencentCOSProvider:
|
||||
setting, err := s.ossSettingForResource(userID, resource)
|
||||
if err != nil {
|
||||
return fmt.Errorf("无法读取腾讯云 COS 配置:%w", err)
|
||||
}
|
||||
return deleteTencentCOSObject(setting, resource.ObjectKey)
|
||||
default:
|
||||
return fmt.Errorf("资源 %s 使用了不支持的存储类型 %q", resource.ID, resource.Provider)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) deleteLocalResourceObject(objectKey string) error {
|
||||
root, err := filepath.Abs(filepath.Join(s.dataDir, "resources"))
|
||||
if err != nil {
|
||||
return fmt.Errorf("解析本地资源目录失败:%w", err)
|
||||
}
|
||||
target, err := filepath.Abs(filepath.Join(root, filepath.FromSlash(strings.TrimLeft(objectKey, "/\\"))))
|
||||
if err != nil {
|
||||
return fmt.Errorf("解析本地资源路径失败:%w", err)
|
||||
}
|
||||
relative, err := filepath.Rel(root, target)
|
||||
if err != nil || relative == "." || relative == ".." || strings.HasPrefix(relative, ".."+string(filepath.Separator)) {
|
||||
return errors.New("本地资源路径超出允许目录")
|
||||
}
|
||||
fileInfo, err := os.Lstat(target)
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return nil
|
||||
} else if err != nil {
|
||||
return fmt.Errorf("检查服务器本地文件失败:%w", err)
|
||||
}
|
||||
if fileInfo.IsDir() {
|
||||
return errors.New("本地资源路径指向目录,已停止删除")
|
||||
}
|
||||
resolvedRoot, err := filepath.EvalSymlinks(root)
|
||||
if err != nil {
|
||||
return fmt.Errorf("检查本地资源目录失败:%w", err)
|
||||
}
|
||||
resolvedTarget, err := filepath.EvalSymlinks(target)
|
||||
if err != nil {
|
||||
return fmt.Errorf("检查本地资源路径失败:%w", err)
|
||||
}
|
||||
resolvedRelative, err := filepath.Rel(resolvedRoot, resolvedTarget)
|
||||
if err != nil || resolvedRelative == "." || resolvedRelative == ".." || strings.HasPrefix(resolvedRelative, ".."+string(filepath.Separator)) {
|
||||
return errors.New("本地资源真实路径超出允许目录")
|
||||
}
|
||||
if err := os.Remove(target); err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
return fmt.Errorf("删除服务器本地文件失败:%w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func deleteAliyunOSSObject(setting ossSettingValue, objectKey string) error {
|
||||
req, err := newOSSRequest(http.MethodDelete, setting, objectKey, "", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resp, err := OutboundHTTPClient(2 * time.Minute).Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("删除阿里云 OSS 对象失败:%w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if (resp.StatusCode < 200 || resp.StatusCode >= 300) && resp.StatusCode != http.StatusNotFound {
|
||||
detail, _ := io.ReadAll(io.LimitReader(resp.Body, 512))
|
||||
return fmt.Errorf("删除阿里云 OSS 对象失败:%s %s", resp.Status, strings.TrimSpace(string(detail)))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func deleteTencentCOSObject(setting ossSettingValue, objectKey string) error {
|
||||
client, err := newCOSClient(setting, 2*time.Minute)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resp, err := client.Object.Delete(context.Background(), objectKey)
|
||||
if resp != nil && resp.Body != nil {
|
||||
defer resp.Body.Close()
|
||||
}
|
||||
if err != nil {
|
||||
if resp != nil && resp.StatusCode == http.StatusNotFound {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("删除腾讯云 COS 对象失败:%w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCollectOwnedAssetDocumentReferences(t *testing.T) {
|
||||
resources := map[string]struct{}{}
|
||||
tasks := map[string]struct{}{}
|
||||
raw := `{
|
||||
"data":{"storageKey":"resource:resource-1","url":"/api/resources/resource-2/file"},
|
||||
"metadata":{"taskId":"task-1","referenceResourceIds":["resource-3"],"errorDetails":"failed near /api/resources/not-a-reference/file"}
|
||||
}`
|
||||
if err := collectOwnedAssetDocumentReferences(raw, resources, tasks); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, resourceID := range []string{"resource-1", "resource-2", "resource-3"} {
|
||||
if _, exists := resources[resourceID]; !exists {
|
||||
t.Fatalf("resource %q was not collected: %#v", resourceID, resources)
|
||||
}
|
||||
}
|
||||
if _, exists := tasks["task-1"]; !exists {
|
||||
t.Fatalf("origin task was not collected: %#v", tasks)
|
||||
}
|
||||
if _, exists := resources["not-a-reference"]; exists {
|
||||
t.Fatalf("diagnostic text must not be treated as an actual resource reference: %#v", resources)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDocumentReferencesResourcesUsesExactResourceID(t *testing.T) {
|
||||
candidates := map[string]struct{}{"resource-1": {}}
|
||||
if documentReferencesResources(`{"storageKey":"resource:resource-10"}`, candidates) {
|
||||
t.Fatal("resource-1 must not match resource-10")
|
||||
}
|
||||
if !documentReferencesResources(`{"storageKey":"resource:resource-1"}`, candidates) {
|
||||
t.Fatal("exact resource ID should match")
|
||||
}
|
||||
}
|
||||
|
||||
func TestResourceOccupiedMessageNamesBusinessRecord(t *testing.T) {
|
||||
message := resourceOccupiedMessage([]resourceUsage{
|
||||
{Kind: "画布", ID: "canvas-1", Title: "广告分镜"},
|
||||
{Kind: "画布", ID: "canvas-1", Title: "广告分镜"},
|
||||
})
|
||||
if !strings.Contains(message, "画布「广告分镜」") || !strings.Contains(message, "解除引用") {
|
||||
t.Fatalf("message = %q", message)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteLocalResourceObjectRemovesOnlyResourceDirectoryFile(t *testing.T) {
|
||||
dataDir := t.TempDir()
|
||||
resourcePath := filepath.Join(dataDir, "resources", "users", "user-1", "image", "asset.png")
|
||||
if err := os.MkdirAll(filepath.Dir(resourcePath), 0o750); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(resourcePath, []byte("image"), 0o640); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
service := &Service{dataDir: dataDir}
|
||||
if err := service.deleteLocalResourceObject("users/user-1/image/asset.png"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := os.Stat(resourcePath); !os.IsNotExist(err) {
|
||||
t.Fatalf("resource file still exists or stat failed unexpectedly: %v", err)
|
||||
}
|
||||
|
||||
outsidePath := filepath.Join(dataDir, "outside.txt")
|
||||
if err := os.WriteFile(outsidePath, []byte("keep"), 0o640); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := service.deleteLocalResourceObject("../outside.txt"); err == nil {
|
||||
t.Fatal("path traversal should be rejected")
|
||||
}
|
||||
if _, err := os.Stat(outsidePath); err != nil {
|
||||
t.Fatalf("outside file was changed: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -358,6 +358,123 @@ func TestDirectResourceURLChecksOwnershipAndSignsOSSResource(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrepareResourceDeliveryPrefersConfiguredCDN(t *testing.T) {
|
||||
svc := newResourceTestService(t)
|
||||
settingJSON, _ := json.Marshal(ossSettingValue{
|
||||
Enabled: true, Provider: tencentCOSProvider, Endpoint: "https://cos.ap-shanghai.myqcloud.com", CDNBaseURL: "https://media.example.com",
|
||||
Bucket: "private-bucket-1250000000", AccessKeyID: "secret-id", AccessKeySecret: "secret-key",
|
||||
})
|
||||
if err := svc.repo.SaveSystemSetting(&model.SystemSetting{Key: ossSettingKey, ValueJSON: string(settingJSON)}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resource := model.Resource{
|
||||
ID: "resource-cdn-delivery", UserID: "user-1", Kind: "image", Status: model.ResourceStatusReady,
|
||||
Provider: tencentCOSProvider, Endpoint: "https://cos.ap-shanghai.myqcloud.com", Bucket: "private-bucket-1250000000",
|
||||
ObjectKey: "users/user-1/image/test image.png", MimeType: "image/png",
|
||||
}
|
||||
if err := svc.repo.CreateResource(&resource); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
delivery, err := svc.PrepareResourceDelivery("user-1", resource.ID, ResourceDeliveryOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if delivery.Resource == nil || delivery.Resource.ID != resource.ID || delivery.RedirectURL != "https://media.example.com/users/user-1/image/test%20image.png" {
|
||||
t.Fatalf("PrepareResourceDelivery() = %#v", delivery)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrepareResourceDeliveryAllowsExplicitProxyWithCDN(t *testing.T) {
|
||||
svc := newResourceTestService(t)
|
||||
settingJSON, _ := json.Marshal(ossSettingValue{
|
||||
Enabled: true, Provider: aliyunOSSProvider, Endpoint: "https://oss-cn-test.aliyuncs.com", CDNBaseURL: "https://media.example.com",
|
||||
Bucket: "private-bucket", AccessKeyID: "access-id", AccessKeySecret: "secret-value",
|
||||
})
|
||||
if err := svc.repo.SaveSystemSetting(&model.SystemSetting{Key: ossSettingKey, ValueJSON: string(settingJSON)}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resource := model.Resource{
|
||||
ID: "resource-cdn-proxy", UserID: "user-1", Kind: "image", Status: model.ResourceStatusReady,
|
||||
Provider: aliyunOSSProvider, Endpoint: "https://oss-cn-test.aliyuncs.com", Bucket: "private-bucket",
|
||||
ObjectKey: "users/user-1/image/proxy.png", MimeType: "image/png",
|
||||
}
|
||||
if err := svc.repo.CreateResource(&resource); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
delivery, err := svc.PrepareResourceDelivery("user-1", resource.ID, ResourceDeliveryOptions{ForceProxy: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if delivery.Resource == nil || delivery.Resource.ID != resource.ID || delivery.RedirectURL != "" {
|
||||
t.Fatalf("PrepareResourceDelivery(force proxy) = %#v", delivery)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCurrentUserCDNSettingAppliesToHistoricalResourcesInSameStorage(t *testing.T) {
|
||||
t.Setenv("CANVAS_ALLOW_PRIVATE_UPSTREAMS", "true")
|
||||
server := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}))
|
||||
defer server.Close()
|
||||
svc := newResourceTestService(t)
|
||||
actor := &model.User{ID: "user-1"}
|
||||
if _, err := svc.UpdateUserOSSSetting(actor, OSSSettingRequest{
|
||||
Enabled: true, Provider: aliyunOSSProvider, Endpoint: server.URL, Bucket: "private-bucket",
|
||||
AccessKeyID: "access-id", AccessKeySecret: "secret-value",
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
historical, _, err := svc.readUserOSSSetting(actor.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resource := model.Resource{
|
||||
ID: "resource-user-cdn", UserID: actor.ID, Kind: "image", Status: model.ResourceStatusReady,
|
||||
Provider: aliyunOSSProvider, Endpoint: server.URL, Bucket: "private-bucket", StorageSettingID: historical.ID,
|
||||
ObjectKey: "users/user-1/image/historical.png", MimeType: "image/png",
|
||||
}
|
||||
if err := svc.repo.CreateResource(&resource); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := svc.UpdateUserOSSSetting(actor, OSSSettingRequest{
|
||||
Enabled: true, Provider: aliyunOSSProvider, Endpoint: server.URL, CDNBaseURL: server.URL, Bucket: "private-bucket",
|
||||
AccessKeyID: "access-id", AccessKeySecret: "secret-value",
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
delivery, err := svc.PrepareResourceDelivery(actor.ID, resource.ID, ResourceDeliveryOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if delivery.RedirectURL != server.URL+"/users/user-1/image/historical.png" {
|
||||
t.Fatalf("PrepareResourceDelivery(historical user resource) = %#v", delivery)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrepareResourceDeliveryKeepsForcedOriginDirectWithoutCDN(t *testing.T) {
|
||||
svc := newResourceTestService(t)
|
||||
settingJSON, _ := json.Marshal(ossSettingValue{
|
||||
Enabled: true, Provider: aliyunOSSProvider, Endpoint: "https://oss-cn-test.aliyuncs.com", Bucket: "private-bucket",
|
||||
AccessKeyID: "access-id", AccessKeySecret: "secret-value",
|
||||
})
|
||||
if err := svc.repo.SaveSystemSetting(&model.SystemSetting{Key: ossSettingKey, ValueJSON: string(settingJSON)}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resource := model.Resource{
|
||||
ID: "resource-origin-direct", UserID: "user-1", Kind: "image", Status: model.ResourceStatusReady,
|
||||
Provider: aliyunOSSProvider, Endpoint: "https://oss-cn-test.aliyuncs.com", Bucket: "private-bucket",
|
||||
ObjectKey: "users/user-1/image/direct.png", MimeType: "image/png",
|
||||
}
|
||||
if err := svc.repo.CreateResource(&resource); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
delivery, err := svc.PrepareResourceDelivery("user-1", resource.ID, ResourceDeliveryOptions{ForceDirect: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(delivery.RedirectURL, "private-bucket.oss-cn-test.aliyuncs.com/users/user-1/image/direct.png") || !strings.Contains(delivery.RedirectURL, "Signature=") {
|
||||
t.Fatalf("PrepareResourceDelivery(force direct) = %#v", delivery)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeSingleByteRange(t *testing.T) {
|
||||
tests := map[string]string{
|
||||
"bytes=0-1023": "bytes=0-1023",
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
)
|
||||
|
||||
// EnsureChatCompletionStreamUsageRequest 为 Token 计费的流式 Chat Completions
|
||||
// 请求开启最终 usage 块;非流式请求保持原样,避免改变普通接口合同。
|
||||
func EnsureChatCompletionStreamUsageRequest(data []byte) ([]byte, error) {
|
||||
var payload map[string]any
|
||||
if err := json.Unmarshal(data, &payload); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
stream, _ := payload["stream"].(bool)
|
||||
if !stream {
|
||||
return data, nil
|
||||
}
|
||||
if err := ensureChatCompletionStreamUsage(payload); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return json.Marshal(payload)
|
||||
}
|
||||
|
||||
func ensureChatCompletionStreamUsage(payload map[string]any) error {
|
||||
options := map[string]any{}
|
||||
if value, exists := payload["stream_options"]; exists {
|
||||
var ok bool
|
||||
options, ok = value.(map[string]any)
|
||||
if !ok {
|
||||
return errors.New("stream_options 必须是 JSON 对象")
|
||||
}
|
||||
}
|
||||
options["include_usage"] = true
|
||||
payload["stream_options"] = options
|
||||
return nil
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"infinite-canvas/backend/internal/model"
|
||||
|
||||
@@ -102,17 +103,9 @@ func (s *Service) UpsertUserAsset(userID string, raw json.RawMessage) (UserDataS
|
||||
}
|
||||
|
||||
func (s *Service) DeleteUserAsset(userID string, id string) error {
|
||||
if _, err := s.repo.AssetForUser(userID, id); err != nil {
|
||||
return err
|
||||
}
|
||||
references, err := s.repo.AssetReferenceCount(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if references > 0 {
|
||||
return BadAuthRequest("素材仍被项目或镜头引用,请先解除引用")
|
||||
}
|
||||
return s.repo.DeleteAsset(userID, id)
|
||||
s.storageMu.Lock()
|
||||
defer s.storageMu.Unlock()
|
||||
return s.deleteUserAssetWithResources(userID, id)
|
||||
}
|
||||
|
||||
func (s *Service) UserAssets(userID string) ([]json.RawMessage, error) {
|
||||
@@ -295,6 +288,13 @@ func assetFromJSON(userID string, raw json.RawMessage) (model.Asset, error) {
|
||||
if id == "" {
|
||||
id = newID()
|
||||
}
|
||||
if utf8.RuneCountInString(id) > model.AssetIDMaxLength {
|
||||
return model.Asset{}, BadAuthRequest("素材 ID 不能超过 80 个字符")
|
||||
}
|
||||
primaryVersionID := strings.TrimSpace(payload.PrimaryVersionID)
|
||||
if utf8.RuneCountInString(primaryVersionID) > 36 {
|
||||
return model.Asset{}, BadAuthRequest("素材主版本 ID 不能超过 36 个字符")
|
||||
}
|
||||
category := model.AssetCategory(strings.TrimSpace(payload.Category))
|
||||
if category == "" {
|
||||
category = model.AssetCategoryOther
|
||||
@@ -309,7 +309,7 @@ func assetFromJSON(userID string, raw json.RawMessage) (model.Asset, error) {
|
||||
Kind: strings.TrimSpace(payload.Kind),
|
||||
Category: category,
|
||||
Status: status,
|
||||
PrimaryVersionID: strings.TrimSpace(payload.PrimaryVersionID),
|
||||
PrimaryVersionID: primaryVersionID,
|
||||
Title: strings.TrimSpace(payload.Title),
|
||||
PayloadJSON: string(raw),
|
||||
CreatedAt: createdAt,
|
||||
|
||||
@@ -2,9 +2,55 @@ package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"infinite-canvas/backend/internal/model"
|
||||
)
|
||||
|
||||
func TestAssetFromJSONAcceptsDeterministicGenerationID(t *testing.T) {
|
||||
id := "generation_" + strings.Repeat("a", 64)
|
||||
raw, err := json.Marshal(map[string]any{"id": id, "kind": "image", "title": "生成图片"})
|
||||
if err != nil {
|
||||
t.Fatalf("marshal asset: %v", err)
|
||||
}
|
||||
|
||||
asset, err := assetFromJSON("user-1", raw)
|
||||
if err != nil {
|
||||
t.Fatalf("assetFromJSON: %v", err)
|
||||
}
|
||||
if asset.ID != id {
|
||||
t.Fatalf("asset ID = %q, want %q", asset.ID, id)
|
||||
}
|
||||
if len([]rune(asset.ID)) > model.AssetIDMaxLength {
|
||||
t.Fatalf("generation asset ID length = %d, limit %d", len([]rune(asset.ID)), model.AssetIDMaxLength)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAssetFromJSONRejectsIDOverLimit(t *testing.T) {
|
||||
raw, err := json.Marshal(map[string]any{"id": strings.Repeat("a", model.AssetIDMaxLength+1), "kind": "image"})
|
||||
if err != nil {
|
||||
t.Fatalf("marshal asset: %v", err)
|
||||
}
|
||||
|
||||
_, err = assetFromJSON("user-1", raw)
|
||||
if err == nil || !strings.Contains(err.Error(), "素材 ID 不能超过 80 个字符") {
|
||||
t.Fatalf("assetFromJSON error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAssetFromJSONRejectsPrimaryVersionIDOverLimit(t *testing.T) {
|
||||
raw, err := json.Marshal(map[string]any{"id": "asset-1", "primaryVersionId": strings.Repeat("v", 37)})
|
||||
if err != nil {
|
||||
t.Fatalf("marshal asset: %v", err)
|
||||
}
|
||||
|
||||
_, err = assetFromJSON("user-1", raw)
|
||||
if err == nil || !strings.Contains(err.Error(), "素材主版本 ID 不能超过 36 个字符") {
|
||||
t.Fatalf("assetFromJSON error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateSyncedPayloadAllowsDataURLMentionInErrorMessage(t *testing.T) {
|
||||
raw, err := json.Marshal(map[string]interface{}{
|
||||
"nodes": []interface{}{
|
||||
|
||||
Reference in new issue
Block a user