Files
go_blog/models/upload_config.go
kevinandClaude Fable 5 e3367e6e12 feat: add platform configuration tables and admin settings
Add four DB-backed configuration tables for site-wide settings:
site_settings (logo, top-left title, header/footer text with zh/en
variants), upload_configs (master switch, default size, storage dir),
upload_file_types (per-extension whitelist with per-type size limits),
and download_baseurls (multiple download sources with priority/default).

- Models, AutoMigrate, and first-run seed (18 common file types)
- Process-level config cache warmed at startup, refreshed on admin save
- SetUserContext injects cached site settings into templates
- base.html renders logo (local upload or external URL), title, header
  banner, and footer with i18n fallback
- Upload validator drives avatar uploads (form + AJAX) through the
  configured switch/type/size policy
- Admin pages for site/upload/download settings with i18n keys

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-21 20:32:28 +08:00

75 lines
3.0 KiB
Go

package models
import "time"
// UploadCategory groups file types for the admin UI.
const (
CategoryImage = "image"
CategoryDocument = "document"
CategoryArchive = "archive"
CategoryVideo = "video"
CategoryOther = "other"
)
// DefaultUploadMaxSize is the default per-file size limit (10 MiB), in bytes.
const DefaultUploadMaxSize int64 = 10 * 1024 * 1024
// UploadConfig holds the singleton (id=1) global attachment upload policy.
type UploadConfig struct {
ID uint `gorm:"primarykey" json:"id"`
Enabled bool `gorm:"default:true" json:"enabled"` // master switch for attachment uploads
DefaultMaxSize int64 `gorm:"default:10485760" json:"default_max_size"` // bytes; overridden per type by UploadFileType.MaxSize
StorageDir string `gorm:"size:255;default:attachments" json:"storage_dir"` // sub-dir under cfg.Path
UpdatedBy uint `gorm:"index" json:"updated_by"`
UpdatedAt time.Time `json:"updated_at"`
}
// TableName overrides the default GORM table name.
func (UploadConfig) TableName() string {
return "upload_configs"
}
// UploadFileType describes one permitted attachment extension. Multiple rows.
type UploadFileType struct {
ID uint `gorm:"primarykey" json:"id"`
Extension string `gorm:"size:32;uniqueIndex" json:"extension"` // with leading dot, e.g. ".pdf"
MimeType string `gorm:"size:128" json:"mime_type"` // associated MIME for validation
Category string `gorm:"size:32;index" json:"category"` // image/document/archive/video/other
MaxSize int64 `gorm:"default:0" json:"max_size"` // bytes; 0 means use UploadConfig.DefaultMaxSize
Enabled bool `gorm:"default:true" json:"enabled"`
Sort int `gorm:"default:0" json:"sort"`
}
// TableName overrides the default GORM table name.
func (UploadFileType) TableName() string {
return "upload_file_types"
}
// EffectiveMaxSize returns the per-file size limit for this type, falling back
// to the provided default when MaxSize is 0.
func (t *UploadFileType) EffectiveMaxSize(def int64) int64 {
if t.MaxSize > 0 {
return t.MaxSize
}
return def
}
// DownloadBaseURL is one source base URL used to build attachment download
// links. Multiple rows; the row marked IsDefault (or the highest-priority
// enabled one) is used for generated links.
type DownloadBaseURL struct {
ID uint `gorm:"primarykey" json:"id"`
Name string `gorm:"size:64" json:"name"` // label, e.g. "主站" / "CDN"
BaseURL string `gorm:"size:512" json:"base_url"` // e.g. https://cdn.example.com/uploads
Priority int `gorm:"default:0" json:"priority"` // lower = higher 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 overrides the default GORM table name.
func (DownloadBaseURL) TableName() string {
return "download_baseurls"
}