2021-10-27 14:45:36 +00:00
|
|
|
package bootstrap
|
2021-10-25 10:53:59 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/Xhofe/alist/conf"
|
2021-10-27 14:45:36 +00:00
|
|
|
"github.com/Xhofe/alist/drivers"
|
|
|
|
"github.com/Xhofe/alist/model"
|
|
|
|
"github.com/Xhofe/alist/utils"
|
2021-10-25 10:53:59 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"gorm.io/driver/mysql"
|
|
|
|
"gorm.io/driver/postgres"
|
|
|
|
"gorm.io/driver/sqlite"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"gorm.io/gorm/schema"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func InitModel() {
|
|
|
|
log.Infof("init model...")
|
|
|
|
config := conf.Conf.Database
|
|
|
|
switch config.Type {
|
|
|
|
case "sqlite3":
|
|
|
|
{
|
|
|
|
if !(strings.HasSuffix(config.DBFile, ".db") && len(config.DBFile) > 3) {
|
|
|
|
log.Fatalf("db name error.")
|
|
|
|
}
|
|
|
|
db, err := gorm.Open(sqlite.Open(config.DBFile), &gorm.Config{
|
|
|
|
NamingStrategy: schema.NamingStrategy{
|
|
|
|
TablePrefix: config.TablePrefix,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to connect database:%s", err.Error())
|
|
|
|
}
|
|
|
|
conf.DB = db
|
|
|
|
}
|
|
|
|
case "mysql":
|
|
|
|
{
|
|
|
|
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
|
|
|
|
config.User, config.Password, config.Host, config.Port, config.Name)
|
|
|
|
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
|
|
|
|
NamingStrategy: schema.NamingStrategy{
|
|
|
|
TablePrefix: config.TablePrefix,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to connect database:%s", err.Error())
|
|
|
|
}
|
|
|
|
conf.DB = db
|
|
|
|
}
|
|
|
|
case "postgres":
|
|
|
|
{
|
|
|
|
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=disable TimeZone=Asia/Shanghai",
|
|
|
|
config.Host, config.User, config.Password, config.Name, config.Port)
|
|
|
|
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
|
|
|
|
NamingStrategy: schema.NamingStrategy{
|
|
|
|
TablePrefix: config.TablePrefix,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed to connect database:%s", err.Error())
|
|
|
|
}
|
|
|
|
conf.DB = db
|
|
|
|
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
log.Fatalf("not supported database type: %s", config.Type)
|
|
|
|
}
|
|
|
|
log.Infof("auto migrate model")
|
2021-10-29 16:35:29 +00:00
|
|
|
err := conf.DB.AutoMigrate(&model.SettingItem{}, &model.Account{}, &model.Meta{})
|
2021-10-25 10:53:59 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to auto migrate")
|
|
|
|
}
|
2021-10-26 14:28:37 +00:00
|
|
|
|
2021-10-27 10:59:03 +00:00
|
|
|
// TODO init filetype
|
2021-10-26 14:28:37 +00:00
|
|
|
initAccounts()
|
2021-10-27 10:59:03 +00:00
|
|
|
initSettings()
|
2021-10-26 14:28:37 +00:00
|
|
|
}
|
2021-10-27 14:45:36 +00:00
|
|
|
|
|
|
|
func initAccounts() {
|
|
|
|
log.Infof("init accounts...")
|
|
|
|
var accounts []model.Account
|
|
|
|
if err := conf.DB.Find(&accounts).Error; err != nil {
|
|
|
|
log.Fatalf("failed sync init accounts")
|
|
|
|
}
|
|
|
|
for _, account := range accounts {
|
|
|
|
model.RegisterAccount(account)
|
|
|
|
driver, ok := drivers.GetDriver(account.Type)
|
|
|
|
if !ok {
|
2021-10-28 14:50:09 +00:00
|
|
|
log.Errorf("no [%s] driver", driver)
|
2021-10-27 14:45:36 +00:00
|
|
|
} else {
|
|
|
|
err := driver.Save(&account, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("init account [%s] error:[%s]", account.Name, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func initSettings() {
|
|
|
|
log.Infof("init settings...")
|
|
|
|
version, err := model.GetSettingByKey("version")
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("first run")
|
|
|
|
version = &model.SettingItem{
|
|
|
|
Key: "version",
|
|
|
|
Value: "0.0.0",
|
2021-10-29 16:35:29 +00:00
|
|
|
Type: "string",
|
2021-10-27 14:45:36 +00:00
|
|
|
Description: "version",
|
2021-10-29 16:35:29 +00:00
|
|
|
Group: model.CONST,
|
2021-10-27 14:45:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
settingsMap := map[string][]model.SettingItem{
|
|
|
|
"2.0.0": {
|
|
|
|
{
|
|
|
|
Key: "title",
|
|
|
|
Value: "Alist",
|
|
|
|
Description: "title",
|
2021-10-29 16:35:29 +00:00
|
|
|
Type: "string",
|
|
|
|
Group: model.PUBLIC,
|
2021-10-27 14:45:36 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "password",
|
|
|
|
Value: "alist",
|
2021-10-29 16:35:29 +00:00
|
|
|
Type: "string",
|
2021-10-27 14:45:36 +00:00
|
|
|
Description: "password",
|
2021-10-29 16:35:29 +00:00
|
|
|
Group: model.PRIVATE,
|
2021-10-27 14:45:36 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "version",
|
|
|
|
Value: "2.0.0",
|
2021-10-29 16:35:29 +00:00
|
|
|
Type: "string",
|
2021-10-27 14:45:36 +00:00
|
|
|
Description: "version",
|
2021-10-29 16:35:29 +00:00
|
|
|
Group: model.CONST,
|
2021-10-27 14:45:36 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "logo",
|
2021-10-30 11:26:23 +00:00
|
|
|
Value: "https://store.heytapimage.com/cdo-portal/feedback/202110/30/d43c41c5d257c9bc36366e310374fb19.png",
|
2021-10-29 16:35:29 +00:00
|
|
|
Type: "string",
|
2021-10-27 14:45:36 +00:00
|
|
|
Description: "logo",
|
2021-10-29 16:35:29 +00:00
|
|
|
Group: model.PUBLIC,
|
2021-10-27 14:45:36 +00:00
|
|
|
},
|
2021-10-30 09:34:34 +00:00
|
|
|
{
|
2021-10-30 11:26:23 +00:00
|
|
|
Key: "icon color",
|
2021-10-30 09:34:34 +00:00
|
|
|
Value: "blue.400",
|
|
|
|
Type: "string",
|
|
|
|
Description: "icon's color",
|
|
|
|
Group: model.PUBLIC,
|
|
|
|
},
|
2021-10-30 11:26:23 +00:00
|
|
|
{
|
|
|
|
Key: "text types",
|
|
|
|
Value: "txt,htm,html,xml,java,properties,sql,js,md,json,conf,ini,vue,php,py,bat,gitignore,yml,go,sh,c,cpp,h,hpp",
|
|
|
|
Type: "string",
|
|
|
|
Description: "text type extensions",
|
|
|
|
},
|
2021-10-27 14:45:36 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for k, v := range settingsMap {
|
|
|
|
if utils.VersionCompare(k, version.Value) > 0 {
|
|
|
|
log.Infof("writing [v%s] settings", k)
|
|
|
|
err = model.SaveSettings(v)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("save settings error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-30 11:26:23 +00:00
|
|
|
textTypes, err := model.GetSettingByKey("text types")
|
|
|
|
if err==nil{
|
|
|
|
conf.ImageTypes = strings.Split(textTypes.Value,",")
|
|
|
|
}
|
2021-10-27 14:45:36 +00:00
|
|
|
}
|