migration.go 29.5 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 7
	"github.com/jinzhu/gorm"
)
H
HFO4 已提交
8

9 10 11 12 13 14 15
// 是否需要迁移
func needMigration() bool {
	var setting Setting
	DB.Where("name = ?", "database_version").First(&setting)
	return setting.Value != conf.RequiredDBVersion
}

H
HFO4 已提交
16
//执行数据迁移
H
HFO4 已提交
17
func migration() {
18 19 20 21 22
	// 确认是否需要执行迁移
	if !needMigration() {
		util.Log().Info("数据库版本匹配,跳过数据库迁移")
		return

23 24
	}

25
	util.Log().Info("开始进行数据库初始化...")
26

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

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

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

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

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

46
	util.Log().Info("数据库初始化结束")
H
HFO4 已提交
47

H
HFO4 已提交
48 49
}

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

H
HFO4 已提交
74 75
func addDefaultSettings() {
	defaultSettings := []Setting{
76
		{Name: "siteURL", Value: `http://localhost`, Type: "basic"},
H
HFO4 已提交
77
		{Name: "siteName", Value: `Cloudreve`, Type: "basic"},
H
HFO4 已提交
78 79
		{Name: "register_enabled", Value: `1`, Type: "register"},
		{Name: "default_group", Value: `2`, Type: "register"},
H
HFO4 已提交
80 81 82 83
		{Name: "siteKeywords", Value: `网盘,网盘`, Type: "basic"},
		{Name: "siteDes", Value: `Cloudreve`, Type: "basic"},
		{Name: "siteTitle", Value: `平步云端`, Type: "basic"},
		{Name: "fromName", Value: `Cloudreve`, Type: "mail"},
H
HFO4 已提交
84
		{Name: "mail_keepalive", Value: `30`, Type: "mail"},
H
HFO4 已提交
85 86 87 88 89 90 91 92 93 94 95
		{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 已提交
96
		{Name: "oss_timeout", Value: `3600`, Type: "timeout"},
97 98
		{Name: "archive_timeout", Value: `30`, Type: "timeout"},
		{Name: "download_timeout", Value: `30`, Type: "timeout"},
99
		{Name: "preview_timeout", Value: `60`, Type: "timeout"},
100
		{Name: "doc_preview_timeout", Value: `60`, Type: "timeout"},
H
HFO4 已提交
101 102
		{Name: "upload_credential_timeout", Value: `1800`, Type: "timeout"},
		{Name: "upload_session_timeout", Value: `86400`, Type: "timeout"},
H
HFO4 已提交
103
		{Name: "slave_api_timeout", Value: `60`, Type: "timeout"},
104
		{Name: "onedrive_monitor_timeout", Value: `600`, Type: "timeout"},
105
		{Name: "share_download_session_timeout", Value: `2073600`, Type: "timeout"},
106
		{Name: "onedrive_callback_check", Value: `20`, Type: "timeout"},
H
HFO4 已提交
107
		{Name: "aria2_call_timeout", Value: `5`, Type: "timeout"},
H
HFO4 已提交
108
		{Name: "onedrive_chunk_retries", Value: `1`, Type: "retry"},
H
HFO4 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
		{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: "pack_data", Value: `[]`, Type: "pack"},
132
		{Name: "database_version", Value: `3.0.0-beta1`, Type: "version"},
H
HFO4 已提交
133 134
		{Name: "alipay_enabled", Value: `0`, Type: "payment"},
		{Name: "payjs_enabled", Value: `0`, Type: "payment"},
H
HFO4 已提交
135 136
		{Name: "payjs_id", Value: ``, Type: "payment"},
		{Name: "payjs_secret", Value: ``, Type: "payment"},
H
HFO4 已提交
137 138 139 140 141 142
		{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"},
H
HFO4 已提交
143
		{Name: "gravatar_server", Value: `https://gravatar.loli.net/`, Type: "avatar"},
H
HFO4 已提交
144
		{Name: "defaultTheme", Value: `#3f51b5`, Type: "basic"},
H
HFO4 已提交
145
		{Name: "themes", Value: `{"#3f51b5":{"palette":{"primary":{"main":"#3f51b5"},"secondary":{"main":"#f50057"}}},"#2196f3":{"palette":{"primary":{"main":"#2196f3"},"secondary":{"main":"#FFC107"}}},"#673AB7":{"palette":{"primary":{"main":"#673AB7"},"secondary":{"main":"#2196F3"}}},"#E91E63":{"palette":{"primary":{"main":"#E91E63"},"secondary":{"main":"#42A5F5","contrastText":"#fff"}}},"#FF5722":{"palette":{"primary":{"main":"#FF5722"},"secondary":{"main":"#3F51B5"}}},"#FFC107":{"palette":{"primary":{"main":"#FFC107"},"secondary":{"main":"#26C6DA"}}},"#8BC34A":{"palette":{"primary":{"main":"#8BC34A","contrastText":"#fff"},"secondary":{"main":"#FF8A65","contrastText":"#fff"}}},"#009688":{"palette":{"primary":{"main":"#009688"},"secondary":{"main":"#4DD0E1","contrastText":"#fff"}}},"#607D8B":{"palette":{"primary":{"main":"#607D8B"},"secondary":{"main":"#F06292"}}},"#795548":{"palette":{"primary":{"main":"#795548"},"secondary":{"main":"#4CAF50","contrastText":"#fff"}}}}`, Type: "basic"},
H
HFO4 已提交
146
		{Name: "aria2_token", Value: `your token`, Type: "aria2"},
H
HFO4 已提交
147
		{Name: "aria2_temp_path", Value: ``, Type: "aria2"},
148
		{Name: "aria2_options", Value: `[]`, Type: "aria2"},
H
HFO4 已提交
149
		{Name: "aria2_interval", Value: `10`, Type: "aria2"},
H
HFO4 已提交
150
		{Name: "max_worker_num", Value: `10`, Type: "task"},
H
HFO4 已提交
151
		{Name: "max_parallel_transfer", Value: `4`, Type: "task"},
H
HFO4 已提交
152
		{Name: "secret_key", Value: util.RandStringRunes(256), Type: "auth"},
H
HFO4 已提交
153
		{Name: "temp_path", Value: "temp", Type: "path"},
H
HFO4 已提交
154 155 156 157 158
		{Name: "avatar_path", Value: "avatar", Type: "path"},
		{Name: "avatar_size", Value: "2097152", Type: "avatar"},
		{Name: "avatar_size_l", Value: "200", Type: "avatar"},
		{Name: "avatar_size_m", Value: "130", Type: "avatar"},
		{Name: "avatar_size_s", Value: "50", Type: "avatar"},
H
HFO4 已提交
159 160
		{Name: "score_enabled", Value: "1", Type: "score"},
		{Name: "share_score_rate", Value: "80", Type: "score"},
H
HFO4 已提交
161
		{Name: "score_price", Value: "1", Type: "score"},
162 163
		{Name: "home_view_method", Value: "icon", Type: "view"},
		{Name: "share_view_method", Value: "list", Type: "view"},
H
HFO4 已提交
164 165 166
		{Name: "cron_garbage_collect", Value: "@hourly", Type: "cron"},
		{Name: "cron_notify_user", Value: "@hourly", Type: "cron"},
		{Name: "cron_ban_user", Value: "@hourly", Type: "cron"},
H
HFO4 已提交
167
		{Name: "authn_enabled", Value: "1", Type: "authn"},
168 169 170 171 172 173 174 175 176 177 178 179 180
		{Name: "captcha_height", Value: "60", Type: "captcha"},
		{Name: "captcha_width", Value: "240", Type: "captcha"},
		{Name: "captcha_mode", Value: "3", Type: "captcha"},
		{Name: "captcha_ComplexOfNoiseText", Value: "0", Type: "captcha"},
		{Name: "captcha_ComplexOfNoiseDot", Value: "0", Type: "captcha"},
		{Name: "captcha_IsShowHollowLine", Value: "0", Type: "captcha"},
		{Name: "captcha_IsShowNoiseDot", Value: "0", Type: "captcha"},
		{Name: "captcha_IsShowNoiseText", Value: "0", Type: "captcha"},
		{Name: "captcha_IsShowSlimeLine", Value: "0", Type: "captcha"},
		{Name: "captcha_IsShowSineLine", Value: "0", Type: "captcha"},
		{Name: "captcha_CaptchaLen", Value: "6", Type: "captcha"},
		{Name: "thumb_width", Value: "400", Type: "thumb"},
		{Name: "thumb_height", Value: "300", Type: "thumb"},
H
HFO4 已提交
181 182 183
	}

	for _, value := range defaultSettings {
H
HFO4 已提交
184
		DB.Where(Setting{Name: value.Name}).Create(&value)
H
HFO4 已提交
185 186 187
	}
}

H
HFO4 已提交
188 189 190 191 192
func addDefaultGroups() {
	_, err := GetGroupByID(1)
	// 未找到初始管理组时,则创建
	if gorm.IsRecordNotFoundError(err) {
		defaultAdminGroup := Group{
H
HFO4 已提交
193 194 195 196 197 198
			Name:          "管理员",
			PolicyList:    []uint{1},
			MaxStorage:    1 * 1024 * 1024 * 1024,
			ShareEnabled:  true,
			Color:         "danger",
			WebDAVEnabled: true,
H
HFO4 已提交
199
			OptionsSerialized: GroupOption{
H
HFO4 已提交
200 201 202
				ArchiveDownload: true,
				ArchiveTask:     true,
				ShareDownload:   true,
H
HFO4 已提交
203 204
				ShareFree:       true,
				Aria2:           true,
H
HFO4 已提交
205
			},
H
HFO4 已提交
206 207
		}
		if err := DB.Create(&defaultAdminGroup).Error; err != nil {
H
HFO4 已提交
208
			util.Log().Panic("无法创建管理用户组, %s", err)
H
HFO4 已提交
209 210 211 212 213 214 215 216
		}
	}

	err = nil
	_, err = GetGroupByID(2)
	// 未找到初始注册会员时,则创建
	if gorm.IsRecordNotFoundError(err) {
		defaultAdminGroup := Group{
H
HFO4 已提交
217 218 219 220 221 222
			Name:          "注册会员",
			PolicyList:    []uint{1},
			MaxStorage:    1 * 1024 * 1024 * 1024,
			ShareEnabled:  true,
			Color:         "danger",
			WebDAVEnabled: true,
H
HFO4 已提交
223 224
		}
		if err := DB.Create(&defaultAdminGroup).Error; err != nil {
H
HFO4 已提交
225
			util.Log().Panic("无法创建初始注册会员用户组, %s", err)
H
HFO4 已提交
226 227
		}
	}
H
HFO4 已提交
228 229 230 231 232 233

	err = nil
	_, err = GetGroupByID(3)
	// 未找到初始游客用户组时,则创建
	if gorm.IsRecordNotFoundError(err) {
		defaultAdminGroup := Group{
H
HFO4 已提交
234 235
			Name:     "游客",
			Policies: "[]",
H
HFO4 已提交
236 237 238 239 240
		}
		if err := DB.Create(&defaultAdminGroup).Error; err != nil {
			util.Log().Panic("无法创建初始游客用户组, %s", err)
		}
	}
H
HFO4 已提交
241 242
}

H
HFO4 已提交
243
func addDefaultUser() {
H
HFO4 已提交
244
	_, err := GetUserByID(1)
H
HFO4 已提交
245 246

	// 未找到初始用户时,则创建
H
HFO4 已提交
247 248 249 250 251 252
	if gorm.IsRecordNotFoundError(err) {
		defaultUser := NewUser()
		//TODO 动态生成密码
		defaultUser.Email = "admin@cloudreve.org"
		defaultUser.Nick = "admin"
		defaultUser.Status = Active
H
HFO4 已提交
253
		defaultUser.GroupID = 1
H
HFO4 已提交
254 255
		err := defaultUser.SetPassword("admin")
		if err != nil {
H
HFO4 已提交
256
			util.Log().Panic("无法创建密码, %s", err)
H
HFO4 已提交
257 258
		}
		if err := DB.Create(&defaultUser).Error; err != nil {
H
HFO4 已提交
259
			util.Log().Panic("无法创建初始用户, %s", err)
H
HFO4 已提交
260 261
		}
	}
H
HFO4 已提交
262
}