migration.go 27.8 KB
Newer Older
H
HFO4 已提交
1 2
package model

H
HFO4 已提交
3
import (
H
HFO4 已提交
4 5
	"github.com/HFO4/cloudreve/pkg/conf"
	"github.com/HFO4/cloudreve/pkg/util"
H
HFO4 已提交
6
	"github.com/gin-gonic/gin"
H
HFO4 已提交
7
	"github.com/jinzhu/gorm"
8 9
	"github.com/mcuadros/go-version"
	"io/ioutil"
H
HFO4 已提交
10
)
H
HFO4 已提交
11

H
HFO4 已提交
12
//执行数据迁移
H
HFO4 已提交
13
func migration() {
14
	// 检查 version.lock 确认是否需要执行迁移
H
HFO4 已提交
15 16
	// Debug 模式及测试模式下一定会执行迁移
	if !conf.SystemConfig.Debug && gin.Mode() != gin.TestMode {
17 18 19 20 21 22 23 24 25 26 27
		if util.Exists("version.lock") {
			versionLock, _ := ioutil.ReadFile("version.lock")
			if version.Compare(string(versionLock), conf.BackendVersion, "=") {
				util.Log().Info("后端版本匹配,跳过数据库迁移")
				return
			}
		}
	}

	util.Log().Info("开始进行数据库自动迁移...")

H
HFO4 已提交
28
	// 自动迁移模式
H
HFO4 已提交
29 30 31
	if conf.DatabaseConfig.Type == "mysql" {
		DB = DB.Set("gorm:table_options", "ENGINE=InnoDB")
	}
H
HFO4 已提交
32
	DB.AutoMigrate(&User{}, &Setting{}, &Group{}, &Policy{}, &Folder{}, &File{}, &StoragePack{})
H
HFO4 已提交
33 34 35

	// 创建初始存储策略
	addDefaultPolicy()
H
HFO4 已提交
36 37 38

	// 创建初始用户组
	addDefaultGroups()
H
HFO4 已提交
39

H
HFO4 已提交
40 41 42 43 44 45 46 47 48
	// 创建初始管理员账户
	addDefaultUser()

	// 向设置数据表添加初始设置
	addDefaultSettings()

	// 迁移完毕后写入版本锁 version.lock
	err := conf.WriteVersionLock()
	if err != nil {
49
		util.Log().Warning("无法写入版本控制锁 version.lock, %s", err)
H
HFO4 已提交
50 51
	}

H
HFO4 已提交
52 53
	util.Log().Info("数据库自动迁移结束")

H
HFO4 已提交
54 55
}

H
HFO4 已提交
56
func addDefaultPolicy() {
H
HFO4 已提交
57
	_, err := GetPolicyByID(uint(1))
H
HFO4 已提交
58 59 60
	// 未找到初始存储策略时,则创建
	if gorm.IsRecordNotFoundError(err) {
		defaultPolicy := Policy{
H
HFO4 已提交
61
			Name:               "默认存储策略",
H
HFO4 已提交
62
			Type:               "local",
H
HFO4 已提交
63
			Server:             "/api/v3/file/upload",
H
HFO4 已提交
64 65 66
			BaseURL:            "http://cloudreve.org/public/uploads/",
			MaxSize:            10 * 1024 * 1024 * 1024,
			AutoRename:         true,
H
HFO4 已提交
67
			DirNameRule:        "uploads/{uid}/{path}",
H
HFO4 已提交
68 69
			FileNameRule:       "{uid}_{randomkey8}_{originname}",
			IsOriginLinkEnable: false,
H
HFO4 已提交
70 71 72
			OptionsSerialized: PolicyOption{
				FileType: []string{},
			},
H
HFO4 已提交
73 74
		}
		if err := DB.Create(&defaultPolicy).Error; err != nil {
75
			util.Log().Panic("无法创建初始存储策略, %s", err)
H
HFO4 已提交
76 77 78 79
		}
	}
}

