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"
|
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"
|
2021-11-01 15:14:34 +00:00
|
|
|
"gorm.io/gorm/logger"
|
2021-10-25 10:53:59 +00:00
|
|
|
"gorm.io/gorm/schema"
|
2021-11-01 15:14:34 +00:00
|
|
|
log2 "log"
|
|
|
|
"os"
|
2021-10-25 10:53:59 +00:00
|
|
|
"strings"
|
2021-11-01 15:14:34 +00:00
|
|
|
"time"
|
2021-10-25 10:53:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func InitModel() {
|
|
|
|
log.Infof("init model...")
|
2021-11-01 15:14:34 +00:00
|
|
|
databaseConfig := conf.Conf.Database
|
|
|
|
newLogger := logger.New(
|
|
|
|
log2.New(os.Stdout, "\r\n", log2.LstdFlags),
|
|
|
|
logger.Config{
|
|
|
|
SlowThreshold: time.Second,
|
|
|
|
LogLevel: logger.Silent,
|
|
|
|
IgnoreRecordNotFoundError: true,
|
|
|
|
Colorful: true,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
gormConfig := &gorm.Config{
|
|
|
|
NamingStrategy: schema.NamingStrategy{
|
|
|
|
TablePrefix: databaseConfig.TablePrefix,
|
|
|
|
},
|
|
|
|
Logger: newLogger,
|
|
|
|
}
|
|
|
|
switch databaseConfig.Type {
|
2021-10-25 10:53:59 +00:00
|
|
|
case "sqlite3":
|
|
|
|
{
|
2021-11-01 15:14:34 +00:00
|
|
|
if !(strings.HasSuffix(databaseConfig.DBFile, ".db") && len(databaseConfig.DBFile) > 3) {
|
2021-10-25 10:53:59 +00:00
|
|
|
log.Fatalf("db name error.")
|
|
|
|
}
|
2021-11-01 15:14:34 +00:00
|
|
|
db, err := gorm.Open(sqlite.Open(databaseConfig.DBFile), gormConfig)
|
2021-10-25 10:53:59 +00:00
|
|
|
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",
|
2021-11-01 15:14:34 +00:00
|
|
|
databaseConfig.User, databaseConfig.Password, databaseConfig.Host, databaseConfig.Port, databaseConfig.Name)
|
|
|
|
db, err := gorm.Open(mysql.Open(dsn), gormConfig)
|
2021-10-25 10:53:59 +00:00
|
|
|
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",
|
2021-11-01 15:14:34 +00:00
|
|
|
databaseConfig.Host, databaseConfig.User, databaseConfig.Password, databaseConfig.Name, databaseConfig.Port)
|
|
|
|
db, err := gorm.Open(postgres.Open(dsn), gormConfig)
|
2021-10-25 10:53:59 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed to connect database:%s", err.Error())
|
|
|
|
}
|
|
|
|
conf.DB = db
|
|
|
|
|
|
|
|
}
|
|
|
|
default:
|
2021-11-01 15:14:34 +00:00
|
|
|
log.Fatalf("not supported database type: %s", databaseConfig.Type)
|
2021-10-25 10:53:59 +00:00
|
|
|
}
|
|
|
|
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...")
|
2021-11-04 05:23:17 +00:00
|
|
|
version := model.SettingItem{
|
|
|
|
Key: "version",
|
|
|
|
Value: conf.GitTag,
|
|
|
|
Description: "version",
|
|
|
|
Group: model.CONST,
|
2021-10-27 14:45:36 +00:00
|
|
|
}
|
2021-11-04 05:23:17 +00:00
|
|
|
|
|
|
|
_ = model.SaveSetting(version)
|
|
|
|
|
|
|
|
settings := []model.SettingItem{
|
|
|
|
{
|
|
|
|
Key: "title",
|
|
|
|
Value: "Alist",
|
|
|
|
Description: "title",
|
|
|
|
Group: model.PUBLIC,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "password",
|
|
|
|
Value: "alist",
|
|
|
|
Description: "password",
|
|
|
|
Group: model.PRIVATE,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "logo",
|
|
|
|
Value: "https://store.heytapimage.com/cdo-portal/feedback/202110/30/d43c41c5d257c9bc36366e310374fb19.png",
|
|
|
|
Description: "logo",
|
|
|
|
Group: model.PUBLIC,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "favicon",
|
|
|
|
Value: "https://store.heytapimage.com/cdo-portal/feedback/202110/30/d43c41c5d257c9bc36366e310374fb19.png",
|
|
|
|
Description: "favicon",
|
|
|
|
Group: model.PUBLIC,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "icon color",
|
|
|
|
Value: "teal.300",
|
|
|
|
Description: "icon's color",
|
|
|
|
Group: model.PUBLIC,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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",
|
|
|
|
Description: "text type extensions",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "readme file",
|
|
|
|
Value: "hide",
|
|
|
|
Description: "hide readme file? (show/hide)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "music cover",
|
|
|
|
Value: "https://store.heytapimage.com/cdo-portal/feedback/202110/30/d43c41c5d257c9bc36366e310374fb19.png",
|
|
|
|
Description: "music cover image",
|
|
|
|
Group: model.PUBLIC,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "site beian",
|
|
|
|
Description: "chinese beian info",
|
|
|
|
Group: model.PUBLIC,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "home readme url",
|
|
|
|
Description: "when have multiple, the readme file to show",
|
|
|
|
Group: model.PUBLIC,
|
2021-10-27 14:45:36 +00:00
|
|
|
},
|
|
|
|
}
|
2021-11-04 05:23:17 +00:00
|
|
|
for _, v := range settings {
|
|
|
|
_, err := model.GetSettingByKey(v.Key)
|
|
|
|
if err == gorm.ErrRecordNotFound {
|
|
|
|
err = model.SaveSetting(v)
|
2021-10-27 14:45:36 +00:00
|
|
|
if err != nil {
|
2021-11-04 05:23:17 +00:00
|
|
|
log.Fatalf("failed write setting: %s", err.Error())
|
2021-10-27 14:45:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-30 11:26:23 +00:00
|
|
|
textTypes, err := model.GetSettingByKey("text types")
|
2021-11-01 14:42:24 +00:00
|
|
|
if err == nil {
|
|
|
|
conf.TextTypes = strings.Split(textTypes.Value, ",")
|
2021-10-30 11:26:23 +00:00
|
|
|
}
|
2021-10-27 14:45:36 +00:00
|
|
|
}
|