From 045c31a9f9d9ab5a88b95aa7787b404e56d8edcb Mon Sep 17 00:00:00 2001 From: Ben Sarmiento Date: Thu, 7 Dec 2023 22:34:25 +0100 Subject: [PATCH] Biggest file filter --- dist/md5.txt | 1 - internal/config/v1.go | 7 +++++++ internal/config/v1types.go | 11 ++++++----- internal/http/listing.go | 10 ++++++++++ internal/torrent/types.go | 10 ++++++++++ 5 files changed, 33 insertions(+), 6 deletions(-) delete mode 100644 dist/md5.txt diff --git a/dist/md5.txt b/dist/md5.txt deleted file mode 100644 index ccb26fe..0000000 --- a/dist/md5.txt +++ /dev/null @@ -1 +0,0 @@ -d41d8cd98f00b204e9800998ecf8427e ./md5.txt diff --git a/internal/config/v1.go b/internal/config/v1.go index 89f7796..0861264 100644 --- a/internal/config/v1.go +++ b/internal/config/v1.go @@ -40,6 +40,13 @@ func (z *ZurgConfigV1) GetDirectories() []string { return rootDirectories } +func (z *ZurgConfigV1) GetDirectoryConfig(directory string) Directory { + if dirCfg, ok := z.Directories[directory]; ok { + return *dirCfg + } + return Directory{} +} + func (z *ZurgConfigV1) GetGroupMap() map[string][]string { var groupMap = make(map[string][]string) var groupOrderMap = make(map[string]int) // To store GroupOrder for each directory diff --git a/internal/config/v1types.go b/internal/config/v1types.go index d1f8690..22c8b7c 100644 --- a/internal/config/v1types.go +++ b/internal/config/v1types.go @@ -4,13 +4,14 @@ import "github.com/debridmediamanager/zurg/pkg/logutil" type ZurgConfigV1 struct { ZurgConfig `yaml:",inline"` - Directories map[string]*DirectoryFilterConditionsV1 `yaml:"directories"` + Directories map[string]*Directory `yaml:"directories"` log *logutil.Logger } -type DirectoryFilterConditionsV1 struct { - GroupOrder int `yaml:"group_order"` - Group string `yaml:"group"` - Filters []*FilterConditionsV1 `yaml:"filters"` +type Directory struct { + GroupOrder int `yaml:"group_order"` + Group string `yaml:"group"` + Filters []*FilterConditionsV1 `yaml:"filters"` + OnlyShowTheBiggestFile bool `yaml:"only_show_the_biggest_file"` } type FilterConditionsV1 struct { diff --git a/internal/http/listing.go b/internal/http/listing.go index 50adfb5..ed671c8 100644 --- a/internal/http/listing.go +++ b/internal/http/listing.go @@ -7,6 +7,7 @@ import ( "sort" "strings" + "github.com/debridmediamanager/zurg/internal/config" "github.com/debridmediamanager/zurg/internal/torrent" "github.com/debridmediamanager/zurg/pkg/logutil" ) @@ -59,6 +60,12 @@ func HandleListFiles(directory, torrentName string, torMgr *torrent.TorrentManag return nil, fmt.Errorf("cannot find torrent %s", torrentName) } + dirCfg := torMgr.Config.(*config.ZurgConfigV1).GetDirectoryConfig(directory) + biggestFileSize := int64(0) + if dirCfg.OnlyShowTheBiggestFile { + biggestFileSize = tor.ComputeBiggestFileSize() + } + htmlDoc := "
    " filenames := tor.SelectedFiles.Keys() sort.Strings(filenames) @@ -67,6 +74,9 @@ func HandleListFiles(directory, torrentName string, torMgr *torrent.TorrentManag if !ok || !strings.HasPrefix(file.Link, "http") { continue } + if dirCfg.OnlyShowTheBiggestFile && file.Bytes < biggestFileSize { + continue + } filePath := filepath.Join(directory, torrentName, url.PathEscape(filename)) htmlDoc += fmt.Sprintf("
  1. %s
  2. ", filePath, filename) } diff --git a/internal/torrent/types.go b/internal/torrent/types.go index dffbf70..ddd8aaf 100644 --- a/internal/torrent/types.go +++ b/internal/torrent/types.go @@ -115,6 +115,16 @@ func (t *Torrent) ComputeTotalSize() int64 { return totalSize } +func (t *Torrent) ComputeBiggestFileSize() int64 { + biggestSize := int64(0) + t.SelectedFiles.IterCb(func(key string, value *File) { + if value.Bytes > biggestSize { + biggestSize = value.Bytes + } + }) + return biggestSize +} + func (t *Torrent) OlderThanDuration(duration time.Duration) bool { latestAdded, err := time.Parse(time.RFC3339, t.LatestAdded) if err != nil {