Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions pkg/server/qbit/category_filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package qbit

import (
"fmt"
"strings"

"github.com/sirrobot01/decypharr/pkg/storage"
)

// categoryHashFilter never interprets absent hashes as a request for all rows.
// qBittorrent clients may use either repeated fields or a pipe-delimited field.
func categoryHashFilter(values []string) (func(*storage.Entry) bool, error) {
hashes := make(map[string]bool)
for _, value := range values {
for _, hash := range strings.Split(value, "|") {
hash = strings.ToLower(strings.TrimSpace(hash))
if hash != "" {
hashes[hash] = true
}
}
}
if len(hashes) == 0 {
return nil, fmt.Errorf("explicit hashes required for category changes")
}
if hashes["all"] {
if len(hashes) != 1 {
return nil, fmt.Errorf("all cannot be mixed with individual hashes")
}
return func(*storage.Entry) bool { return true }, nil
}
return func(entry *storage.Entry) bool {
return hashes[strings.ToLower(entry.InfoHash)]
}, nil
}
86 changes: 86 additions & 0 deletions pkg/server/qbit/category_filter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package qbit

import (
"os"
"testing"

"github.com/sirrobot01/decypharr/internal/config"
"github.com/sirrobot01/decypharr/pkg/storage"
)

func TestMain(m *testing.M) {
dir, err := os.MkdirTemp("", "decypharr-category-test-")
if err != nil {
panic(err)
}
config.SetConfigPath(dir)
code := m.Run()
os.RemoveAll(dir)
os.Exit(code)
}

func TestCategoryHashFilter(t *testing.T) {
for _, tc := range []struct {
name string
values []string
want []bool
bad bool
}{
{"one", []string{"AAA"}, []bool{true, false, false}, false},
{"pipe", []string{"aaa|bbb"}, []bool{true, true, false}, false},
{"repeated", []string{"aaa", "bbb"}, []bool{true, true, false}, false},
{"unknown", []string{"ddd"}, []bool{false, false, false}, false},
{"explicit all", []string{"all"}, []bool{true, true, true}, false},
{"missing", nil, nil, true},
{"empty", []string{" | "}, nil, true},
{"mixed all", []string{"all|aaa"}, nil, true},
} {
t.Run(tc.name, func(t *testing.T) {
filter, err := categoryHashFilter(tc.values)
if (err != nil) != tc.bad {
t.Fatalf("unexpected error: %v", err)
}
if tc.bad {
return
}
for i, hash := range []string{"aaa", "BBB", "ccc"} {
if filter(&storage.Entry{InfoHash: hash}) != tc.want[i] {
t.Fatalf("incorrect selection of row %s", hash)
}
}
})
}
}

func TestCategoryUpdatePreservesUnselectedRows(t *testing.T) {
s, err := storage.NewStorage(t.TempDir())
if err != nil {
t.Fatal(err)
}
defer s.Close()
for _, hash := range []string{"aaa", "bbb", "ccc"} {
if err := s.UpdateQueue(&storage.Entry{InfoHash: hash, Category: "wrong", SavePath: "/downloads/original"}); err != nil {
t.Fatal(err)
}
}
filter, _ := categoryHashFilter([]string{"aaa|bbb"})
if err := s.UpdateWhereQueued(filter, func(e *storage.Entry) bool {
e.Category = "restored"
return true
}); err != nil {
t.Fatal(err)
}
for _, hash := range []string{"aaa", "bbb", "ccc"} {
e, err := s.GetQueued(hash)
if err != nil {
t.Fatal(err)
}
want := "restored"
if hash == "ccc" {
want = "wrong"
}
if e.Category != want || e.SavePath != "/downloads/original" {
t.Fatalf("unexpected mutation for %s", hash)
}
}
}
12 changes: 4 additions & 8 deletions pkg/server/qbit/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,10 @@ func (q *QBit) handleSetCategory(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
category := getCategory(ctx)
hashes := getHashes(ctx)
var filterFunc func(t *storage.Entry) bool

hashSet := make(map[string]bool)
if len(hashes) > 0 {
for _, h := range hashes {
hashSet[h] = true
}

filterFunc, err := categoryHashFilter(hashes)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

updateFunc := func(t *storage.Entry) bool {
Expand Down