2023-04-22 12:39:27 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
2023-04-22 13:14:09 +00:00
|
|
|
"one-api/common"
|
2023-04-22 12:39:27 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Option struct {
|
|
|
|
Key string `json:"key" gorm:"primaryKey"`
|
|
|
|
Value string `json:"value"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func AllOption() ([]*Option, error) {
|
|
|
|
var options []*Option
|
|
|
|
var err error
|
|
|
|
err = DB.Find(&options).Error
|
|
|
|
return options, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func InitOptionMap() {
|
|
|
|
common.OptionMapRWMutex.Lock()
|
|
|
|
common.OptionMap = make(map[string]string)
|
|
|
|
common.OptionMap["FileUploadPermission"] = strconv.Itoa(common.FileUploadPermission)
|
|
|
|
common.OptionMap["FileDownloadPermission"] = strconv.Itoa(common.FileDownloadPermission)
|
|
|
|
common.OptionMap["ImageUploadPermission"] = strconv.Itoa(common.ImageUploadPermission)
|
|
|
|
common.OptionMap["ImageDownloadPermission"] = strconv.Itoa(common.ImageDownloadPermission)
|
|
|
|
common.OptionMap["PasswordLoginEnabled"] = strconv.FormatBool(common.PasswordLoginEnabled)
|
|
|
|
common.OptionMap["PasswordRegisterEnabled"] = strconv.FormatBool(common.PasswordRegisterEnabled)
|
|
|
|
common.OptionMap["EmailVerificationEnabled"] = strconv.FormatBool(common.EmailVerificationEnabled)
|
|
|
|
common.OptionMap["GitHubOAuthEnabled"] = strconv.FormatBool(common.GitHubOAuthEnabled)
|
|
|
|
common.OptionMap["WeChatAuthEnabled"] = strconv.FormatBool(common.WeChatAuthEnabled)
|
|
|
|
common.OptionMap["TurnstileCheckEnabled"] = strconv.FormatBool(common.TurnstileCheckEnabled)
|
|
|
|
common.OptionMap["RegisterEnabled"] = strconv.FormatBool(common.RegisterEnabled)
|
|
|
|
common.OptionMap["SMTPServer"] = ""
|
2023-05-12 03:44:38 +00:00
|
|
|
common.OptionMap["SMTPPort"] = strconv.Itoa(common.SMTPPort)
|
2023-04-22 12:39:27 +00:00
|
|
|
common.OptionMap["SMTPAccount"] = ""
|
|
|
|
common.OptionMap["SMTPToken"] = ""
|
|
|
|
common.OptionMap["Notice"] = ""
|
|
|
|
common.OptionMap["About"] = ""
|
|
|
|
common.OptionMap["Footer"] = common.Footer
|
|
|
|
common.OptionMap["ServerAddress"] = ""
|
|
|
|
common.OptionMap["GitHubClientId"] = ""
|
|
|
|
common.OptionMap["GitHubClientSecret"] = ""
|
|
|
|
common.OptionMap["WeChatServerAddress"] = ""
|
|
|
|
common.OptionMap["WeChatServerToken"] = ""
|
|
|
|
common.OptionMap["WeChatAccountQRCodeImageURL"] = ""
|
|
|
|
common.OptionMap["TurnstileSiteKey"] = ""
|
|
|
|
common.OptionMap["TurnstileSecretKey"] = ""
|
2023-04-26 13:40:56 +00:00
|
|
|
common.OptionMap["QuotaForNewUser"] = strconv.Itoa(common.QuotaForNewUser)
|
2023-05-11 12:59:35 +00:00
|
|
|
common.OptionMap["ModelRatio"] = common.ModelRatio2JSONString()
|
2023-04-27 08:32:21 +00:00
|
|
|
common.OptionMap["TopUpLink"] = common.TopUpLink
|
2023-04-22 12:39:27 +00:00
|
|
|
common.OptionMapRWMutex.Unlock()
|
|
|
|
options, _ := AllOption()
|
|
|
|
for _, option := range options {
|
|
|
|
updateOptionMap(option.Key, option.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func UpdateOption(key string, value string) error {
|
|
|
|
// Save to database first
|
|
|
|
option := Option{
|
|
|
|
Key: key,
|
|
|
|
}
|
|
|
|
// https://gorm.io/docs/update.html#Save-All-Fields
|
|
|
|
DB.FirstOrCreate(&option, Option{Key: key})
|
|
|
|
option.Value = value
|
|
|
|
// Save is a combination function.
|
|
|
|
// If save value does not contain primary key, it will execute Create,
|
|
|
|
// otherwise it will execute Update (with all fields).
|
|
|
|
DB.Save(&option)
|
|
|
|
// Update OptionMap
|
|
|
|
updateOptionMap(key, value)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-11 12:59:35 +00:00
|
|
|
func updateOptionMap(key string, value string) (err error) {
|
2023-04-22 12:39:27 +00:00
|
|
|
common.OptionMapRWMutex.Lock()
|
|
|
|
defer common.OptionMapRWMutex.Unlock()
|
|
|
|
common.OptionMap[key] = value
|
|
|
|
if strings.HasSuffix(key, "Permission") {
|
|
|
|
intValue, _ := strconv.Atoi(value)
|
|
|
|
switch key {
|
|
|
|
case "FileUploadPermission":
|
|
|
|
common.FileUploadPermission = intValue
|
|
|
|
case "FileDownloadPermission":
|
|
|
|
common.FileDownloadPermission = intValue
|
|
|
|
case "ImageUploadPermission":
|
|
|
|
common.ImageUploadPermission = intValue
|
|
|
|
case "ImageDownloadPermission":
|
|
|
|
common.ImageDownloadPermission = intValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if strings.HasSuffix(key, "Enabled") {
|
|
|
|
boolValue := value == "true"
|
|
|
|
switch key {
|
|
|
|
case "PasswordRegisterEnabled":
|
|
|
|
common.PasswordRegisterEnabled = boolValue
|
|
|
|
case "PasswordLoginEnabled":
|
|
|
|
common.PasswordLoginEnabled = boolValue
|
|
|
|
case "EmailVerificationEnabled":
|
|
|
|
common.EmailVerificationEnabled = boolValue
|
|
|
|
case "GitHubOAuthEnabled":
|
|
|
|
common.GitHubOAuthEnabled = boolValue
|
|
|
|
case "WeChatAuthEnabled":
|
|
|
|
common.WeChatAuthEnabled = boolValue
|
|
|
|
case "TurnstileCheckEnabled":
|
|
|
|
common.TurnstileCheckEnabled = boolValue
|
|
|
|
case "RegisterEnabled":
|
|
|
|
common.RegisterEnabled = boolValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch key {
|
|
|
|
case "SMTPServer":
|
|
|
|
common.SMTPServer = value
|
2023-05-12 03:44:38 +00:00
|
|
|
case "SMTPPort":
|
|
|
|
intValue, _ := strconv.Atoi(value)
|
|
|
|
common.SMTPPort = intValue
|
2023-04-22 12:39:27 +00:00
|
|
|
case "SMTPAccount":
|
|
|
|
common.SMTPAccount = value
|
|
|
|
case "SMTPToken":
|
|
|
|
common.SMTPToken = value
|
|
|
|
case "ServerAddress":
|
|
|
|
common.ServerAddress = value
|
|
|
|
case "GitHubClientId":
|
|
|
|
common.GitHubClientId = value
|
|
|
|
case "GitHubClientSecret":
|
|
|
|
common.GitHubClientSecret = value
|
|
|
|
case "Footer":
|
|
|
|
common.Footer = value
|
|
|
|
case "WeChatServerAddress":
|
|
|
|
common.WeChatServerAddress = value
|
|
|
|
case "WeChatServerToken":
|
|
|
|
common.WeChatServerToken = value
|
|
|
|
case "WeChatAccountQRCodeImageURL":
|
|
|
|
common.WeChatAccountQRCodeImageURL = value
|
|
|
|
case "TurnstileSiteKey":
|
|
|
|
common.TurnstileSiteKey = value
|
|
|
|
case "TurnstileSecretKey":
|
|
|
|
common.TurnstileSecretKey = value
|
2023-04-26 13:40:56 +00:00
|
|
|
case "QuotaForNewUser":
|
|
|
|
common.QuotaForNewUser, _ = strconv.Atoi(value)
|
2023-05-11 12:59:35 +00:00
|
|
|
case "ModelRatio":
|
|
|
|
err = common.UpdateModelRatioByJSONString(value)
|
2023-04-27 08:32:21 +00:00
|
|
|
case "TopUpLink":
|
|
|
|
common.TopUpLink = value
|
2023-04-22 12:39:27 +00:00
|
|
|
}
|
2023-05-11 12:59:35 +00:00
|
|
|
return err
|
2023-04-22 12:39:27 +00:00
|
|
|
}
|