v7.1.1
This commit is contained in:
+129
-8
@@ -2,11 +2,138 @@ package backend
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const legacyTidalAPICacheFile = "tidal-api-urls.json"
|
||||
|
||||
func normalizeCustomTidalAPIValue(value interface{}) string {
|
||||
customAPI, _ := value.(string)
|
||||
customAPI = strings.TrimRight(strings.TrimSpace(customAPI), "/")
|
||||
if strings.HasPrefix(customAPI, "https://") {
|
||||
return customAPI
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func sanitizeDownloaderValue(value interface{}, allowTidal bool) string {
|
||||
downloader, _ := value.(string)
|
||||
switch strings.TrimSpace(strings.ToLower(downloader)) {
|
||||
case "tidal":
|
||||
if allowTidal {
|
||||
return "tidal"
|
||||
}
|
||||
return "auto"
|
||||
case "qobuz":
|
||||
return "qobuz"
|
||||
case "amazon":
|
||||
return "amazon"
|
||||
default:
|
||||
return "auto"
|
||||
}
|
||||
}
|
||||
|
||||
func sanitizeAutoOrderValue(value interface{}, allowTidal bool) string {
|
||||
autoOrder, _ := value.(string)
|
||||
allowed := map[string]struct{}{
|
||||
"qobuz": {},
|
||||
"amazon": {},
|
||||
}
|
||||
fallback := "qobuz-amazon"
|
||||
if allowTidal {
|
||||
allowed["tidal"] = struct{}{}
|
||||
fallback = "tidal-qobuz-amazon"
|
||||
}
|
||||
|
||||
seen := make(map[string]struct{})
|
||||
parts := make([]string, 0, 3)
|
||||
for _, rawPart := range strings.Split(strings.TrimSpace(strings.ToLower(autoOrder)), "-") {
|
||||
part := strings.TrimSpace(rawPart)
|
||||
if part == "" {
|
||||
continue
|
||||
}
|
||||
if _, ok := allowed[part]; !ok {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[part]; ok {
|
||||
continue
|
||||
}
|
||||
seen[part] = struct{}{}
|
||||
parts = append(parts, part)
|
||||
}
|
||||
|
||||
if len(parts) < 2 {
|
||||
return fallback
|
||||
}
|
||||
|
||||
return strings.Join(parts, "-")
|
||||
}
|
||||
|
||||
func SanitizeSettingsMap(settings map[string]interface{}) map[string]interface{} {
|
||||
if settings == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
sanitized := make(map[string]interface{}, len(settings))
|
||||
for key, value := range settings {
|
||||
sanitized[key] = value
|
||||
}
|
||||
|
||||
customAPI := normalizeCustomTidalAPIValue(sanitized["customTidalApi"])
|
||||
sanitized["customTidalApi"] = customAPI
|
||||
allowTidal := customAPI != ""
|
||||
sanitized["downloader"] = sanitizeDownloaderValue(sanitized["downloader"], allowTidal)
|
||||
sanitized["autoOrder"] = sanitizeAutoOrderValue(sanitized["autoOrder"], allowTidal)
|
||||
|
||||
return sanitized
|
||||
}
|
||||
|
||||
func CleanupLegacyTidalPublicAPIState() error {
|
||||
appDir, err := EnsureAppDir()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cachePath := filepath.Join(appDir, legacyTidalAPICacheFile)
|
||||
if err := os.Remove(cachePath); err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func SanitizePersistedConfigSettings() error {
|
||||
configPath, err := GetConfigPath()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var settings map[string]interface{}
|
||||
if err := json.Unmarshal(data, &settings); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sanitized := SanitizeSettingsMap(settings)
|
||||
payload, err := json.MarshalIndent(sanitized, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return os.WriteFile(configPath, payload, 0o644)
|
||||
}
|
||||
|
||||
func GetDefaultMusicPath() string {
|
||||
|
||||
homeDir, err := os.UserHomeDir()
|
||||
@@ -47,7 +174,7 @@ func LoadConfigSettings() (map[string]interface{}, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return settings, nil
|
||||
return SanitizeSettingsMap(settings), nil
|
||||
}
|
||||
|
||||
func GetRedownloadWithSuffixSetting() bool {
|
||||
@@ -66,13 +193,7 @@ func GetCustomTidalAPISetting() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
customAPI, _ := settings["customTidalApi"].(string)
|
||||
customAPI = strings.TrimRight(strings.TrimSpace(customAPI), "/")
|
||||
if strings.HasPrefix(customAPI, "https://") {
|
||||
return customAPI
|
||||
}
|
||||
|
||||
return ""
|
||||
return normalizeCustomTidalAPIValue(settings["customTidalApi"])
|
||||
}
|
||||
|
||||
func normalizeExistingFileCheckMode(value string) string {
|
||||
|
||||
Reference in New Issue
Block a user