- 48 个 Go 文件所有注释(行注释/块注释/行尾注释,含 _test.go)翻译为中文 - 保留技术标识符:SECURITY_TODO(n)、unsafe-inline、sqlite/mysql、路由参数等 - 代码、字符串字面量、日志消息保持英文原文,零逻辑改动 - go build/vet 通过,go test -count=1 ./... 全绿
99 lines
4.0 KiB
Go
99 lines
4.0 KiB
Go
package models
|
||
|
||
import (
|
||
"log"
|
||
|
||
"gorm.io/gorm"
|
||
)
|
||
|
||
// seedSiteSettings 在不存在时插入单例的 site_settings 行(id=1)。
|
||
// 文本字段留空,以便模板回退到 i18n 默认值,直到管理员配置它们为止。
|
||
func seedSiteSettings(db *gorm.DB) {
|
||
var count int64
|
||
db.Model(&SiteSetting{}).Count(&count)
|
||
if count > 0 {
|
||
return
|
||
}
|
||
s := &SiteSetting{ID: 1}
|
||
if err := db.Create(s).Error; err != nil {
|
||
log.Printf("Warning: failed to seed site_settings: %v", err)
|
||
}
|
||
}
|
||
|
||
// seedUploadConfig 在不存在时插入单例的 upload_configs 行(id=1)。
|
||
func seedUploadConfig(db *gorm.DB) {
|
||
var count int64
|
||
db.Model(&UploadConfig{}).Count(&count)
|
||
if count > 0 {
|
||
return
|
||
}
|
||
c := &UploadConfig{
|
||
ID: 1,
|
||
Enabled: true,
|
||
DefaultMaxSize: DefaultUploadMaxSize,
|
||
StorageDir: "attachments",
|
||
}
|
||
if err := db.Create(c).Error; err != nil {
|
||
log.Printf("Warning: failed to seed upload_configs: %v", err)
|
||
}
|
||
}
|
||
|
||
// seedCommentConfig 在不存在时插入单例的 comment_configs 行(id=1)。
|
||
func seedCommentConfig(db *gorm.DB) {
|
||
var count int64
|
||
db.Model(&CommentConfig{}).Count(&count)
|
||
if count > 0 {
|
||
return
|
||
}
|
||
c := &CommentConfig{
|
||
ID: 1,
|
||
Enabled: true,
|
||
AllowGuest: true,
|
||
GuestRequireApproval: false,
|
||
// SECURITY_TODO #15:Gravatar 可通过反向查询暴露 MD5(邮箱);
|
||
// 新部署默认关闭,管理员可在评论设置页面重新启用。
|
||
UseGravatar: false,
|
||
}
|
||
if err := db.Create(c).Error; err != nil {
|
||
log.Printf("Warning: failed to seed comment_configs: %v", err)
|
||
}
|
||
}
|
||
|
||
// defaultUploadFileTypes 是首次运行初始化的常用允许附件类型集合。
|
||
var defaultUploadFileTypes = []UploadFileType{
|
||
// 图片
|
||
{Extension: ".jpg", MimeType: "image/jpeg", Category: CategoryImage, Enabled: true, Sort: 1},
|
||
{Extension: ".jpeg", MimeType: "image/jpeg", Category: CategoryImage, Enabled: true, Sort: 2},
|
||
{Extension: ".png", MimeType: "image/png", Category: CategoryImage, Enabled: true, Sort: 3},
|
||
{Extension: ".gif", MimeType: "image/gif", Category: CategoryImage, Enabled: true, Sort: 4},
|
||
{Extension: ".webp", MimeType: "image/webp", Category: CategoryImage, Enabled: true, Sort: 5},
|
||
// 文档
|
||
{Extension: ".pdf", MimeType: "application/pdf", Category: CategoryDocument, Enabled: true, Sort: 10},
|
||
{Extension: ".doc", MimeType: "application/msword", Category: CategoryDocument, Enabled: true, Sort: 11},
|
||
{Extension: ".docx", MimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document", Category: CategoryDocument, Enabled: true, Sort: 12},
|
||
{Extension: ".xls", MimeType: "application/vnd.ms-excel", Category: CategoryDocument, Enabled: true, Sort: 13},
|
||
{Extension: ".xlsx", MimeType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", Category: CategoryDocument, Enabled: true, Sort: 14},
|
||
{Extension: ".ppt", MimeType: "application/vnd.ms-powerpoint", Category: CategoryDocument, Enabled: true, Sort: 15},
|
||
{Extension: ".pptx", MimeType: "application/vnd.openxmlformats-officedocument.presentationml.presentation", Category: CategoryDocument, Enabled: true, Sort: 16},
|
||
{Extension: ".txt", MimeType: "text/plain", Category: CategoryDocument, Enabled: true, Sort: 17},
|
||
// 压缩包
|
||
{Extension: ".zip", MimeType: "application/zip", Category: CategoryArchive, Enabled: true, Sort: 20},
|
||
{Extension: ".rar", MimeType: "application/vnd.rar", Category: CategoryArchive, Enabled: true, Sort: 21},
|
||
{Extension: ".7z", MimeType: "application/x-7z-compressed", Category: CategoryArchive, Enabled: true, Sort: 22},
|
||
// 视频
|
||
{Extension: ".mp4", MimeType: "video/mp4", Category: CategoryVideo, Enabled: true, Sort: 30},
|
||
{Extension: ".avi", MimeType: "video/x-msvideo", Category: CategoryVideo, Enabled: true, Sort: 31},
|
||
}
|
||
|
||
// seedUploadFileTypes 在表为空时初始化允许的文件类型行。
|
||
func seedUploadFileTypes(db *gorm.DB) {
|
||
var count int64
|
||
db.Model(&UploadFileType{}).Count(&count)
|
||
if count > 0 {
|
||
return
|
||
}
|
||
if err := db.Create(&defaultUploadFileTypes).Error; err != nil {
|
||
log.Printf("Warning: failed to seed upload_file_types: %v", err)
|
||
}
|
||
}
|