fix(download): honor explicitly selected provider

This commit is contained in:
zarzet
2026-07-28 14:11:14 +07:00
parent 7bf6f32802
commit b4f239c692
3 changed files with 49 additions and 3 deletions
+6 -1
View File
@@ -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) {
+29 -2
View File
@@ -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 != "" {
+14
View File
@@ -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",