fix(download): suffix only colliding quality variants

This commit is contained in:
zarzet
2026-07-29 02:43:35 +07:00
parent 1f55523b64
commit e7fbaf7b75
41 changed files with 629 additions and 110 deletions
+17
View File
@@ -26,6 +26,23 @@ func attemptExtensionDownload(
lastRetryAfterSeconds *int,
) (resp *DownloadResponse, cancelledOuter bool) {
outputPath := buildOutputPathForExtension(req, ext)
if shouldReuseExistingOutput(req, outputPath) {
result := DownloadResult{FilePath: outputPath}
enrichResultQualityFromFile(&result)
built := buildDownloadSuccessResponse(
req,
result,
providerLabel,
"File already exists",
outputPath,
true,
)
if req.ItemID != "" {
CompleteItemProgress(req.ItemID)
}
GoLog("[DownloadWithExtensionFallback] Keeping existing output instead of replacing it: %s\n", outputPath)
return &built, false
}
if req.ItemID != "" {
SetItemPreparingStage(req.ItemID, "resolving_stream")
}
+12
View File
@@ -83,6 +83,18 @@ func buildOutputPathForExtension(req DownloadRequest, ext *loadedExtension) stri
return filepath.Join(tempDir, buildDownloadFilename(req))
}
func shouldReuseExistingOutput(req DownloadRequest, outputPath string) bool {
if req.AllowQualityVariant || isFDOutput(req.OutputFD) {
return false
}
path := strings.TrimSpace(outputPath)
if path == "" || strings.HasPrefix(path, "content://") || strings.HasPrefix(path, "/proc/self/fd/") {
return false
}
info, err := os.Stat(path)
return err == nil && info.Mode().IsRegular() && info.Size() > 0
}
func canEmbedGenreLabel(filePath string) bool {
path := strings.TrimSpace(filePath)
if path == "" || strings.HasPrefix(path, "content://") || strings.HasPrefix(path, "/proc/self/fd/") {
@@ -0,0 +1,35 @@
package gobackend
import (
"os"
"path/filepath"
"testing"
)
func TestShouldReuseExistingOutputProtectsCompletedFile(t *testing.T) {
path := filepath.Join(t.TempDir(), "Artist - Track.flac")
if err := os.WriteFile(path, []byte("existing audio"), 0o644); err != nil {
t.Fatalf("write existing output: %v", err)
}
if !shouldReuseExistingOutput(DownloadRequest{}, path) {
t.Fatal("expected completed output to be reused when variants are disabled")
}
if shouldReuseExistingOutput(
DownloadRequest{AllowQualityVariant: true},
path,
) {
t.Fatal("quality-variant staging output must remain independently writable")
}
}
func TestShouldReuseExistingOutputIgnoresEmptyStagingFile(t *testing.T) {
path := filepath.Join(t.TempDir(), "native_saf_work.flac")
if err := os.WriteFile(path, nil, 0o644); err != nil {
t.Fatalf("write staging output: %v", err)
}
if shouldReuseExistingOutput(DownloadRequest{}, path) {
t.Fatal("empty staging output must not be treated as a completed download")
}
}