From 0fa7b407b088ccfad098a680342e369b3207f422 Mon Sep 17 00:00:00 2001 From: Ben Sarmiento Date: Wed, 18 Oct 2023 21:36:40 +0200 Subject: [PATCH] Fix grouping --- internal/config/v1.go | 15 ++++----------- internal/dav/router.go | 3 +++ internal/torrent/manager.go | 29 +++++++++++++++-------------- 3 files changed, 22 insertions(+), 25 deletions(-) diff --git a/internal/config/v1.go b/internal/config/v1.go index 9b8fc57..5812605 100644 --- a/internal/config/v1.go +++ b/internal/config/v1.go @@ -1,6 +1,7 @@ package config import ( + "fmt" "regexp" "strings" @@ -16,7 +17,7 @@ func loadV1Config(content []byte) (*ZurgConfigV1, error) { } func (z *ZurgConfigV1) GetVersion() string { - return z.Version + return "v1" } func (z *ZurgConfigV1) GetDirectories() []string { @@ -28,23 +29,15 @@ 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 } -func (z *ZurgConfigV1) GetDirectoriesByGroup(group string) []string { - var groupDirs []string - for directory := range z.Directories { - if z.Directories[directory].Group == group { - groupDirs = append(groupDirs, directory) - } - } - return groupDirs -} - func (z *ZurgConfigV1) MeetsConditions(directory, fileID, torrentName string) bool { if _, ok := z.Directories[directory]; !ok { return false diff --git a/internal/dav/router.go b/internal/dav/router.go index 79f8f22..d9044de 100644 --- a/internal/dav/router.go +++ b/internal/dav/router.go @@ -1,6 +1,7 @@ package dav import ( + "fmt" "log" "net/http" "os" @@ -16,6 +17,8 @@ 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) { diff --git a/internal/torrent/manager.go b/internal/torrent/manager.go index bb57cc0..365eb5c 100644 --- a/internal/torrent/manager.go +++ b/internal/torrent/manager.go @@ -2,6 +2,7 @@ package torrent import ( "encoding/gob" + "fmt" "log" "os" "strings" @@ -50,25 +51,29 @@ func (t *TorrentManager) getAll() []Torrent { return nil } var torrentsV2 []Torrent + for _, torrent := range torrents { + torrentV2 := Torrent{ + Torrent: torrent, + SelectedFiles: nil, + } + torrentsV2 = append(torrentsV2, torrentV2) + } version := t.config.GetVersion() + fmt.Println("config version", version) if version == "v1" { configV1 := t.config.(*config.ZurgConfigV1) groupMap := configV1.GetGroupMap() - for _, directories := range groupMap { - for _, torrent := range torrents { - // process grouping - torrentV2 := Torrent{ - Torrent: torrent, - SelectedFiles: nil, - } + for group, directories := range groupMap { + log.Printf("Processing group %s\n", group) + for i := range torrents { for _, directory := range directories { - if configV1.MeetsConditions(directory, torrent.ID, torrent.Name) { - torrentV2.Directories = append(torrentV2.Directories, directory) + 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 } } - torrentsV2 = append(torrentsV2, torrentV2) } } } @@ -76,10 +81,6 @@ func (t *TorrentManager) getAll() []Torrent { return torrentsV2 } -func (t *TorrentManager) GetAll() []Torrent { - return t.torrents -} - func (t *TorrentManager) GetByDirectory(directory string) []Torrent { var torrents []Torrent for _, torrent := range t.torrents {