- 48 个 Go 文件所有注释(行注释/块注释/行尾注释,含 _test.go)翻译为中文 - 保留技术标识符:SECURITY_TODO(n)、unsafe-inline、sqlite/mysql、路由参数等 - 代码、字符串字面量、日志消息保持英文原文,零逻辑改动 - go build/vet 通过,go test -count=1 ./... 全绿
75 lines
2.9 KiB
Go
75 lines
2.9 KiB
Go
package models
|
|
|
|
import "time"
|
|
|
|
// UploadCategory 为管理界面按类别归类文件类型。
|
|
const (
|
|
CategoryImage = "image"
|
|
CategoryDocument = "document"
|
|
CategoryArchive = "archive"
|
|
CategoryVideo = "video"
|
|
CategoryOther = "other"
|
|
)
|
|
|
|
// DefaultUploadMaxSize 是默认的单文件大小上限(10 MiB),单位为字节。
|
|
const DefaultUploadMaxSize int64 = 10 * 1024 * 1024
|
|
|
|
// UploadConfig 保存单例(id=1)的全局附件上传策略。
|
|
type UploadConfig struct {
|
|
ID uint `gorm:"primarykey" json:"id"`
|
|
Enabled bool `gorm:"default:true" json:"enabled"` // 附件上传总开关
|
|
DefaultMaxSize int64 `gorm:"default:10485760" json:"default_max_size"` // 字节;可被 UploadFileType.MaxSize 按类型覆盖
|
|
StorageDir string `gorm:"size:255;default:attachments" json:"storage_dir"` // cfg.Path 下的子目录
|
|
UpdatedBy uint `gorm:"index" json:"updated_by"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
|
|
// TableName 覆盖 GORM 默认的表名。
|
|
func (UploadConfig) TableName() string {
|
|
return "upload_configs"
|
|
}
|
|
|
|
// UploadFileType 描述一种允许的附件扩展名。允许多行记录。
|
|
type UploadFileType struct {
|
|
ID uint `gorm:"primarykey" json:"id"`
|
|
Extension string `gorm:"size:32;uniqueIndex" json:"extension"` // 带前导点,如 ".pdf"
|
|
MimeType string `gorm:"size:128" json:"mime_type"` // 用于校验的关联 MIME
|
|
Category string `gorm:"size:32;index" json:"category"` // image/document/archive/video/other
|
|
MaxSize int64 `gorm:"default:0" json:"max_size"` // 字节;0 表示使用 UploadConfig.DefaultMaxSize
|
|
Enabled bool `gorm:"default:true" json:"enabled"`
|
|
Sort int `gorm:"default:0" json:"sort"`
|
|
}
|
|
|
|
// TableName 覆盖 GORM 默认的表名。
|
|
func (UploadFileType) TableName() string {
|
|
return "upload_file_types"
|
|
}
|
|
|
|
// EffectiveMaxSize 返回该类型下单个文件的大小上限,当 MaxSize 为 0 时
|
|
// 回退到传入的默认值。
|
|
func (t *UploadFileType) EffectiveMaxSize(def int64) int64 {
|
|
if t.MaxSize > 0 {
|
|
return t.MaxSize
|
|
}
|
|
return def
|
|
}
|
|
|
|
// DownloadBaseURL 是一种用于构建附件下载链接的来源基础 URL。
|
|
// 允许多行记录;标记为 IsDefault(或优先级最高且启用)的行
|
|
// 用于生成链接。
|
|
type DownloadBaseURL struct {
|
|
ID uint `gorm:"primarykey" json:"id"`
|
|
Name string `gorm:"size:64" json:"name"` // 标签,如 "主站" / "CDN"
|
|
BaseURL string `gorm:"size:512" json:"base_url"` // 例如 https://cdn.example.com/uploads
|
|
Priority int `gorm:"default:0" json:"priority"` // 数值越小优先级越高
|
|
IsDefault bool `gorm:"default:false" json:"is_default"`
|
|
Enabled bool `gorm:"default:true" json:"enabled"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
|
|
// TableName 覆盖 GORM 默认的表名。
|
|
func (DownloadBaseURL) TableName() string {
|
|
return "download_baseurls"
|
|
}
|