Fix directory mapping
This commit is contained in:
@@ -16,7 +16,7 @@ type ConfigInterface interface {
|
||||
EnableRepair() bool
|
||||
GetPort() string
|
||||
GetDirectories() []string
|
||||
MeetsConditions(directory, fileID, fileName string) bool
|
||||
MeetsConditions(directory, torrentID, torrentName string, fileNames []string) bool
|
||||
}
|
||||
|
||||
func LoadZurgConfig(filename string) (ConfigInterface, error) {
|
||||
|
||||
@@ -2,6 +2,7 @@ package config
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
@@ -56,25 +57,38 @@ func (z *ZurgConfigV1) GetDirectories() []string {
|
||||
|
||||
func (z *ZurgConfigV1) GetGroupMap() map[string][]string {
|
||||
var groupMap = make(map[string][]string)
|
||||
var groupOrderMap = make(map[string]int) // To store GroupOrder for each group
|
||||
|
||||
// Populate the groupMap and groupOrderMap
|
||||
for directory, val := range z.Directories {
|
||||
groupMap[val.Group] = append(groupMap[val.Group], directory)
|
||||
groupOrderMap[val.Group] = val.GroupOrder
|
||||
}
|
||||
|
||||
// Sort the slice based on GroupOrder
|
||||
for group, dirs := range groupMap {
|
||||
sort.Slice(dirs, func(i, j int) bool {
|
||||
return groupOrderMap[dirs[i]] < groupOrderMap[dirs[j]]
|
||||
})
|
||||
groupMap[group] = dirs
|
||||
}
|
||||
|
||||
return groupMap
|
||||
}
|
||||
|
||||
func (z *ZurgConfigV1) MeetsConditions(directory, fileID, torrentName string) bool {
|
||||
func (z *ZurgConfigV1) MeetsConditions(directory, torrentID, torrentName string, fileNames []string) bool {
|
||||
if _, ok := z.Directories[directory]; !ok {
|
||||
return false
|
||||
}
|
||||
for _, filter := range z.Directories[directory].Filters {
|
||||
if z.matchFilter(fileID, torrentName, filter) {
|
||||
if z.matchFilter(torrentID, torrentName, fileNames, filter) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (z *ZurgConfigV1) matchFilter(fileID, torrentName string, filter *FilterConditionsV1) bool {
|
||||
func (z *ZurgConfigV1) matchFilter(fileID, torrentName string, fileNames []string, filter *FilterConditionsV1) bool {
|
||||
if filter.ID != "" && fileID == filter.ID {
|
||||
return true
|
||||
}
|
||||
@@ -99,7 +113,7 @@ func (z *ZurgConfigV1) matchFilter(fileID, torrentName string, filter *FilterCon
|
||||
if len(filter.And) > 0 {
|
||||
andResult := true
|
||||
for _, andFilter := range filter.And {
|
||||
andResult = andResult && z.matchFilter(fileID, torrentName, andFilter)
|
||||
andResult = andResult && z.matchFilter(fileID, torrentName, fileNames, andFilter)
|
||||
if !andResult {
|
||||
return false
|
||||
}
|
||||
@@ -108,7 +122,29 @@ func (z *ZurgConfigV1) matchFilter(fileID, torrentName string, filter *FilterCon
|
||||
}
|
||||
if len(filter.Or) > 0 {
|
||||
for _, orFilter := range filter.Or {
|
||||
if z.matchFilter(fileID, torrentName, orFilter) {
|
||||
if z.matchFilter(fileID, torrentName, fileNames, orFilter) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
if filter.FileInsideRegexStr != "" {
|
||||
for _, filename := range fileNames {
|
||||
regex := compilePattern(filter.FileInsideRegexStr)
|
||||
if regex.MatchString(filename) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
if filter.FileInsideContains != "" {
|
||||
for _, filename := range fileNames {
|
||||
if strings.Contains(strings.ToLower(filename), strings.ToLower(filter.FileInsideContains)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
if filter.FileInsideContainsStrict != "" {
|
||||
for _, filename := range fileNames {
|
||||
if strings.Contains(filename, filter.FileInsideContainsStrict) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,17 +5,23 @@ type ZurgConfigV1 struct {
|
||||
Directories map[string]*DirectoryFilterConditionsV1 `yaml:"directories"`
|
||||
}
|
||||
type DirectoryFilterConditionsV1 struct {
|
||||
Group string `yaml:"group"`
|
||||
Filters []*FilterConditionsV1 `yaml:"filters"`
|
||||
GroupOrder int `yaml:"group_order"`
|
||||
Group string `yaml:"group"`
|
||||
Filters []*FilterConditionsV1 `yaml:"filters"`
|
||||
}
|
||||
|
||||
type FilterConditionsV1 struct {
|
||||
RegexStr string `yaml:"regex"`
|
||||
Contains string `yaml:"contains"`
|
||||
ContainsStrict string `yaml:"contains_strict"`
|
||||
NotContains string `yaml:"not_contains"`
|
||||
NotContainsStrict string `yaml:"not_contains_strict"`
|
||||
ID string `yaml:"id"`
|
||||
And []*FilterConditionsV1 `yaml:"and"`
|
||||
Or []*FilterConditionsV1 `yaml:"or"`
|
||||
ID string `yaml:"id"`
|
||||
RegexStr string `yaml:"regex"`
|
||||
Contains string `yaml:"contains"`
|
||||
ContainsStrict string `yaml:"contains_strict"`
|
||||
NotContains string `yaml:"not_contains"`
|
||||
NotContainsStrict string `yaml:"not_contains_strict"`
|
||||
|
||||
And []*FilterConditionsV1 `yaml:"and"`
|
||||
Or []*FilterConditionsV1 `yaml:"or"`
|
||||
|
||||
FileInsideRegexStr string `yaml:"any_file_inside_regex"`
|
||||
FileInsideContains string `yaml:"any_file_inside_contains"`
|
||||
FileInsideContainsStrict string `yaml:"any_file_inside_contains_strict"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user