2019-11-09 06:04:48 -05:00
|
|
|
|
package model
|
|
|
|
|
|
|
|
|
|
import (
|
2019-12-10 04:10:34 -05:00
|
|
|
|
"net/url"
|
2020-01-01 23:44:53 -05:00
|
|
|
|
"strconv"
|
2020-11-21 04:34:55 -05:00
|
|
|
|
|
|
|
|
|
"github.com/cloudreve/Cloudreve/v3/pkg/cache"
|
|
|
|
|
"github.com/jinzhu/gorm"
|
2019-11-09 06:04:48 -05:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Setting 系统设置模型
|
|
|
|
|
type Setting struct {
|
|
|
|
|
gorm.Model
|
|
|
|
|
Type string `gorm:"not null"`
|
|
|
|
|
Name string `gorm:"unique;not null;index:setting_key"`
|
|
|
|
|
Value string `gorm:"size:65535"`
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-11 06:13:17 -05:00
|
|
|
|
// IsTrueVal 返回设置的值是否为真
|
|
|
|
|
func IsTrueVal(val string) bool {
|
|
|
|
|
return val == "1" || val == "true"
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-09 06:04:48 -05:00
|
|
|
|
// GetSettingByName 用 Name 获取设置值
|
2019-11-10 04:07:35 -05:00
|
|
|
|
func GetSettingByName(name string) string {
|
2019-11-09 06:04:48 -05:00
|
|
|
|
var setting Setting
|
|
|
|
|
|
2019-11-09 07:32:15 -05:00
|
|
|
|
// 优先从缓存中查找
|
2019-12-05 04:22:43 -05:00
|
|
|
|
cacheKey := "setting_" + name
|
2019-12-09 02:20:02 -05:00
|
|
|
|
if optionValue, ok := cache.Get(cacheKey); ok {
|
2019-12-05 04:22:43 -05:00
|
|
|
|
return optionValue.(string)
|
2019-11-10 04:07:35 -05:00
|
|
|
|
}
|
2021-10-30 20:41:56 -05:00
|
|
|
|
|
2019-11-10 04:07:35 -05:00
|
|
|
|
// 尝试数据库中查找
|
2021-10-30 20:41:56 -05:00
|
|
|
|
if DB != nil {
|
|
|
|
|
result := DB.Where("name = ?", name).First(&setting)
|
|
|
|
|
if result.Error == nil {
|
|
|
|
|
_ = cache.Set(cacheKey, setting.Value, -1)
|
|
|
|
|
return setting.Value
|
|
|
|
|
}
|
2019-11-10 04:07:35 -05:00
|
|
|
|
}
|
2021-10-30 20:41:56 -05:00
|
|
|
|
|
2019-11-10 04:07:35 -05:00
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-23 06:02:39 -05:00
|
|
|
|
// GetSettingByNameWithDefault 用 Name 获取设置值, 取不到时使用缺省值
|
|
|
|
|
func GetSettingByNameWithDefault(name, fallback string) string {
|
|
|
|
|
res := GetSettingByName(name)
|
|
|
|
|
if res == "" {
|
|
|
|
|
return fallback
|
|
|
|
|
}
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-10 04:07:35 -05:00
|
|
|
|
// GetSettingByNames 用多个 Name 获取设置值
|
2020-02-04 02:29:52 -05:00
|
|
|
|
func GetSettingByNames(names ...string) map[string]string {
|
2019-11-10 04:07:35 -05:00
|
|
|
|
var queryRes []Setting
|
2019-12-09 06:30:52 -05:00
|
|
|
|
res, miss := cache.GetSettings(names, "setting_")
|
2019-11-10 04:07:35 -05:00
|
|
|
|
|
2020-02-06 00:53:47 -05:00
|
|
|
|
if len(miss) > 0 {
|
|
|
|
|
DB.Where("name IN (?)", miss).Find(&queryRes)
|
|
|
|
|
for _, setting := range queryRes {
|
|
|
|
|
res[setting.Name] = setting.Value
|
|
|
|
|
}
|
2019-11-10 04:07:35 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-09 06:30:52 -05:00
|
|
|
|
_ = cache.SetSettings(res, "setting_")
|
2019-11-10 04:07:35 -05:00
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetSettingByType 获取一个或多个分组的所有设置值
|
|
|
|
|
func GetSettingByType(types []string) map[string]string {
|
|
|
|
|
var queryRes []Setting
|
|
|
|
|
res := make(map[string]string)
|
|
|
|
|
|
|
|
|
|
DB.Where("type IN (?)", types).Find(&queryRes)
|
|
|
|
|
for _, setting := range queryRes {
|
|
|
|
|
res[setting.Name] = setting.Value
|
2019-11-09 06:04:48 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-10 04:07:35 -05:00
|
|
|
|
return res
|
2019-11-09 06:04:48 -05:00
|
|
|
|
}
|
2019-12-10 04:10:34 -05:00
|
|
|
|
|
|
|
|
|
// GetSiteURL 获取站点地址
|
|
|
|
|
func GetSiteURL() *url.URL {
|
|
|
|
|
base, err := url.Parse(GetSettingByName("siteURL"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
base, _ = url.Parse("https://cloudreve.org")
|
|
|
|
|
}
|
|
|
|
|
return base
|
|
|
|
|
}
|
2020-01-01 23:44:53 -05:00
|
|
|
|
|
|
|
|
|
// GetIntSetting 获取整形设置值,如果转换失败则返回默认值defaultVal
|
|
|
|
|
func GetIntSetting(key string, defaultVal int) int {
|
|
|
|
|
res, err := strconv.Atoi(GetSettingByName(key))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return defaultVal
|
|
|
|
|
}
|
|
|
|
|
return res
|
|
|
|
|
}
|