mirror of
https://github.com/zarzet/SpotiFLAC-Mobile.git
synced 2026-08-02 09:08:35 +02:00
fix(download): honor explicitly selected provider
This commit is contained in:
@@ -524,7 +524,12 @@ func DownloadWithExtensionFallback(req DownloadRequest) (*DownloadResponse, erro
|
||||
}
|
||||
}
|
||||
|
||||
priority = prioritizeFallbackProvidersByHealth(priority, extManager, req.Source)
|
||||
healthProtectedProvider := selectedProvider
|
||||
if strings.TrimSpace(healthProtectedProvider) == "" {
|
||||
healthProtectedProvider = req.Source
|
||||
}
|
||||
priority = prioritizeFallbackProvidersByHealth(priority, extManager, healthProtectedProvider)
|
||||
priority = moveProviderToFront(priority, selectedProvider)
|
||||
|
||||
for _, providerID := range priority {
|
||||
if isDownloadCancelled(req.ItemID) {
|
||||
|
||||
@@ -280,7 +280,7 @@ func fallbackRuntimeHealthStatus(ext *loadedExtension) string {
|
||||
}
|
||||
}
|
||||
|
||||
func prioritizeFallbackProvidersByHealth(priority []string, extManager *extensionManager, sourceProvider string) []string {
|
||||
func prioritizeFallbackProvidersByHealth(priority []string, extManager *extensionManager, protectedProvider string) []string {
|
||||
if len(priority) == 0 || extManager == nil {
|
||||
return priority
|
||||
}
|
||||
@@ -294,7 +294,7 @@ func prioritizeFallbackProvidersByHealth(priority []string, extManager *extensio
|
||||
if providerID == "" {
|
||||
continue
|
||||
}
|
||||
if strings.EqualFold(providerID, sourceProvider) || !isExtensionFallbackAllowed(providerID) {
|
||||
if strings.EqualFold(providerID, protectedProvider) || !isExtensionFallbackAllowed(providerID) {
|
||||
unknown = append(unknown, providerID)
|
||||
continue
|
||||
}
|
||||
@@ -324,6 +324,33 @@ func prioritizeFallbackProvidersByHealth(priority []string, extManager *extensio
|
||||
return result
|
||||
}
|
||||
|
||||
// moveProviderToFront preserves the user's explicit provider selection after
|
||||
// health-based fallback sorting. Health may order the remaining fallback
|
||||
// candidates, but it must never silently replace the provider the user picked.
|
||||
func moveProviderToFront(priority []string, providerID string) []string {
|
||||
providerID = strings.TrimSpace(providerID)
|
||||
if providerID == "" || len(priority) < 2 {
|
||||
return priority
|
||||
}
|
||||
|
||||
selectedIndex := -1
|
||||
for i, candidate := range priority {
|
||||
if strings.EqualFold(strings.TrimSpace(candidate), providerID) {
|
||||
selectedIndex = i
|
||||
break
|
||||
}
|
||||
}
|
||||
if selectedIndex <= 0 {
|
||||
return priority
|
||||
}
|
||||
|
||||
reordered := make([]string, 0, len(priority))
|
||||
reordered = append(reordered, priority[selectedIndex])
|
||||
reordered = append(reordered, priority[:selectedIndex]...)
|
||||
reordered = append(reordered, priority[selectedIndex+1:]...)
|
||||
return reordered
|
||||
}
|
||||
|
||||
func resolveExtensionAvailabilityReason(availability *ExtAvailabilityResult, err error) string {
|
||||
if availability != nil {
|
||||
if reason := strings.TrimSpace(availability.Reason); reason != "" {
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
@@ -457,6 +458,19 @@ func TestShouldStopProviderFallback(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMoveProviderToFrontPreservesExplicitSelection(t *testing.T) {
|
||||
priority := []string{"qobuz-web", "amazon-web", "tidal-web"}
|
||||
got := moveProviderToFront(priority, "AMAZON-WEB")
|
||||
want := []string{"amazon-web", "qobuz-web", "tidal-web"}
|
||||
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("moveProviderToFront() = %#v, want %#v", got, want)
|
||||
}
|
||||
if !reflect.DeepEqual(priority, []string{"qobuz-web", "amazon-web", "tidal-web"}) {
|
||||
t.Fatalf("moveProviderToFront mutated input: %#v", priority)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildExtensionFallbackStoppedResponsePrefersAvailabilityReason(t *testing.T) {
|
||||
resp := buildExtensionFallbackStoppedResponse("soundcloud", &ExtAvailabilityResult{
|
||||
Reason: "direct SoundCloud track ID",
|
||||
|
||||
Reference in New Issue
Block a user