Files
sub2api/backend/ent/sorausagestat.go
yangjianbo 13262a5698 feat(sora): 新增 Sora 平台支持并修复高危安全和性能问题
新增功能:
- 新增 Sora 账号管理和 OAuth 认证
- 新增 Sora 视频/图片生成 API 网关
- 新增 Sora 任务调度和缓存机制
- 新增 Sora 使用统计和计费支持
- 前端增加 Sora 平台配置界面

安全修复(代码审核):
- [SEC-001] 限制媒体下载响应体大小(图片 20MB、视频 200MB),防止 DoS 攻击
- [SEC-002] 限制 SDK API 响应大小(1MB),防止内存耗尽
- [SEC-003] 修复 SSRF 风险,添加 URL 验证并强制使用代理配置

BUG 修复(代码审核):
- [BUG-001] 修复 for 循环内 defer 累积导致的资源泄漏
- [BUG-002] 修复图片并发槽位获取失败时已持有锁未释放的永久泄漏

性能优化(代码审核):
- [PERF-001] 添加 Sentinel Token 缓存(3 分钟有效期),减少 PoW 计算开销

技术细节:
- 使用 io.LimitReader 限制所有外部输入的大小
- 添加 urlvalidator 验证防止 SSRF 攻击
- 使用 sync.Map 实现线程安全的包级缓存
- 优化并发槽位管理,添加 releaseAll 模式防止泄漏

影响范围:
- 后端:新增 Sora 相关数据模型、服务、网关和管理接口
- 前端:新增 Sora 平台配置、账号管理和监控界面
- 配置:新增 Sora 相关配置项和环境变量

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-29 16:18:38 +08:00

232 lines
8.8 KiB
Go

