Clean up grouping logic

This commit is contained in:
Ben Sarmiento
2023-10-18 21:47:57 +02:00
parent 0fa7b407b0
commit ce0b2445b2
4 changed files with 14 additions and 21 deletions

View File

@@ -1,7 +1,6 @@
package config
import (
"fmt"
"regexp"
"strings"
@@ -29,10 +28,8 @@ func (z *ZurgConfigV1) GetDirectories() []string {
}
func (z *ZurgConfigV1) GetGroupMap() map[string][]string {
fmt.Println("Getting group map")
var groupMap = make(map[string][]string)
for directory, val := range z.Directories {
fmt.Println(directory, val.Group)
groupMap[val.Group] = append(groupMap[val.Group], directory)
}
return groupMap

View File

@@ -1,7 +1,6 @@
package dav
import (
"fmt"
"log"
"net/http"
"os"
@@ -17,8 +16,6 @@ func Router(mux *http.ServeMux) {
log.Panicf("Config failed to load: %v", err)
}
fmt.Println("config version", c.GetDirectories())
t := torrent.NewTorrentManager(os.Getenv("RD_TOKEN"), c)
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {

View File

@@ -23,9 +23,9 @@ func findAllTorrentsWithName(t *torrent.TorrentManager, directory, torrentName s
var matchingTorrents []torrent.Torrent
torrents := t.GetByDirectory(directory)
for _, torrent := range torrents {
if torrent.Name == torrentName || strings.HasPrefix(torrent.Name, torrentName) {
matchingTorrents = append(matchingTorrents, torrent)
for i := range torrents {
if torrents[i].Name == torrentName || strings.HasPrefix(torrents[i].Name, torrentName) {
matchingTorrents = append(matchingTorrents, torrents[i])
}
}

View File

@@ -2,7 +2,6 @@ package torrent
import (
"encoding/gob"
"fmt"
"log"
"os"
"strings"
@@ -58,9 +57,9 @@ func (t *TorrentManager) getAll() []Torrent {
}
torrentsV2 = append(torrentsV2, torrentV2)
}
log.Printf("Fetched %d torrents", len(torrentsV2))
version := t.config.GetVersion()
fmt.Println("config version", version)
if version == "v1" {
configV1 := t.config.(*config.ZurgConfigV1)
groupMap := configV1.GetGroupMap()
@@ -70,23 +69,23 @@ func (t *TorrentManager) getAll() []Torrent {
for _, directory := range directories {
if configV1.MeetsConditions(directory, torrentsV2[i].ID, torrentsV2[i].Name) {
torrentsV2[i].Directories = append(torrentsV2[i].Directories, directory)
fmt.Println(torrentsV2[i].Name, torrentsV2[i].Directories)
break
}
}
}
}
}
log.Printf("Fetched %d torrents", len(torrentsV2))
log.Println("Finished mapping to groups")
return torrentsV2
}
func (t *TorrentManager) GetByDirectory(directory string) []Torrent {
var torrents []Torrent
for _, torrent := range t.torrents {
for _, dir := range torrent.Directories {
for i := range t.torrents {
for _, dir := range t.torrents[i].Directories {
if dir == directory {
torrents = append(torrents, torrent)
torrents = append(torrents, t.torrents[i])
}
}
}
@@ -174,17 +173,17 @@ func (t *TorrentManager) getInfo(torrentID string) *Torrent {
}
func (t *TorrentManager) GetInfo(torrentID string) *Torrent {
for _, torrent := range t.torrents {
if torrent.ID == torrentID {
return &torrent
for i := range t.torrents {
if t.torrents[i].ID == torrentID {
return &t.torrents[i]
}
}
return t.getInfo(torrentID)
}
func (t *TorrentManager) getByID(torrentID string) *Torrent {
for i, torrent := range t.torrents {
if torrent.ID == torrentID {
for i := range t.torrents {
if t.torrents[i].ID == torrentID {
return &t.torrents[i]
}
}