Files
sub2api/backend/ent/soracachefile_update.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

597 lines
18 KiB
Go

// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"time"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
"github.com/Wei-Shaw/sub2api/ent/predicate"
"github.com/Wei-Shaw/sub2api/ent/soracachefile"
)
// SoraCacheFileUpdate is the builder for updating SoraCacheFile entities.
type SoraCacheFileUpdate struct {
config
hooks []Hook
mutation *SoraCacheFileMutation
}
// Where appends a list predicates to the SoraCacheFileUpdate builder.
func (_u *SoraCacheFileUpdate) Where(ps ...predicate.SoraCacheFile) *SoraCacheFileUpdate {
_u.mutation.Where(ps...)
return _u
}
// SetTaskID sets the "task_id" field.
func (_u *SoraCacheFileUpdate) SetTaskID(v string) *SoraCacheFileUpdate {
_u.mutation.SetTaskID(v)
return _u
}
// SetNillableTaskID sets the "task_id" field if the given value is not nil.
func (_u *SoraCacheFileUpdate) SetNillableTaskID(v *string) *SoraCacheFileUpdate {
if v != nil {
_u.SetTaskID(*v)
}
return _u
}
// ClearTaskID clears the value of the "task_id" field.
func (_u *SoraCacheFileUpdate) ClearTaskID() *SoraCacheFileUpdate {
_u.mutation.ClearTaskID()
return _u
}
// SetAccountID sets the "account_id" field.
func (_u *SoraCacheFileUpdate) SetAccountID(v int64) *SoraCacheFileUpdate {
_u.mutation.ResetAccountID()
_u.mutation.SetAccountID(v)
return _u
}
// SetNillableAccountID sets the "account_id" field if the given value is not nil.
func (_u *SoraCacheFileUpdate) SetNillableAccountID(v *int64) *SoraCacheFileUpdate {
if v != nil {
_u.SetAccountID(*v)
}
return _u
}
// AddAccountID adds value to the "account_id" field.
func (_u *SoraCacheFileUpdate) AddAccountID(v int64) *SoraCacheFileUpdate {
_u.mutation.AddAccountID(v)
return _u
}
// SetUserID sets the "user_id" field.
func (_u *SoraCacheFileUpdate) SetUserID(v int64) *SoraCacheFileUpdate {
_u.mutation.ResetUserID()
_u.mutation.SetUserID(v)
return _u
}
// SetNillableUserID sets the "user_id" field if the given value is not nil.
func (_u *SoraCacheFileUpdate) SetNillableUserID(v *int64) *SoraCacheFileUpdate {
if v != nil {
_u.SetUserID(*v)
}
return _u
}
// AddUserID adds value to the "user_id" field.
func (_u *SoraCacheFileUpdate) AddUserID(v int64) *SoraCacheFileUpdate {
_u.mutation.AddUserID(v)
return _u
}
// SetMediaType sets the "media_type" field.
func (_u *SoraCacheFileUpdate) SetMediaType(v string) *SoraCacheFileUpdate {
_u.mutation.SetMediaType(v)
return _u
}
// SetNillableMediaType sets the "media_type" field if the given value is not nil.
func (_u *SoraCacheFileUpdate) SetNillableMediaType(v *string) *SoraCacheFileUpdate {
if v != nil {
_u.SetMediaType(*v)
}
return _u
}
// SetOriginalURL sets the "original_url" field.
func (_u *SoraCacheFileUpdate) SetOriginalURL(v string) *SoraCacheFileUpdate {
_u.mutation.SetOriginalURL(v)
return _u
}
// SetNillableOriginalURL sets the "original_url" field if the given value is not nil.
func (_u *SoraCacheFileUpdate) SetNillableOriginalURL(v *string) *SoraCacheFileUpdate {
if v != nil {
_u.SetOriginalURL(*v)
}
return _u
}
// SetCachePath sets the "cache_path" field.
func (_u *SoraCacheFileUpdate) SetCachePath(v string) *SoraCacheFileUpdate {
_u.mutation.SetCachePath(v)
return _u
}
// SetNillableCachePath sets the "cache_path" field if the given value is not nil.
func (_u *SoraCacheFileUpdate) SetNillableCachePath(v *string) *SoraCacheFileUpdate {
if v != nil {
_u.SetCachePath(*v)
}
return _u
}
// SetCacheURL sets the "cache_url" field.
func (_u *SoraCacheFileUpdate) SetCacheURL(v string) *SoraCacheFileUpdate {
_u.mutation.SetCacheURL(v)
return _u
}
// SetNillableCacheURL sets the "cache_url" field if the given value is not nil.
func (_u *SoraCacheFileUpdate) SetNillableCacheURL(v *string) *SoraCacheFileUpdate {
if v != nil {
_u.SetCacheURL(*v)
}
return _u
}
// SetSizeBytes sets the "size_bytes" field.
func (_u *SoraCacheFileUpdate) SetSizeBytes(v int64) *SoraCacheFileUpdate {
_u.mutation.ResetSizeBytes()
_u.mutation.SetSizeBytes(v)
return _u
}
// SetNillableSizeBytes sets the "size_bytes" field if the given value is not nil.
func (_u *SoraCacheFileUpdate) SetNillableSizeBytes(v *int64) *SoraCacheFileUpdate {
if v != nil {
_u.SetSizeBytes(*v)
}
return _u
}
// AddSizeBytes adds value to the "size_bytes" field.
func (_u *SoraCacheFileUpdate) AddSizeBytes(v int64) *SoraCacheFileUpdate {
_u.mutation.AddSizeBytes(v)
return _u
}
// SetCreatedAt sets the "created_at" field.
func (_u *SoraCacheFileUpdate) SetCreatedAt(v time.Time) *SoraCacheFileUpdate {
_u.mutation.SetCreatedAt(v)
return _u
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (_u *SoraCacheFileUpdate) SetNillableCreatedAt(v *time.Time) *SoraCacheFileUpdate {
if v != nil {
_u.SetCreatedAt(*v)
}
return _u
}
// Mutation returns the SoraCacheFileMutation object of the builder.
func (_u *SoraCacheFileUpdate) Mutation() *SoraCacheFileMutation {
return _u.mutation
}
// Save executes the query and returns the number of nodes affected by the update operation.
func (_u *SoraCacheFileUpdate) Save(ctx context.Context) (int, error) {
return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}
// SaveX is like Save, but panics if an error occurs.
func (_u *SoraCacheFileUpdate) SaveX(ctx context.Context) int {
affected, err := _u.Save(ctx)
if err != nil {
panic(err)
}
return affected
}
// Exec executes the query.
func (_u *SoraCacheFileUpdate) Exec(ctx context.Context) error {
_, err := _u.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_u *SoraCacheFileUpdate) ExecX(ctx context.Context) {
if err := _u.Exec(ctx); err != nil {
panic(err)
}
}
// check runs all checks and user-defined validators on the builder.
func (_u *SoraCacheFileUpdate) check() error {
if v, ok := _u.mutation.TaskID(); ok {
if err := soracachefile.TaskIDValidator(v); err != nil {
return &ValidationError{Name: "task_id", err: fmt.Errorf(`ent: validator failed for field "SoraCacheFile.task_id": %w`, err)}
}
}
if v, ok := _u.mutation.MediaType(); ok {
if err := soracachefile.MediaTypeValidator(v); err != nil {
return &ValidationError{Name: "media_type", err: fmt.Errorf(`ent: validator failed for field "SoraCacheFile.media_type": %w`, err)}
}
}
return nil
}
func (_u *SoraCacheFileUpdate) sqlSave(ctx context.Context) (_node int, err error) {
if err := _u.check(); err != nil {
return _node, err
}
_spec := sqlgraph.NewUpdateSpec(soracachefile.Table, soracachefile.Columns, sqlgraph.NewFieldSpec(soracachefile.FieldID, field.TypeInt64))
if ps := _u.mutation.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if value, ok := _u.mutation.TaskID(); ok {
_spec.SetField(soracachefile.FieldTaskID, field.TypeString, value)
}
if _u.mutation.TaskIDCleared() {
_spec.ClearField(soracachefile.FieldTaskID, field.TypeString)
}
if value, ok := _u.mutation.AccountID(); ok {
_spec.SetField(soracachefile.FieldAccountID, field.TypeInt64, value)
}
if value, ok := _u.mutation.AddedAccountID(); ok {
_spec.AddField(soracachefile.FieldAccountID, field.TypeInt64, value)
}
if value, ok := _u.mutation.UserID(); ok {
_spec.SetField(soracachefile.FieldUserID, field.TypeInt64, value)
}
if value, ok := _u.mutation.AddedUserID(); ok {
_spec.AddField(soracachefile.FieldUserID, field.TypeInt64, value)
}
if value, ok := _u.mutation.MediaType(); ok {
_spec.SetField(soracachefile.FieldMediaType, field.TypeString, value)
}
if value, ok := _u.mutation.OriginalURL(); ok {
_spec.SetField(soracachefile.FieldOriginalURL, field.TypeString, value)
}
if value, ok := _u.mutation.CachePath(); ok {
_spec.SetField(soracachefile.FieldCachePath, field.TypeString, value)
}
if value, ok := _u.mutation.CacheURL(); ok {
_spec.SetField(soracachefile.FieldCacheURL, field.TypeString, value)
}
if value, ok := _u.mutation.SizeBytes(); ok {
_spec.SetField(soracachefile.FieldSizeBytes, field.TypeInt64, value)
}
if value, ok := _u.mutation.AddedSizeBytes(); ok {
_spec.AddField(soracachefile.FieldSizeBytes, field.TypeInt64, value)
}
if value, ok := _u.mutation.CreatedAt(); ok {
_spec.SetField(soracachefile.FieldCreatedAt, field.TypeTime, value)
}
if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{soracachefile.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return 0, err
}
_u.mutation.done = true
return _node, nil
}
// SoraCacheFileUpdateOne is the builder for updating a single SoraCacheFile entity.
type SoraCacheFileUpdateOne struct {
config
fields []string
hooks []Hook
mutation *SoraCacheFileMutation
}
// SetTaskID sets the "task_id" field.
func (_u *SoraCacheFileUpdateOne) SetTaskID(v string) *SoraCacheFileUpdateOne {
_u.mutation.SetTaskID(v)
return _u
}
// SetNillableTaskID sets the "task_id" field if the given value is not nil.
func (_u *SoraCacheFileUpdateOne) SetNillableTaskID(v *string) *SoraCacheFileUpdateOne {
if v != nil {
_u.SetTaskID(*v)
}
return _u
}
// ClearTaskID clears the value of the "task_id" field.
func (_u *SoraCacheFileUpdateOne) ClearTaskID() *SoraCacheFileUpdateOne {
_u.mutation.ClearTaskID()
return _u
}
// SetAccountID sets the "account_id" field.
func (_u *SoraCacheFileUpdateOne) SetAccountID(v int64) *SoraCacheFileUpdateOne {
_u.mutation.ResetAccountID()
_u.mutation.SetAccountID(v)
return _u
}
// SetNillableAccountID sets the "account_id" field if the given value is not nil.
func (_u *SoraCacheFileUpdateOne) SetNillableAccountID(v *int64) *SoraCacheFileUpdateOne {
if v != nil {
_u.SetAccountID(*v)
}
return _u
}
// AddAccountID adds value to the "account_id" field.
func (_u *SoraCacheFileUpdateOne) AddAccountID(v int64) *SoraCacheFileUpdateOne {
_u.mutation.AddAccountID(v)
return _u
}
// SetUserID sets the "user_id" field.
func (_u *SoraCacheFileUpdateOne) SetUserID(v int64) *SoraCacheFileUpdateOne {
_u.mutation.ResetUserID()
_u.mutation.SetUserID(v)
return _u
}
// SetNillableUserID sets the "user_id" field if the given value is not nil.
func (_u *SoraCacheFileUpdateOne) SetNillableUserID(v *int64) *SoraCacheFileUpdateOne {
if v != nil {
_u.SetUserID(*v)
}
return _u
}
// AddUserID adds value to the "user_id" field.
func (_u *SoraCacheFileUpdateOne) AddUserID(v int64) *SoraCacheFileUpdateOne {
_u.mutation.AddUserID(v)
return _u
}
// SetMediaType sets the "media_type" field.
func (_u *SoraCacheFileUpdateOne) SetMediaType(v string) *SoraCacheFileUpdateOne {
_u.mutation.SetMediaType(v)
return _u
}
// SetNillableMediaType sets the "media_type" field if the given value is not nil.
func (_u *SoraCacheFileUpdateOne) SetNillableMediaType(v *string) *SoraCacheFileUpdateOne {
if v != nil {
_u.SetMediaType(*v)
}
return _u
}
// SetOriginalURL sets the "original_url" field.
func (_u *SoraCacheFileUpdateOne) SetOriginalURL(v string) *SoraCacheFileUpdateOne {
_u.mutation.SetOriginalURL(v)
return _u
}
// SetNillableOriginalURL sets the "original_url" field if the given value is not nil.
func (_u *SoraCacheFileUpdateOne) SetNillableOriginalURL(v *string) *SoraCacheFileUpdateOne {
if v != nil {
_u.SetOriginalURL(*v)
}
return _u
}
// SetCachePath sets the "cache_path" field.
func (_u *SoraCacheFileUpdateOne) SetCachePath(v string) *SoraCacheFileUpdateOne {
_u.mutation.SetCachePath(v)
return _u
}
// SetNillableCachePath sets the "cache_path" field if the given value is not nil.
func (_u *SoraCacheFileUpdateOne) SetNillableCachePath(v *string) *SoraCacheFileUpdateOne {
if v != nil {
_u.SetCachePath(*v)
}
return _u
}
// SetCacheURL sets the "cache_url" field.
func (_u *SoraCacheFileUpdateOne) SetCacheURL(v string) *SoraCacheFileUpdateOne {
_u.mutation.SetCacheURL(v)
return _u
}
// SetNillableCacheURL sets the "cache_url" field if the given value is not nil.
func (_u *SoraCacheFileUpdateOne) SetNillableCacheURL(v *string) *SoraCacheFileUpdateOne {
if v != nil {
_u.SetCacheURL(*v)
}
return _u
}
// SetSizeBytes sets the "size_bytes" field.
func (_u *SoraCacheFileUpdateOne) SetSizeBytes(v int64) *SoraCacheFileUpdateOne {
_u.mutation.ResetSizeBytes()
_u.mutation.SetSizeBytes(v)
return _u
}
// SetNillableSizeBytes sets the "size_bytes" field if the given value is not nil.
func (_u *SoraCacheFileUpdateOne) SetNillableSizeBytes(v *int64) *SoraCacheFileUpdateOne {
if v != nil {
_u.SetSizeBytes(*v)
}
return _u
}
// AddSizeBytes adds value to the "size_bytes" field.
func (_u *SoraCacheFileUpdateOne) AddSizeBytes(v int64) *SoraCacheFileUpdateOne {
_u.mutation.AddSizeBytes(v)
return _u
}
// SetCreatedAt sets the "created_at" field.
func (_u *SoraCacheFileUpdateOne) SetCreatedAt(v time.Time) *SoraCacheFileUpdateOne {
_u.mutation.SetCreatedAt(v)
return _u
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (_u *SoraCacheFileUpdateOne) SetNillableCreatedAt(v *time.Time) *SoraCacheFileUpdateOne {
if v != nil {
_u.SetCreatedAt(*v)
}
return _u
}
// Mutation returns the SoraCacheFileMutation object of the builder.
func (_u *SoraCacheFileUpdateOne) Mutation() *SoraCacheFileMutation {
return _u.mutation
}
// Where appends a list predicates to the SoraCacheFileUpdate builder.
func (_u *SoraCacheFileUpdateOne) Where(ps ...predicate.SoraCacheFile) *SoraCacheFileUpdateOne {
_u.mutation.Where(ps...)
return _u
}
// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (_u *SoraCacheFileUpdateOne) Select(field string, fields ...string) *SoraCacheFileUpdateOne {
_u.fields = append([]string{field}, fields...)
return _u
}
// Save executes the query and returns the updated SoraCacheFile entity.
func (_u *SoraCacheFileUpdateOne) Save(ctx context.Context) (*SoraCacheFile, error) {
return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}
// SaveX is like Save, but panics if an error occurs.
func (_u *SoraCacheFileUpdateOne) SaveX(ctx context.Context) *SoraCacheFile {
node, err := _u.Save(ctx)
if err != nil {
panic(err)
}
return node
}
// Exec executes the query on the entity.
func (_u *SoraCacheFileUpdateOne) Exec(ctx context.Context) error {
_, err := _u.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_u *SoraCacheFileUpdateOne) ExecX(ctx context.Context) {
if err := _u.Exec(ctx); err != nil {
panic(err)
}
}
// check runs all checks and user-defined validators on the builder.
func (_u *SoraCacheFileUpdateOne) check() error {
if v, ok := _u.mutation.TaskID(); ok {
if err := soracachefile.TaskIDValidator(v); err != nil {
return &ValidationError{Name: "task_id", err: fmt.Errorf(`ent: validator failed for field "SoraCacheFile.task_id": %w`, err)}
}
}
if v, ok := _u.mutation.MediaType(); ok {
if err := soracachefile.MediaTypeValidator(v); err != nil {
return &ValidationError{Name: "media_type", err: fmt.Errorf(`ent: validator failed for field "SoraCacheFile.media_type": %w`, err)}
}
}
return nil
}
func (_u *SoraCacheFileUpdateOne) sqlSave(ctx context.Context) (_node *SoraCacheFile, err error) {
if err := _u.check(); err != nil {
return _node, err
}
_spec := sqlgraph.NewUpdateSpec(soracachefile.Table, soracachefile.Columns, sqlgraph.NewFieldSpec(soracachefile.FieldID, field.TypeInt64))
id, ok := _u.mutation.ID()
if !ok {
return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "SoraCacheFile.id" for update`)}
}
_spec.Node.ID.Value = id
if fields := _u.fields; len(fields) > 0 {
_spec.Node.Columns = make([]string, 0, len(fields))
_spec.Node.Columns = append(_spec.Node.Columns, soracachefile.FieldID)
for _, f := range fields {
if !soracachefile.ValidColumn(f) {
return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
}
if f != soracachefile.FieldID {
_spec.Node.Columns = append(_spec.Node.Columns, f)
}
}
}
if ps := _u.mutation.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if value, ok := _u.mutation.TaskID(); ok {
_spec.SetField(soracachefile.FieldTaskID, field.TypeString, value)
}
if _u.mutation.TaskIDCleared() {
_spec.ClearField(soracachefile.FieldTaskID, field.TypeString)
}
if value, ok := _u.mutation.AccountID(); ok {
_spec.SetField(soracachefile.FieldAccountID, field.TypeInt64, value)
}
if value, ok := _u.mutation.AddedAccountID(); ok {
_spec.AddField(soracachefile.FieldAccountID, field.TypeInt64, value)
}
if value, ok := _u.mutation.UserID(); ok {
_spec.SetField(soracachefile.FieldUserID, field.TypeInt64, value)
}
if value, ok := _u.mutation.AddedUserID(); ok {
_spec.AddField(soracachefile.FieldUserID, field.TypeInt64, value)
}
if value, ok := _u.mutation.MediaType(); ok {
_spec.SetField(soracachefile.FieldMediaType, field.TypeString, value)
}
if value, ok := _u.mutation.OriginalURL(); ok {
_spec.SetField(soracachefile.FieldOriginalURL, field.TypeString, value)
}
if value, ok := _u.mutation.CachePath(); ok {
_spec.SetField(soracachefile.FieldCachePath, field.TypeString, value)
}
if value, ok := _u.mutation.CacheURL(); ok {
_spec.SetField(soracachefile.FieldCacheURL, field.TypeString, value)
}
if value, ok := _u.mutation.SizeBytes(); ok {
_spec.SetField(soracachefile.FieldSizeBytes, field.TypeInt64, value)
}
if value, ok := _u.mutation.AddedSizeBytes(); ok {
_spec.AddField(soracachefile.FieldSizeBytes, field.TypeInt64, value)
}
if value, ok := _u.mutation.CreatedAt(); ok {
_spec.SetField(soracachefile.FieldCreatedAt, field.TypeTime, value)
}
_node = &SoraCacheFile{config: _u.config}
_spec.Assign = _node.assignValues
_spec.ScanValues = _node.scanValues
if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{soracachefile.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return nil, err
}
_u.mutation.done = true
return _node, nil
}