feat: add file safety config integration
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
//go:build mysql
|
||||
// +build mysql
|
||||
|
||||
package mysql
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"strings"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
t "github.com/tinode/chat/server/store/types"
|
||||
)
|
||||
|
||||
func (a *adapter) ThirdPartyConfig(group string, keys ...string) (map[string]string, error) {
|
||||
db := a.registryDB
|
||||
if db == nil {
|
||||
db = a.db
|
||||
}
|
||||
if db == nil {
|
||||
return nil, t.ErrInternal
|
||||
}
|
||||
group = strings.TrimSpace(group)
|
||||
if group == "" {
|
||||
return nil, t.ErrMalformed
|
||||
}
|
||||
|
||||
ctx, cancel := a.getContext()
|
||||
if cancel != nil {
|
||||
defer cancel()
|
||||
}
|
||||
|
||||
rows := []struct {
|
||||
Key string `db:"key"`
|
||||
Value string `db:"value"`
|
||||
}{}
|
||||
var err error
|
||||
if len(keys) == 0 {
|
||||
err = db.SelectContext(ctx, &rows, `SELECT `+"`key`"+`, value FROM hg_sys_config
|
||||
WHERE `+"`group`"+`=? AND status=1`, group)
|
||||
} else {
|
||||
normalized := make([]string, 0, len(keys))
|
||||
for _, key := range keys {
|
||||
if key = strings.TrimSpace(key); key != "" {
|
||||
normalized = append(normalized, key)
|
||||
}
|
||||
}
|
||||
if len(normalized) == 0 {
|
||||
return map[string]string{}, nil
|
||||
}
|
||||
query, args, inErr := sqlx.In(`SELECT `+"`key`"+`, value FROM hg_sys_config
|
||||
WHERE `+"`group`"+`=? AND status=1 AND `+"`key`"+` IN (?)`, group, normalized)
|
||||
if inErr != nil {
|
||||
return nil, inErr
|
||||
}
|
||||
query = db.Rebind(query)
|
||||
err = db.SelectContext(ctx, &rows, query, args...)
|
||||
}
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return map[string]string{}, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := make(map[string]string, len(rows))
|
||||
for _, row := range rows {
|
||||
result[row.Key] = row.Value
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
Reference in New Issue
Block a user