H
HFO4 已提交
80 81
func addDefaultSettings() {
	defaultSettings := []Setting{
H
HFO4 已提交
82
		{Name: "siteURL", Value: ``, Type: "basic"},
H
HFO4 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
		{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: `<meta name="viewport"content="width=device-width"><meta http-equiv="Content-Type"content="text/html; charset=UTF-8"><title>容量超额提醒</title><style type="text/css">img{max-width:100%}body{-webkit-font-smoothing:antialiased;-webkit-text-size-adjust:none;width:100%!important;height:100%;line-height:1.6em}body{background-color:#f6f6f6}@media only screen and(max-width:640px){body{padding:0!important}h1{font-weight:800!important;margin:20px 0 5px!important}h2{font-weight:800!important;margin:20px 0 5px!important}h3{font-weight:800!important;margin:20px 0 5px!important}h4{font-weight:800!important;margin:20px 0 5px!important}h1{font-size:22px!important}h2{font-size:18px!important}h3{font-size:16px!important}.container{padding:0!important;width:100%!important}.content{padding:0!important}.content-wrap{padding:10px!important}.invoice{width:100%!important}}</style><table class="body-wrap"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; width: 100%; background-color: #f6f6f6; margin: 0;"bgcolor="#f6f6f6"><tbody><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0;"valign="top"></td><td class="container"width="600"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; display: block !important; max-width: 600px !important; clear: both !important; margin: 0 auto;"valign="top"><div class="content"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; max-width: 600px; display: block; margin: 0 auto; padding: 20px;"><table class="main"width="100%"cellpadding="0"cellspacing="0"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; border-radius: 3px; background-color: #fff; margin: 0; border: 1px 
solid #e9e9e9;"bgcolor="#fff"><tbody><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="alert alert-warning"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 16px; vertical-align: top; color: #fff; font-weight: 500; text-align: center; border-radius: 3px 3px 0 0; background-color: #FF9F00; margin: 0; padding: 20px;"align="center"bgcolor="#FF9F00"valign="top">容量超额警告</td></tr><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-wrap"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 20px;"valign="top"><table width="100%"cellpadding="0"cellspacing="0"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><tbody><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-block"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;"valign="top">亲爱的<strong style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">{userName}</strong>:</td></tr><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-block"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;"valign="top">由于{notifyReason},您在{siteTitle}的账户的容量使用超出配额,您将无法继续上传新文件,请尽快清理文件,否则我们将会禁用您的账户。</td></tr><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-block"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;"valign="top"><a href="{siteUrl}Login"class="btn-primary"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; color: #FFF; text-decoration: none; line-height: 2em; font-weight: bold; text-align: center; cursor: pointer; display: inline-block; border-radius: 5px; text-transform: capitalize; background-color: #348eda; margin: 0; border-color: #348eda; border-style: solid; border-width: 10px 20px;">登录{siteTitle}</a></td></tr><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-block"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;"valign="top">感谢您选择{siteTitle}。</td></tr></tbody></table></td></tr></tbody></table><div class="footer"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; width: 100%; clear: both; color: #999; margin: 0; padding: 20px;"><table width="100%"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><tbody><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="aligncenter content-block"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 12px; vertical-align: top; color: #999; text-align: center; margin: 0; padding: 0 0 20px;"align="center"valign="top">此邮件由系统自动发送,请不要直接回复。</td></tr></tbody></table></div></div></td><td style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0;"valign="top"></td></tr></tbody></table>`, Type: "mail_template"},
		{Name: "ban_time", Value: `10`, Type: "storage_policy"},
		{Name: "maxEditSize", Value: `100000`, Type: "file_edit"},
H
HFO4 已提交
102
		{Name: "oss_timeout", Value: `3600`, Type: "timeout"},
103 104
		{Name: "archive_timeout", Value: `30`, Type: "timeout"},
		{Name: "download_timeout", Value: `30`, Type: "timeout"},
105
		{Name: "preview_timeout", Value: `60`, Type: "timeout"},
106
		{Name: "doc_preview_timeout", Value: `60`, Type: "timeout"},
H
HFO4 已提交
107 108
		{Name: "upload_credential_timeout", Value: `1800`, Type: "timeout"},
		{Name: "upload_session_timeout", Value: `86400`, Type: "timeout"},
H
HFO4 已提交
109
		{Name: "slave_api_timeout", Value: `60`, Type: "timeout"},
H
HFO4 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
		{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: `<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; box-sizing: border-box; 
font-size: 14px; margin: 0;"><head><meta name="viewport"content="width=device-width"/><meta http-equiv="Content-Type"content="text/html; charset=UTF-8"/><title>容量超额提醒</title><style type="text/css">img{max-width:100%}body{-webkit-font-smoothing:antialiased;-webkit-text-size-adjust:none;width:100%!important;height:100%;line-height:1.6em}body{background-color:#f6f6f6}@media only screen and(max-width:640px){body{padding:0!important}h1{font-weight:800!important;margin:20px 0 5px!important}h2{font-weight:800!important;margin:20px 0 5px!important}h3{font-weight:800!important;margin:20px 0 5px!important}h4{font-weight:800!important;margin:20px 0 5px!important}h1{font-size:22px!important}h2{font-size:18px!important}h3{font-size:16px!important}.container{padding:0!important;width:100%!important}.content{padding:0!important}.content-wrap{padding:10px!important}.invoice{width:100%!important}}</style></head><body itemscope itemtype="http://schema.org/EmailMessage"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: 
border-box; font-size: 14px; -webkit-font-smoothing: antialiased; -webkit-text-size-adjust: none; width: 100% !important; height: 100%; line-height: 1.6em; background-color: #f6f6f6; margin: 0;"bgcolor="#f6f6f6"><table class="body-wrap"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; width: 100%; background-color: #f6f6f6; margin: 0;"bgcolor="#f6f6f6"><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; 
box-sizing: border-box; font-size: 14px; margin: 0;"><td style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0;"valign="top"></td><td class="container"width="600"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; display: block !important; max-width: 600px !important; clear: both !important; margin: 0 auto;"valign="top"><div class="content"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; max-width: 600px; display: block; margin: 0 auto; padding: 20px;"><table class="main"width="100%"cellpadding="0"cellspacing="0"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; border-radius: 3px; background-color: #fff; margin: 0; border: 1px 
solid #e9e9e9;"bgcolor="#fff"><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 
14px; margin: 0;"><td class="alert alert-warning"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 16px; vertical-align: top; color: #fff; font-weight: 500; text-align: center; border-radius: 3px 3px 0 0; background-color: #009688; margin: 0; padding: 20px;"align="center"bgcolor="#FF9F00"valign="top">激活{siteTitle}账户</td></tr><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-wrap"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 20px;"valign="top"><table width="100%"cellpadding="0"cellspacing="0"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-block"style="font-family: 'Helvetica 
Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;"valign="top">亲爱的<strong style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">{userName}</strong>:</td></tr><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-block"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;"valign="top">感谢您注册{siteTitle},请点击下方按钮完成账户激活。</td></tr><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-block"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;"valign="top"><a href="{activationUrl}"class="btn-primary"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; color: #FFF; text-decoration: none; line-height: 2em; font-weight: bold; text-align: center; cursor: pointer; display: inline-block; border-radius: 5px; text-transform: capitalize; background-color: #009688; margin: 0; border-color: #009688; border-style: solid; border-width: 10px 20px;">激活账户</a></td></tr><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-block"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;"valign="top">感谢您选择{siteTitle}。</td></tr></table></td></tr></table><div class="footer"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; width: 100%; clear: both; color: #999; margin: 0; padding: 20px;"><table width="100%"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="aligncenter content-block"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 12px; vertical-align: top; color: #999; text-align: center; margin: 0; padding: 0 0 20px;"align="center"valign="top">此邮件由系统自动发送,请不要直接回复。</td></tr></table></div></div></td><td style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0;"valign="top"></td></tr></table></body></html>`, Type: "mail_template"},
		{Name: "forget_captcha", Value: `0`, Type: "login"},
		{Name: "mail_reset_pwd_template", Value: `<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; box-sizing: border-box; 
font-size: 14px; margin: 0;"><head><meta name="viewport"content="width=device-width"/><meta http-equiv="Content-Type"content="text/html; charset=UTF-8"/><title>重设密码</title><style type="text/css">img{max-width:100%}body{-webkit-font-smoothing:antialiased;-webkit-text-size-adjust:none;width:100%!important;height:100%;line-height:1.6em}body{background-color:#f6f6f6}@media only screen and(max-width:640px){body{padding:0!important}h1{font-weight:800!important;margin:20px 0 5px!important}h2{font-weight:800!important;margin:20px 0 5px!important}h3{font-weight:800!important;margin:20px 0 5px!important}h4{font-weight:800!important;margin:20px 0 5px!important}h1{font-size:22px!important}h2{font-size:18px!important}h3{font-size:16px!important}.container{padding:0!important;width:100%!important}.content{padding:0!important}.content-wrap{padding:10px!important}.invoice{width:100%!important}}</style></head><body itemscope itemtype="http://schema.org/EmailMessage"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: 
border-box; font-size: 14px; -webkit-font-smoothing: antialiased; -webkit-text-size-adjust: none; width: 100% !important; height: 100%; line-height: 1.6em; background-color: #f6f6f6; margin: 0;"bgcolor="#f6f6f6"><table class="body-wrap"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; width: 100%; background-color: #f6f6f6; margin: 0;"bgcolor="#f6f6f6"><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; 
box-sizing: border-box; font-size: 14px; margin: 0;"><td style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0;"valign="top"></td><td class="container"width="600"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; display: block !important; max-width: 600px !important; clear: both !important; margin: 0 auto;"valign="top"><div class="content"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; max-width: 600px; display: block; margin: 0 auto; padding: 20px;"><table class="main"width="100%"cellpadding="0"cellspacing="0"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; border-radius: 3px; background-color: #fff; margin: 0; border: 1px 
solid #e9e9e9;"bgcolor="#fff"><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 
14px; margin: 0;"><td class="alert alert-warning"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 16px; vertical-align: top; color: #fff; font-weight: 500; text-align: center; border-radius: 3px 3px 0 0; background-color: #2196F3; margin: 0; padding: 20px;"align="center"bgcolor="#FF9F00"valign="top">重设{siteTitle}密码</td></tr><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-wrap"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 20px;"valign="top"><table width="100%"cellpadding="0"cellspacing="0"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-block"style="font-family: 'Helvetica 
Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;"valign="top">亲爱的<strong style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">{userName}</strong>:</td></tr><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-block"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;"valign="top">请点击下方按钮完成密码重设。如果非你本人操作,请忽略此邮件。</td></tr><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-block"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;"valign="top"><a href="{resetUrl}"class="btn-primary"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; color: #FFF; text-decoration: none; line-height: 2em; font-weight: bold; text-align: center; cursor: pointer; display: inline-block; border-radius: 5px; text-transform: capitalize; background-color: #2196F3; margin: 0; border-color: #2196F3; border-style: solid; border-width: 10px 20px;">重设密码</a></td></tr><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-block"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;"valign="top">感谢您选择{siteTitle}。</td></tr></table></td></tr></table><div class="footer"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; width: 100%; clear: both; color: #999; margin: 0; padding: 20px;"><table width="100%"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="aligncenter content-block"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 12px; vertical-align: top; color: #999; text-align: center; margin: 0; padding: 0 0 20px;"align="center"valign="top">此邮件由系统自动发送,请不要直接回复。</td></tr></table></div></div></td><td style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0;"valign="top"></td></tr></table></body></html>`, 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: `<script type="text/javascript"></script>`, Type: "basic"},
		{Name: "sendfile", Value: `0`, Type: "download"},
		{Name: "defaultTheme", Value: `#3f51b5`, Type: "basic"},
		{Name: "header", Value: `X-Sendfile`, Type: "download"},
151
		{Name: "themes", Value: `{"#3f51b5":{"palette":{"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"},"explorer":{"filename":"#474849","icon":"#8f8f8f","bgSelected":"#D5DAF0","emptyIcon":"#e8e8e8"}}}}`, Type: "basic"},
H
HFO4 已提交
152 153 154 155 156 157 158
		{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"},
H
HFO4 已提交
159
		{Name: "secret_key", Value: util.RandStringRunes(256), Type: "auth"},
H
HFO4 已提交
160
		{Name: "temp_path", Value: "temp", Type: "path"},
H
HFO4 已提交
161 162 163 164 165 166 167
	}

	for _, value := range defaultSettings {
		DB.Where(Setting{Name: value.Name}).FirstOrCreate(&value)
	}
}

H
HFO4 已提交
168 169 170 171 172
func addDefaultGroups() {
	_, err := GetGroupByID(1)
	// 未找到初始管理组时,则创建
	if gorm.IsRecordNotFoundError(err) {
		defaultAdminGroup := Group{
H
HFO4 已提交
173 174 175 176 177 178 179
			Name:          "管理员",
			PolicyList:    []uint{1},
			MaxStorage:    1 * 1024 * 1024 * 1024,
			ShareEnabled:  true,
			Color:         "danger",
			WebDAVEnabled: true,
			Aria2Option:   "0,0,0",
H
HFO4 已提交
180 181 182 183
			OptionsSerialized: GroupOption{
				ArchiveDownloadEnabled: true,
				ArchiveTaskEnabled:     true,
			},
H
HFO4 已提交
184 185
		}
		if err := DB.Create(&defaultAdminGroup).Error; err != nil {
H
HFO4 已提交
186
			util.Log().Panic("无法创建管理用户组, %s", err)
H
HFO4 已提交
187 188 189 190 191 192 193 194
		}
	}

	err = nil
	_, err = GetGroupByID(2)
	// 未找到初始注册会员时,则创建
	if gorm.IsRecordNotFoundError(err) {
		defaultAdminGroup := Group{
H
HFO4 已提交
195 196 197 198 199 200 201
			Name:          "注册会员",
			PolicyList:    []uint{1},
			MaxStorage:    1 * 1024 * 1024 * 1024,
			ShareEnabled:  true,
			Color:         "danger",
			WebDAVEnabled: true,
			Aria2Option:   "0,0,0",
H
HFO4 已提交
202 203
		}
		if err := DB.Create(&defaultAdminGroup).Error; err != nil {
H
HFO4 已提交
204
			util.Log().Panic("无法创建初始注册会员用户组, %s", err)
H
HFO4 已提交
205 206
		}
	}
H
HFO4 已提交
207 208 209 210 211 212

	err = nil
	_, err = GetGroupByID(3)
	// 未找到初始游客用户组时,则创建
	if gorm.IsRecordNotFoundError(err) {
		defaultAdminGroup := Group{
H
HFO4 已提交
213 214
			Name:     "游客",
			Policies: "[]",
H
HFO4 已提交
215 216 217 218 219
		}
		if err := DB.Create(&defaultAdminGroup).Error; err != nil {
			util.Log().Panic("无法创建初始游客用户组, %s", err)
		}
	}
H
HFO4 已提交
220 221
}

H
HFO4 已提交
222
func addDefaultUser() {
H
HFO4 已提交
223
	_, err := GetUserByID(1)
H
HFO4 已提交
224 225

	// 未找到初始用户时,则创建
H
HFO4 已提交
226 227 228 229 230 231
	if gorm.IsRecordNotFoundError(err) {
		defaultUser := NewUser()
		//TODO 动态生成密码
		defaultUser.Email = "admin@cloudreve.org"
		defaultUser.Nick = "admin"
		defaultUser.Status = Active
H
HFO4 已提交
232
		defaultUser.GroupID = 1
H
HFO4 已提交
233 234 235
		defaultUser.PrimaryGroup = 1
		err := defaultUser.SetPassword("admin")
		if err != nil {
H
HFO4 已提交
236
			util.Log().Panic("无法创建密码, %s", err)
H
HFO4 已提交
237 238
		}
		if err := DB.Create(&defaultUser).Error; err != nil {
H
HFO4 已提交
239
			util.Log().Panic("无法创建初始用户, %s", err)
H
HFO4 已提交
240 241
		}
	}
H
HFO4 已提交
242
}