alist/bootstrap/model.go

231 lines
5.9 KiB
Go
Raw Normal View History

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 14:45:36 +00:00
2021-11-16 07:00:56 +00:00
func InitAccounts() {
2021-10-27 14:45:36 +00:00
log.Infof("init accounts...")
var accounts []model.Account
if err := conf.DB.Find(&accounts).Error; err != nil {
log.Fatalf("failed sync init accounts")
}
2021-11-15 06:53:32 +00:00
for i, account := range accounts {
2021-10-27 14:45:36 +00:00
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 {
2021-11-15 06:53:32 +00:00
err := driver.Save(&accounts[i], nil)
2021-10-27 14:45:36 +00:00
if err != nil {
log.Errorf("init account [%s] error:[%s]", account.Name, err.Error())
2021-11-16 07:00:56 +00:00
} else {
log.Infof("success init account: %s, type: %s", account.Name, account.Type)
2021-10-27 14:45:36 +00:00
}
}
}
}
2021-11-16 07:00:56 +00:00
func InitSettings() {
2021-10-27 14:45:36 +00:00
log.Infof("init settings...")
2021-11-04 05:23:17 +00:00
version := model.SettingItem{
Key: "version",
Value: conf.GitTag,
Description: "version",
2021-11-13 16:30:15 +00:00
Type: "string",
2021-11-04 05:23:17 +00:00
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",
2021-11-12 11:39:01 +00:00
Type: "string",
2021-11-04 05:23:17 +00:00
Group: model.PUBLIC,
},
{
Key: "password",
Value: "alist",
Description: "password",
2021-11-12 11:39:01 +00:00
Type: "string",
2021-11-04 05:23:17 +00:00
Group: model.PRIVATE,
},
{
Key: "logo",
Value: "https://store.heytapimage.com/cdo-portal/feedback/202110/30/d43c41c5d257c9bc36366e310374fb19.png",
Description: "logo",
2021-11-12 11:39:01 +00:00
Type: "string",
2021-11-04 05:23:17 +00:00
Group: model.PUBLIC,
},
{
Key: "favicon",
Value: "https://store.heytapimage.com/cdo-portal/feedback/202110/30/d43c41c5d257c9bc36366e310374fb19.png",
Description: "favicon",
2021-11-12 11:39:01 +00:00
Type: "string",
2021-11-04 05:23:17 +00:00
Group: model.PUBLIC,
},
{
Key: "icon color",
Value: "teal.300",
Description: "icon's color",
2021-11-12 11:39:01 +00:00
Type: "string",
2021-11-04 05:23:17 +00:00
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",
2021-11-12 11:39:01 +00:00
Type: "string",
2021-11-04 05:23:17 +00:00
Description: "text type extensions",
},
{
2021-11-12 11:39:01 +00:00
Key: "hide readme file",
Value: "true",
Type: "bool",
Description: "hide readme file? ",
2021-11-04 05:23:17 +00:00
},
{
Key: "music cover",
Value: "https://store.heytapimage.com/cdo-portal/feedback/202110/30/d43c41c5d257c9bc36366e310374fb19.png",
Description: "music cover image",
2021-11-12 11:39:01 +00:00
Type: "string",
2021-11-04 05:23:17 +00:00
Group: model.PUBLIC,
},
{
Key: "site beian",
Description: "chinese beian info",
2021-11-12 11:39:01 +00:00
Type: "string",
2021-11-04 05:23:17 +00:00
Group: model.PUBLIC,
},
{
Key: "home readme url",
Description: "when have multiple, the readme file to show",
2021-11-12 11:39:01 +00:00
Type: "string",
2021-11-04 05:23:17 +00:00
Group: model.PUBLIC,
2021-10-27 14:45:36 +00:00
},
2021-11-06 09:33:00 +00:00
{
2021-11-12 11:39:01 +00:00
Key: "markdown theme",
Value: "vuepress",
2021-11-06 09:33:00 +00:00
Description: "default | github | vuepress",
2021-11-12 11:39:01 +00:00
Group: model.PUBLIC,
Type: "select",
Values: "default,github,vuepress",
2021-11-06 09:33:00 +00:00
},
2021-11-12 14:56:30 +00:00
{
2021-11-13 16:30:15 +00:00
Key: "autoplay video",
Value: "false",
Type: "bool",
2021-11-12 14:56:30 +00:00
},
{
2021-11-13 16:30:15 +00:00
Key: "autoplay audio",
Value: "false",
Type: "bool",
2021-11-12 14:56:30 +00:00
},
{
Key: "check parent folder",
2021-11-12 15:03:39 +00:00
Value: "false",
2021-11-12 14:56:30 +00:00
Type: "bool",
Description: "check parent folder password",
},
{
2021-11-16 07:00:56 +00:00
Key: "customize style",
Value: "",
Type: "text",
Description: "customize style, don't need add <style></style>",
},
{
2021-11-16 07:00:56 +00:00
Key: "customize script",
Value: "",
Type: "text",
Description: "customize script, don't need add <script></script>",
},
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-11-12 14:56:30 +00:00
model.LoadSettings()
2021-10-27 14:45:36 +00:00
}