// Code generated by ent, DO NOT EDIT.
package ent
import (
"fmt"
"strings"
"time"
"entgo.io/ent"
"entgo.io/ent/dialect/sql"
"github.com/Wei-Shaw/sub2api/ent/sorausagestat"
)
// SoraUsageStat is the model entity for the SoraUsageStat schema.
type SoraUsageStat struct {
config `json:"-"`
// ID of the ent.
ID int64 `json:"id,omitempty"`
// CreatedAt holds the value of the "created_at" field.
CreatedAt time.Time `json:"created_at,omitempty"`
// UpdatedAt holds the value of the "updated_at" field.
UpdatedAt time.Time `json:"updated_at,omitempty"`
// 关联 accounts 表的 ID
AccountID int64 `json:"account_id,omitempty"`
// ImageCount holds the value of the "image_count" field.
ImageCount int `json:"image_count,omitempty"`
// VideoCount holds the value of the "video_count" field.
VideoCount int `json:"video_count,omitempty"`
// ErrorCount holds the value of the "error_count" field.
ErrorCount int `json:"error_count,omitempty"`
// LastErrorAt holds the value of the "last_error_at" field.
LastErrorAt *time.Time `json:"last_error_at,omitempty"`
// TodayImageCount holds the value of the "today_image_count" field.
TodayImageCount int `json:"today_image_count,omitempty"`
// TodayVideoCount holds the value of the "today_video_count" field.
TodayVideoCount int `json:"today_video_count,omitempty"`
// TodayErrorCount holds the value of the "today_error_count" field.
TodayErrorCount int `json:"today_error_count,omitempty"`
// TodayDate holds the value of the "today_date" field.
TodayDate *time.Time `json:"today_date,omitempty"`
// ConsecutiveErrorCount holds the value of the "consecutive_error_count" field.
ConsecutiveErrorCount int `json:"consecutive_error_count,omitempty"`
selectValues sql.SelectValues
}
// scanValues returns the types for scanning values from sql.Rows.
func (*SoraUsageStat) scanValues(columns []string) ([]any, error) {
values := make([]any, len(columns))
for i := range columns {
switch columns[i] {
case sorausagestat.FieldID, sorausagestat.FieldAccountID, sorausagestat.FieldImageCount, sorausagestat.FieldVideoCount, sorausagestat.FieldErrorCount, sorausagestat.FieldTodayImageCount, sorausagestat.FieldTodayVideoCount, sorausagestat.FieldTodayErrorCount, sorausagestat.FieldConsecutiveErrorCount:
values[i] = new(sql.NullInt64)
case sorausagestat.FieldCreatedAt, sorausagestat.FieldUpdatedAt, sorausagestat.FieldLastErrorAt, sorausagestat.FieldTodayDate:
values[i] = new(sql.NullTime)
default:
values[i] = new(sql.UnknownType)
}
}
return values, nil
}
// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the SoraUsageStat fields.
func (_m *SoraUsageStat) assignValues(columns []string, values []any) error {
if m, n := len(values), len(columns); m < n {
return fmt.Errorf("mismatch number of scan values: %d != %d", m, n)
}
for i := range columns {
switch columns[i] {
case sorausagestat.FieldID:
value, ok := values[i].(*sql.NullInt64)
if !ok {
return fmt.Errorf("unexpected type %T for field id", value)
}
_m.ID = int64(value.Int64)
case sorausagestat.FieldCreatedAt:
if value, ok := values[i].(*sql.NullTime); !ok {
return fmt.Errorf("unexpected type %T for field created_at", values[i])
} else if value.Valid {
_m.CreatedAt = value.Time
}
case sorausagestat.FieldUpdatedAt:
if value, ok := values[i].(*sql.NullTime); !ok {
return fmt.Errorf("unexpected type %T for field updated_at", values[i])
} else if value.Valid {
_m.UpdatedAt = value.Time
}
case sorausagestat.FieldAccountID:
if value, ok := values[i].(*sql.NullInt64); !ok {
return fmt.Errorf("unexpected type %T for field account_id", values[i])
} else if value.Valid {
_m.AccountID = value.Int64
}
case sorausagestat.FieldImageCount:
if value, ok := values[i].(*sql.NullInt64); !ok {
return fmt.Errorf("unexpected type %T for field image_count", values[i])
} else if value.Valid {
_m.ImageCount = int(value.Int64)
}
case sorausagestat.FieldVideoCount:
if value, ok := values[i].(*sql.NullInt64); !ok {
return fmt.Errorf("unexpected type %T for field video_count", values[i])
} else if value.Valid {
_m.VideoCount = int(value.Int64)
}
case sorausagestat.FieldErrorCount:
if value, ok := values[i].(*sql.NullInt64); !ok {
return fmt.Errorf("unexpected type %T for field error_count", values[i])
} else if value.Valid {
_m.ErrorCount = int(value.Int64)
}
case sorausagestat.FieldLastErrorAt:
if value, ok := values[i].(*sql.NullTime); !ok {
return fmt.Errorf("unexpected type %T for field last_error_at", values[i])
} else if value.Valid {
_m.LastErrorAt = new(time.Time)
*_m.LastErrorAt = value.Time
}
case sorausagestat.FieldTodayImageCount:
if value, ok := values[i].(*sql.NullInt64); !ok {
return fmt.Errorf("unexpected type %T for field today_image_count", values[i])
} else if value.Valid {
_m.TodayImageCount = int(value.Int64)
}
case sorausagestat.FieldTodayVideoCount:
if value, ok := values[i].(*sql.NullInt64); !ok {
return fmt.Errorf("unexpected type %T for field today_video_count", values[i])
} else if value.Valid {
_m.TodayVideoCount = int(value.Int64)
}
case sorausagestat.FieldTodayErrorCount:
if value, ok := values[i].(*sql.NullInt64); !ok {
return fmt.Errorf("unexpected type %T for field today_error_count", values[i])
} else if value.Valid {
_m.TodayErrorCount = int(value.Int64)
}
case sorausagestat.FieldTodayDate:
if value, ok := values[i].(*sql.NullTime); !ok {
return fmt.Errorf("unexpected type %T for field today_date", values[i])
} else if value.Valid {
_m.TodayDate = new(time.Time)
*_m.TodayDate = value.Time
}
case sorausagestat.FieldConsecutiveErrorCount:
if value, ok := values[i].(*sql.NullInt64); !ok {
return fmt.Errorf("unexpected type %T for field consecutive_error_count", values[i])
} else if value.Valid {
_m.ConsecutiveErrorCount = int(value.Int64)
}
default:
_m.selectValues.Set(columns[i], values[i])
}
}
return nil
}
// Value returns the ent.Value that was dynamically selected and assigned to the SoraUsageStat.
// This includes values selected through modifiers, order, etc.
func (_m *SoraUsageStat) Value(name string) (ent.Value, error) {
return _m.selectValues.Get(name)
}
// Update returns a builder for updating this SoraUsageStat.
// Note that you need to call SoraUsageStat.Unwrap() before calling this method if this SoraUsageStat
// was returned from a transaction, and the transaction was committed or rolled back.
func (_m *SoraUsageStat) Update() *SoraUsageStatUpdateOne {
return NewSoraUsageStatClient(_m.config).UpdateOne(_m)
}
// Unwrap unwraps the SoraUsageStat entity that was returned from a transaction after it was closed,
// so that all future queries will be executed through the driver which created the transaction.
func (_m *SoraUsageStat) Unwrap() *SoraUsageStat {
_tx, ok := _m.config.driver.(*txDriver)
if !ok {
panic("ent: SoraUsageStat is not a transactional entity")
}
_m.config.driver = _tx.drv
return _m
}
// String implements the fmt.Stringer.
func (_m *SoraUsageStat) String() string {
var builder strings.Builder
builder.WriteString("SoraUsageStat(")
builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID))
builder.WriteString("created_at=")
builder.WriteString(_m.CreatedAt.Format(time.ANSIC))
builder.WriteString(", ")
builder.WriteString("updated_at=")
builder.WriteString(_m.UpdatedAt.Format(time.ANSIC))
builder.WriteString(", ")
builder.WriteString("account_id=")
builder.WriteString(fmt.Sprintf("%v", _m.AccountID))
builder.WriteString(", ")
builder.WriteString("image_count=")
builder.WriteString(fmt.Sprintf("%v", _m.ImageCount))
builder.WriteString(", ")
builder.WriteString("video_count=")
builder.WriteString(fmt.Sprintf("%v", _m.VideoCount))
builder.WriteString(", ")
builder.WriteString("error_count=")
builder.WriteString(fmt.Sprintf("%v", _m.ErrorCount))
builder.WriteString(", ")
if v := _m.LastErrorAt; v != nil {
builder.WriteString("last_error_at=")
builder.WriteString(v.Format(time.ANSIC))
}
builder.WriteString(", ")
builder.WriteString("today_image_count=")
builder.WriteString(fmt.Sprintf("%v", _m.TodayImageCount))
builder.WriteString(", ")
builder.WriteString("today_video_count=")
builder.WriteString(fmt.Sprintf("%v", _m.TodayVideoCount))
builder.WriteString(", ")
builder.WriteString("today_error_count=")
builder.WriteString(fmt.Sprintf("%v", _m.TodayErrorCount))
builder.WriteString(", ")
if v := _m.TodayDate; v != nil {
builder.WriteString("today_date=")
builder.WriteString(v.Format(time.ANSIC))
}
builder.WriteString(", ")
builder.WriteString("consecutive_error_count=")
builder.WriteString(fmt.Sprintf("%v", _m.ConsecutiveErrorCount))
builder.WriteByte(')')
return builder.String()
}
// SoraUsageStats is a parsable slice of SoraUsageStat.
type SoraUsageStats []*SoraUsageStat