Merge remote-tracking branch 'origin/main'
This commit is contained in:
@@ -24,6 +24,7 @@ const (
|
||||
)
|
||||
|
||||
const SystemSettingFileStorage = "file_storage"
|
||||
const SystemSettingClientCustomization = "client_customization"
|
||||
|
||||
const fileStorageChannelColumns = `
|
||||
id::text, channel_key, name, provider, COALESCE(upload_url, ''), credentials,
|
||||
@@ -72,6 +73,20 @@ type FileStorageSettingsInput struct {
|
||||
ResultUploadPolicy string `json:"resultUploadPolicy"`
|
||||
}
|
||||
|
||||
type ClientCustomizationSettings struct {
|
||||
ClientName string `json:"clientName"`
|
||||
ClientEnglishName string `json:"clientEnglishName"`
|
||||
PlatformName string `json:"platformName"`
|
||||
IconPath string `json:"iconPath"`
|
||||
}
|
||||
|
||||
type ClientCustomizationSettingsInput struct {
|
||||
ClientName *string `json:"clientName"`
|
||||
ClientEnglishName *string `json:"clientEnglishName"`
|
||||
PlatformName *string `json:"platformName"`
|
||||
IconPath *string `json:"iconPath"`
|
||||
}
|
||||
|
||||
type fileStorageChannelScanner interface {
|
||||
Scan(dest ...any) error
|
||||
}
|
||||
@@ -348,6 +363,15 @@ func DefaultFileStorageSettings() FileStorageSettings {
|
||||
return FileStorageSettings{ResultUploadPolicy: FileStorageResultUploadPolicyDefault}
|
||||
}
|
||||
|
||||
func DefaultClientCustomizationSettings() ClientCustomizationSettings {
|
||||
return ClientCustomizationSettings{
|
||||
ClientName: "EasyAI AI Gateway",
|
||||
ClientEnglishName: "EasyAI AI Gateway",
|
||||
PlatformName: "EasyAI",
|
||||
IconPath: "",
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Store) GetFileStorageSettings(ctx context.Context) (FileStorageSettings, error) {
|
||||
var value []byte
|
||||
err := s.pool.QueryRow(ctx, `
|
||||
@@ -380,13 +404,53 @@ func (s *Store) UpdateFileStorageSettings(ctx context.Context, input FileStorage
|
||||
return fileStorageSettingsFromValue(decodeObject(saved)), nil
|
||||
}
|
||||
|
||||
func (s *Store) GetClientCustomizationSettings(ctx context.Context) (ClientCustomizationSettings, error) {
|
||||
var value []byte
|
||||
err := s.pool.QueryRow(ctx, `
|
||||
SELECT value
|
||||
FROM system_settings
|
||||
WHERE setting_key = $1`, SystemSettingClientCustomization).Scan(&value)
|
||||
if err != nil {
|
||||
if IsNotFound(err) {
|
||||
return DefaultClientCustomizationSettings(), nil
|
||||
}
|
||||
return ClientCustomizationSettings{}, err
|
||||
}
|
||||
return clientCustomizationSettingsFromValue(decodeObject(value)), nil
|
||||
}
|
||||
|
||||
func (s *Store) UpdateClientCustomizationSettings(ctx context.Context, input ClientCustomizationSettingsInput) (ClientCustomizationSettings, error) {
|
||||
current, err := s.GetClientCustomizationSettings(ctx)
|
||||
if err != nil && !IsUndefinedDatabaseObject(err) {
|
||||
return ClientCustomizationSettings{}, err
|
||||
}
|
||||
settings := normalizeClientCustomizationSettings(input, current)
|
||||
value, _ := json.Marshal(settings)
|
||||
var saved []byte
|
||||
err = s.upsertSystemSetting(ctx, SystemSettingClientCustomization, value, &saved)
|
||||
if err != nil && IsUndefinedDatabaseObject(err) {
|
||||
if ensureErr := s.ensureSystemSettingsTable(ctx); ensureErr != nil {
|
||||
return ClientCustomizationSettings{}, ensureErr
|
||||
}
|
||||
err = s.upsertSystemSetting(ctx, SystemSettingClientCustomization, value, &saved)
|
||||
}
|
||||
if err != nil {
|
||||
return ClientCustomizationSettings{}, err
|
||||
}
|
||||
return clientCustomizationSettingsFromValue(decodeObject(saved)), nil
|
||||
}
|
||||
|
||||
func (s *Store) upsertFileStorageSettings(ctx context.Context, value []byte, saved *[]byte) error {
|
||||
return s.upsertSystemSetting(ctx, SystemSettingFileStorage, value, saved)
|
||||
}
|
||||
|
||||
func (s *Store) upsertSystemSetting(ctx context.Context, settingKey string, value []byte, saved *[]byte) error {
|
||||
return s.pool.QueryRow(ctx, `
|
||||
INSERT INTO system_settings (setting_key, value)
|
||||
VALUES ($1, $2)
|
||||
ON CONFLICT (setting_key)
|
||||
DO UPDATE SET value = EXCLUDED.value, updated_at = now()
|
||||
RETURNING value`, SystemSettingFileStorage, value).Scan(saved)
|
||||
RETURNING value`, settingKey, value).Scan(saved)
|
||||
}
|
||||
|
||||
func (s *Store) ensureSystemSettingsTable(ctx context.Context) error {
|
||||
@@ -409,6 +473,53 @@ func fileStorageSettingsFromValue(value map[string]any) FileStorageSettings {
|
||||
return settings
|
||||
}
|
||||
|
||||
func clientCustomizationSettingsFromValue(value map[string]any) ClientCustomizationSettings {
|
||||
settings := DefaultClientCustomizationSettings()
|
||||
if value == nil {
|
||||
return settings
|
||||
}
|
||||
if clientName := stringFromAny(value["clientName"]); clientName != "" {
|
||||
settings.ClientName = clientName
|
||||
}
|
||||
if clientEnglishName := stringFromAny(value["clientEnglishName"]); clientEnglishName != "" {
|
||||
settings.ClientEnglishName = clientEnglishName
|
||||
}
|
||||
if platformName := stringFromAny(value["platformName"]); platformName != "" {
|
||||
settings.PlatformName = platformName
|
||||
}
|
||||
settings.IconPath = stringFromAny(value["iconPath"])
|
||||
return settings
|
||||
}
|
||||
|
||||
func normalizeClientCustomizationSettings(input ClientCustomizationSettingsInput, current ClientCustomizationSettings) ClientCustomizationSettings {
|
||||
settings := current
|
||||
if settings.ClientName == "" && settings.ClientEnglishName == "" && settings.PlatformName == "" {
|
||||
settings = DefaultClientCustomizationSettings()
|
||||
}
|
||||
if input.ClientName != nil {
|
||||
settings.ClientName = strings.TrimSpace(*input.ClientName)
|
||||
}
|
||||
if input.ClientEnglishName != nil {
|
||||
settings.ClientEnglishName = strings.TrimSpace(*input.ClientEnglishName)
|
||||
}
|
||||
if input.PlatformName != nil {
|
||||
settings.PlatformName = strings.TrimSpace(*input.PlatformName)
|
||||
}
|
||||
if input.IconPath != nil {
|
||||
settings.IconPath = strings.TrimSpace(*input.IconPath)
|
||||
}
|
||||
if settings.ClientName == "" {
|
||||
settings.ClientName = DefaultClientCustomizationSettings().ClientName
|
||||
}
|
||||
if settings.ClientEnglishName == "" {
|
||||
settings.ClientEnglishName = DefaultClientCustomizationSettings().ClientEnglishName
|
||||
}
|
||||
if settings.PlatformName == "" {
|
||||
settings.PlatformName = DefaultClientCustomizationSettings().PlatformName
|
||||
}
|
||||
return settings
|
||||
}
|
||||
|
||||
func NormalizeFileStorageResultUploadPolicy(policy string) string {
|
||||
normalized := strings.ToLower(strings.TrimSpace(policy))
|
||||
normalized = strings.ReplaceAll(normalized, "-", "_")
|
||||
|
||||
Reference in New Issue
Block a user