- models/file.go:File 模型,字段与 Attachment 对齐并新增 Type 归属类型字段 - models/db.go:AutoMigrate 注册 File;启动时幂等迁移 attachments → files (按主键 id 对齐跳过已迁移行,含软删除行一并复制) - scripts/add_files_table.sql:MariaDB 幂等迁移脚本(建表 + INSERT...SELECT) - 线上库 blog_go:files 表已建立,15 条 attachments 记录已迁入,type='attachments'
42 lines
2.0 KiB
Go
42 lines
2.0 KiB
Go
package models
|
||
|
||
import (
|
||
"time"
|
||
|
||
"gorm.io/gorm"
|
||
)
|
||
|
||
// File 是全站统一的上传文件登记表,用于管理所有类型的上传文件
|
||
// (附件、头像、Logo 等)。字段与 Attachment 对齐,额外通过 Type
|
||
// 字段区分文件归属类型。
|
||
//
|
||
// 说明:
|
||
// - attachments 表中的历史数据在启动迁移时复制到本表,Type 一律
|
||
// 标记为 "attachments"(见 db.go 的 migrateAttachmentsToFiles)。
|
||
// - 磁盘去重与引用计数沿用附件策略:StoredName 为内容 SHA-256,
|
||
// 删除时仅当没有任何行引用时才删磁盘文件。
|
||
type File struct {
|
||
ID uint `gorm:"primarykey" json:"id"`
|
||
Type string `gorm:"size:32;index;default:attachments" json:"type"` // 归属类型:attachments/avatar/logo/...
|
||
ArticleID uint `gorm:"index" json:"article_id"` // 在创建页面上待绑定时为 0
|
||
SessionToken string `gorm:"size:64;index" json:"-"` // 创建页面上的临时归属令牌
|
||
UploaderID uint `gorm:"index" json:"uploader_id"`
|
||
Filename string `gorm:"size:255" json:"filename"` // 原始文件名
|
||
StoredName string `gorm:"size:64;index" json:"stored_name"` // SHA-256 十六进制字符串,磁盘文件名
|
||
Ext string `gorm:"size:32" json:"ext"`
|
||
MIME string `gorm:"size:128" json:"mime"`
|
||
Size int64 `gorm:"default:0" json:"size"`
|
||
Category string `gorm:"size:32" json:"category"` // image/document/archive/video/other
|
||
CreatedAt time.Time `json:"created_at"`
|
||
UpdatedAt time.Time `json:"updated_at"`
|
||
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at"`
|
||
}
|
||
|
||
// TableName 覆盖 GORM 默认的表名。
|
||
func (File) TableName() string {
|
||
return "files"
|
||
}
|
||
|
||
// FileTypeAttachment 是文件归属类型常量:来自 attachments 表的历史附件。
|
||
const FileTypeAttachment = "attachments"
|