2021-10-26 14:28:37 +00:00
|
|
|
package model
|
|
|
|
|
2021-10-27 10:59:03 +00:00
|
|
|
import (
|
2021-12-07 12:16:34 +00:00
|
|
|
"fmt"
|
2021-10-27 10:59:03 +00:00
|
|
|
"github.com/Xhofe/alist/conf"
|
2021-12-07 12:16:34 +00:00
|
|
|
"github.com/Xhofe/alist/utils"
|
2021-11-12 14:56:30 +00:00
|
|
|
"strings"
|
2021-10-27 10:59:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
PUBLIC = iota
|
|
|
|
PRIVATE
|
|
|
|
CONST
|
|
|
|
)
|
2021-10-26 14:28:37 +00:00
|
|
|
|
2021-12-28 08:38:56 +00:00
|
|
|
const (
|
|
|
|
FRONT = iota
|
|
|
|
BACK
|
|
|
|
OTHER
|
|
|
|
)
|
|
|
|
|
2021-10-26 14:28:37 +00:00
|
|
|
type SettingItem struct {
|
2021-11-13 07:53:26 +00:00
|
|
|
Key string `json:"key" gorm:"primaryKey" binding:"required"`
|
2021-10-26 14:28:37 +00:00
|
|
|
Value string `json:"value"`
|
|
|
|
Description string `json:"description"`
|
2021-11-12 11:39:01 +00:00
|
|
|
Type string `json:"type"`
|
2021-10-29 16:35:29 +00:00
|
|
|
Group int `json:"group"`
|
2021-12-28 08:38:56 +00:00
|
|
|
Access int `json:"access"`
|
2021-11-12 11:39:01 +00:00
|
|
|
Values string `json:"values"`
|
2021-12-08 11:36:07 +00:00
|
|
|
Version string `json:"version"`
|
2021-10-26 14:28:37 +00:00
|
|
|
}
|
|
|
|
|
2021-12-28 08:38:56 +00:00
|
|
|
var Version = SettingItem{
|
|
|
|
Key: "version",
|
|
|
|
Value: conf.GitTag,
|
|
|
|
Description: "version",
|
|
|
|
Type: "string",
|
|
|
|
Access: CONST,
|
|
|
|
Version: conf.GitTag,
|
|
|
|
Group: OTHER,
|
|
|
|
}
|
|
|
|
|
2021-10-26 14:28:37 +00:00
|
|
|
func SaveSettings(items []SettingItem) error {
|
|
|
|
return conf.DB.Save(items).Error
|
|
|
|
}
|
|
|
|
|
2021-11-04 05:23:17 +00:00
|
|
|
func SaveSetting(item SettingItem) error {
|
|
|
|
return conf.DB.Save(item).Error
|
|
|
|
}
|
|
|
|
|
2021-12-28 08:38:56 +00:00
|
|
|
func GetSettingsPublic() ([]SettingItem, error) {
|
2021-10-26 14:28:37 +00:00
|
|
|
var items []SettingItem
|
2021-12-28 08:38:56 +00:00
|
|
|
if err := conf.DB.Where("`access` <> ?", 1).Find(&items).Error; err != nil {
|
2021-10-29 16:35:29 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-28 08:38:56 +00:00
|
|
|
return items, nil
|
2021-10-29 16:35:29 +00:00
|
|
|
}
|
|
|
|
|
2021-12-28 08:38:56 +00:00
|
|
|
func GetSettingsByGroup(group int) ([]SettingItem, error) {
|
|
|
|
var items []SettingItem
|
|
|
|
if err := conf.DB.Where("`group` = ?", group).Find(&items).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
items = append([]SettingItem{Version}, items...)
|
|
|
|
return items, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetSettings() ([]SettingItem, error) {
|
2021-10-29 16:35:29 +00:00
|
|
|
var items []SettingItem
|
|
|
|
if err := conf.DB.Find(&items).Error; err != nil {
|
2021-10-26 14:28:37 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-28 08:38:56 +00:00
|
|
|
return items, nil
|
2021-10-26 14:28:37 +00:00
|
|
|
}
|
2021-10-27 10:59:03 +00:00
|
|
|
|
2021-12-08 11:36:07 +00:00
|
|
|
func DeleteSetting(key string) error {
|
|
|
|
setting := SettingItem{
|
|
|
|
Key: key,
|
|
|
|
}
|
|
|
|
return conf.DB.Delete(&setting).Error
|
|
|
|
}
|
|
|
|
|
2021-10-27 10:59:03 +00:00
|
|
|
func GetSettingByKey(key string) (*SettingItem, error) {
|
|
|
|
var items SettingItem
|
2021-11-07 06:04:54 +00:00
|
|
|
if err := conf.DB.Where("`key` = ?", key).First(&items).Error; err != nil {
|
2021-10-27 10:59:03 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &items, nil
|
|
|
|
}
|
2021-11-12 14:56:30 +00:00
|
|
|
|
|
|
|
func LoadSettings() {
|
|
|
|
textTypes, err := GetSettingByKey("text types")
|
|
|
|
if err == nil {
|
|
|
|
conf.TextTypes = strings.Split(textTypes.Value, ",")
|
|
|
|
}
|
2022-01-04 13:21:27 +00:00
|
|
|
// html
|
2021-11-14 06:37:26 +00:00
|
|
|
favicon, err := GetSettingByKey("favicon")
|
|
|
|
if err == nil {
|
|
|
|
//conf.Favicon = favicon.Value
|
2022-01-08 13:10:34 +00:00
|
|
|
conf.ManageHtml = strings.Replace(conf.RawIndexHtml, "https://store.heytapimage.com/cdo-portal/feedback/202110/30/d43c41c5d257c9bc36366e310374fb19.png", favicon.Value, 1)
|
2021-11-14 06:37:26 +00:00
|
|
|
}
|
2021-11-16 14:32:05 +00:00
|
|
|
title, err := GetSettingByKey("title")
|
|
|
|
if err == nil {
|
2022-01-08 13:10:34 +00:00
|
|
|
conf.ManageHtml = strings.Replace(conf.ManageHtml, "Loading...", title.Value, 1)
|
2021-11-16 14:32:05 +00:00
|
|
|
}
|
2021-12-05 05:43:08 +00:00
|
|
|
customizeHead, err := GetSettingByKey("customize head")
|
2021-11-14 06:37:26 +00:00
|
|
|
if err == nil {
|
2022-01-08 13:10:34 +00:00
|
|
|
conf.IndexHtml = strings.Replace(conf.ManageHtml, "<!-- customize head -->", customizeHead.Value, 1)
|
2021-11-14 06:37:26 +00:00
|
|
|
}
|
2021-12-05 05:43:08 +00:00
|
|
|
customizeBody, err := GetSettingByKey("customize body")
|
2021-11-14 06:37:26 +00:00
|
|
|
if err == nil {
|
2021-12-05 05:43:08 +00:00
|
|
|
conf.IndexHtml = strings.Replace(conf.IndexHtml, "<!-- customize body -->", customizeBody.Value, 1)
|
2021-11-14 06:37:26 +00:00
|
|
|
}
|
2022-01-04 13:21:27 +00:00
|
|
|
// token
|
2021-12-07 12:16:34 +00:00
|
|
|
adminPassword, err := GetSettingByKey("password")
|
|
|
|
if err == nil {
|
2021-12-08 11:36:07 +00:00
|
|
|
conf.Token = utils.GetMD5Encode(fmt.Sprintf("https://github.com/Xhofe/alist-%s", adminPassword.Value))
|
2021-12-07 12:16:34 +00:00
|
|
|
}
|
2022-01-04 13:21:27 +00:00
|
|
|
// load settings
|
|
|
|
for _, key := range conf.LoadSettings {
|
|
|
|
vm, err := GetSettingByKey(key)
|
|
|
|
if err == nil {
|
|
|
|
conf.Set(key, vm.Value)
|
|
|
|
}
|
2021-12-28 09:05:16 +00:00
|
|
|
}
|
2021-11-14 06:37:26 +00:00
|
|
|
}
|