package model
import (
"github.com/HFO4/cloudreve/pkg/conf"
"github.com/HFO4/cloudreve/pkg/util"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
"github.com/mcuadros/go-version"
"io/ioutil"
)
//执行数据迁移
func migration() {
// 检查 version.lock 确认是否需要执行迁移
// Debug 模式及测试模式下一定会执行迁移
if !conf.SystemConfig.Debug && gin.Mode() != gin.TestMode {
if util.Exists("version.lock") {
versionLock, _ := ioutil.ReadFile("version.lock")
if version.Compare(string(versionLock), conf.BackendVersion, "=") {
util.Log().Info("后端版本匹配,跳过数据库迁移")
return
}
}
}
util.Log().Info("开始进行数据库自动迁移...")
// 自动迁移模式
if conf.DatabaseConfig.Type == "mysql" {
DB = DB.Set("gorm:table_options", "ENGINE=InnoDB")
}
DB.AutoMigrate(&User{}, &Setting{}, &Group{}, &Policy{}, &Folder{}, &File{})
// 创建初始存储策略
addDefaultPolicy()
// 创建初始用户组
addDefaultGroups()
// 创建初始管理员账户
addDefaultUser()
// 向设置数据表添加初始设置
addDefaultSettings()
// 迁移完毕后写入版本锁 version.lock
err := conf.WriteVersionLock()
if err != nil {
util.Log().Warning("无法写入版本控制锁 version.lock, %s", err)
}
util.Log().Info("数据库自动迁移结束")
}
func addDefaultPolicy() {
_, err := GetPolicyByID(uint(1))
// 未找到初始存储策略时,则创建
if gorm.IsRecordNotFoundError(err) {
defaultPolicy := Policy{
Name: "默认存储策略",
Type: "local",
Server: "/api/v3/file/upload",
BaseURL: "http://cloudreve.org/public/uploads/",
MaxSize: 10 * 1024 * 1024 * 1024,
AutoRename: true,
DirNameRule: "uploads/{uid}/{path}",
FileNameRule: "{uid}_{randomkey8}_{originname}",
IsOriginLinkEnable: false,
}
if err := DB.Create(&defaultPolicy).Error; err != nil {
util.Log().Panic("无法创建初始存储策略, %s", err)
}
}
}
func addDefaultSettings() {
defaultSettings := []Setting{
{Name: "siteURL", Value: `http://lite.aoaoao.me/`, Type: "basic"},
{Name: "siteName", Value: `Cloudreve`, Type: "basic"},
{Name: "siteStatus", Value: `open`, Type: "basic"},
{Name: "regStatus", Value: `0`, Type: "register"},
{Name: "defaultGroup", Value: `3`, Type: "register"},
{Name: "siteKeywords", Value: `网盘,网盘`, Type: "basic"},
{Name: "siteDes", Value: `Cloudreve`, Type: "basic"},
{Name: "siteTitle", Value: `平步云端`, Type: "basic"},
{Name: "fromName", Value: `Cloudreve`, Type: "mail"},
{Name: "fromAdress", Value: `no-reply@acg.blue`, Type: "mail"},
{Name: "smtpHost", Value: `smtp.mxhichina.com`, Type: "mail"},
{Name: "smtpPort", Value: `25`, Type: "mail"},
{Name: "replyTo", Value: `abslant@126.com`, Type: "mail"},
{Name: "smtpUser", Value: `no-reply@acg.blue`, Type: "mail"},
{Name: "smtpPass", Value: ``, Type: "mail"},
{Name: "encriptionType", Value: `no`, Type: "mail"},
{Name: "over_used_template", Value: `
容量超额提醒 | 容量超额警告 | 亲爱的{userName}: | 由于{notifyReason},您在{siteTitle}的账户的容量使用超出配额,您将无法继续上传新文件,请尽快清理文件,否则我们将会禁用您的账户。 | 登录{siteTitle} | 感谢您选择{siteTitle}。 |
|
| |
`, Type: "mail_template"},
{Name: "ban_time", Value: `10`, Type: "storage_policy"},
{Name: "maxEditSize", Value: `100000`, Type: "file_edit"},
{Name: "timeout", Value: `3600`, Type: "oss"},
{Name: "allowdVisitorDownload", Value: `false`, Type: "share"},
{Name: "login_captcha", Value: `0`, Type: "login"},
{Name: "qq_login", Value: `0`, Type: "login"},
{Name: "qq_login_id", Value: ``, Type: "login"},
{Name: "qq_login_key", Value: ``, Type: "login"},
{Name: "reg_captcha", Value: `0`, Type: "login"},
{Name: "email_active", Value: `0`, Type: "register"},
{Name: "mail_activation_template", Value: `容量超额提醒 | 激活{siteTitle}账户 | 亲爱的{userName}: | 感谢您注册{siteTitle},请点击下方按钮完成账户激活。 | 激活账户 | 感谢您选择{siteTitle}。 |
|
| |
`, Type: "mail_template"},
{Name: "forget_captcha", Value: `0`, Type: "login"},
{Name: "mail_reset_pwd_template", Value: `重设密码 | 重设{siteTitle}密码 | 亲爱的{userName}: | 请点击下方按钮完成密码重设。如果非你本人操作,请忽略此邮件。 | 重设密码 | 感谢您选择{siteTitle}。 |
|
| |
`, Type: "mail_template"},
{Name: "allow_buy_pack", Value: `1`, Type: "pack"},
{Name: "allow_buy_pack_by_pack", Value: `1`, Type: "pack"},
{Name: "allow_buy_pack_by_slider", Value: `1`, Type: "pack"},
{Name: "pack_data", Value: `[]`, Type: "pack"},
{Name: "database_version", Value: `6`, Type: "version"},
{Name: "payment_type", Value: `youzan`, Type: "payment"},
{Name: "appid", Value: ``, Type: "payment"},
{Name: "appkey", Value: ``, Type: "payment"},
{Name: "shopid", Value: ``, Type: "payment"},
{Name: "hot_share_num", Value: `10`, Type: "share"},
{Name: "allow_buy_group", Value: `1`, Type: "group_sell"},
{Name: "group_sell_data", Value: `[]`, Type: "group_sell"},
{Name: "gravatar_server", Value: `https://v2ex.assets.uxengine.net/gravatar/`, Type: "avatar"},
{Name: "admin_color_body", Value: `fixed-nav sticky-footer bg-light`, Type: "admin"},
{Name: "admin_color_nav", Value: `navbar navbar-expand-lg fixed-top navbar-light bg-light`, Type: "admin"},
{Name: "js_code", Value: ``, Type: "basic"},
{Name: "sendfile", Value: `0`, Type: "download"},
{Name: "defaultTheme", Value: `#3f51b5`, Type: "basic"},
{Name: "header", Value: `X-Sendfile`, Type: "download"},
{Name: "themes", Value: `{"#3f51b5":{"palette":{"common":{"black":"#000","white":"#fff"},"background":{"paper":"#fff","default":"#fafafa"},"primary":{"light":"#7986cb","main":"#3f51b5","dark":"#303f9f","contrastText":"#fff"},"secondary":{"light":"#ff4081","main":"#f50057","dark":"#c51162","contrastText":"#fff"},"error":{"light":"#e57373","main":"#f44336","dark":"#d32f2f","contrastText":"#fff"},"text":{"primary":"rgba(0, 0, 0, 0.87)","secondary":"rgba(0, 0, 0, 0.54)","disabled":"rgba(0, 0, 0, 0.38)","hint":"rgba(0, 0, 0, 0.38)"},"explorer":{"filename":"#474849","icon":"#8f8f8f","bgSelected":"#D5DAF0","emptyIcon":"#e8e8e8"}}}}
`, Type: "basic"},
{Name: "refererCheck", Value: `true`, Type: "share"},
{Name: "header", Value: `X-Sendfile`, Type: "download"},
{Name: "aria2_tmppath", Value: `/path/to/public/download`, Type: "aria2"},
{Name: "aria2_token", Value: `your token`, Type: "aria2"},
{Name: "aria2_rpcurl", Value: `http://127.0.0.1:6800/`, Type: "aria2"},
{Name: "aria2_options", Value: `{"max-tries":5}`, Type: "aria2"},
{Name: "task_queue_token", Value: ``, Type: "task"},
}
for _, value := range defaultSettings {
DB.Where(Setting{Name: value.Name}).FirstOrCreate(&value)
}
}
func addDefaultGroups() {
_, err := GetGroupByID(1)
// 未找到初始管理组时,则创建
if gorm.IsRecordNotFoundError(err) {
defaultAdminGroup := Group{
Name: "管理员",
PolicyList: []uint{1},
MaxStorage: 1 * 1024 * 1024 * 1024,
ShareEnabled: true,
Color: "danger",
WebDAVEnabled: true,
Aria2Option: "0,0,0",
}
if err := DB.Create(&defaultAdminGroup).Error; err != nil {
util.Log().Panic("无法创建管理用户组, %s", err)
}
}
err = nil
_, err = GetGroupByID(2)
// 未找到初始注册会员时,则创建
if gorm.IsRecordNotFoundError(err) {
defaultAdminGroup := Group{
Name: "注册会员",
PolicyList: []uint{1},
MaxStorage: 1 * 1024 * 1024 * 1024,
ShareEnabled: true,
Color: "danger",
WebDAVEnabled: true,
Aria2Option: "0,0,0",
}
if err := DB.Create(&defaultAdminGroup).Error; err != nil {
util.Log().Panic("无法创建初始注册会员用户组, %s", err)
}
}
}
func addDefaultUser() {
_, err := GetUserByID(1)
// 未找到初始用户时,则创建
if gorm.IsRecordNotFoundError(err) {
defaultUser := NewUser()
//TODO 动态生成密码
defaultUser.Email = "admin@cloudreve.org"
defaultUser.Nick = "admin"
defaultUser.Status = Active
defaultUser.GroupID = 1
defaultUser.PrimaryGroup = 1
err := defaultUser.SetPassword("admin")
if err != nil {
util.Log().Panic("无法创建密码, %s", err)
}
if err := DB.Create(&defaultUser).Error; err != nil {
util.Log().Panic("无法创建初始用户, %s", err)
}
}
}