150 lines
3.4 KiB
Go
150 lines
3.4 KiB
Go
package config
|
|
|
|
import (
|
|
"regexp"
|
|
"strings"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
func loadV1Config(content []byte) (*ZurgConfigV1, error) {
|
|
var configV1 ZurgConfigV1
|
|
if err := yaml.Unmarshal(content, &configV1); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &configV1, nil
|
|
}
|
|
|
|
func (z *ZurgConfigV1) GetVersion() string {
|
|
return "v1"
|
|
}
|
|
|
|
func (z *ZurgConfigV1) GetToken() string {
|
|
return z.Token
|
|
}
|
|
|
|
func (z *ZurgConfigV1) GetPort() string {
|
|
return z.Port
|
|
}
|
|
|
|
func (z *ZurgConfigV1) GetNumOfWorkers() int {
|
|
return z.NumOfWorkers
|
|
}
|
|
|
|
func (z *ZurgConfigV1) GetRefreshEverySeconds() int {
|
|
return z.RefreshEverySeconds
|
|
}
|
|
|
|
func (z *ZurgConfigV1) GetCacheTimeHours() int {
|
|
return z.CacheTimeHours
|
|
}
|
|
|
|
func (z *ZurgConfigV1) GetDirectories() []string {
|
|
rootDirectories := make([]string, len(z.Directories))
|
|
i := 0
|
|
for directory := range z.Directories {
|
|
rootDirectories[i] = directory
|
|
i++
|
|
}
|
|
return rootDirectories
|
|
}
|
|
|
|
func (z *ZurgConfigV1) GetGroupMap() map[string][]string {
|
|
var groupMap = make(map[string][]string)
|
|
for directory, val := range z.Directories {
|
|
groupMap[val.Group] = append(groupMap[val.Group], directory)
|
|
}
|
|
return groupMap
|
|
}
|
|
|
|
func (z *ZurgConfigV1) MeetsConditions(directory, fileID, torrentName string) bool {
|
|
if _, ok := z.Directories[directory]; !ok {
|
|
return false
|
|
}
|
|
for _, filter := range z.Directories[directory].Filters {
|
|
if z.matchFilter(fileID, torrentName, filter) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (z *ZurgConfigV1) matchFilter(fileID, torrentName string, filter *FilterConditionsV1) bool {
|
|
if filter.ID != "" && fileID == filter.ID {
|
|
return true
|
|
}
|
|
if filter.RegexStr != "" {
|
|
regex := compilePattern(filter.RegexStr)
|
|
if regex.MatchString(torrentName) {
|
|
return true
|
|
}
|
|
}
|
|
if filter.ContainsStrict != "" && strings.Contains(torrentName, filter.ContainsStrict) {
|
|
return true
|
|
}
|
|
if filter.Contains != "" && strings.Contains(strings.ToLower(torrentName), strings.ToLower(filter.Contains)) {
|
|
return true
|
|
}
|
|
if filter.NotContainsStrict != "" && !strings.Contains(torrentName, filter.NotContainsStrict) {
|
|
return true
|
|
}
|
|
if filter.NotContains != "" && !strings.Contains(strings.ToLower(torrentName), strings.ToLower(filter.NotContains)) {
|
|
return true
|
|
}
|
|
if len(filter.And) > 0 {
|
|
andResult := true
|
|
for _, andFilter := range filter.And {
|
|
andResult = andResult && z.matchFilter(fileID, torrentName, andFilter)
|
|
if !andResult {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
if len(filter.Or) > 0 {
|
|
for _, orFilter := range filter.Or {
|
|
if z.matchFilter(fileID, torrentName, orFilter) {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func compilePattern(pattern string) *regexp.Regexp {
|
|
flags := map[rune]string{
|
|
'i': "(?i)",
|
|
'm': "(?m)",
|
|
's': "(?s)",
|
|
'x': "(?x)",
|
|
}
|
|
|
|
lastSlash := strings.LastIndex(pattern, "/")
|
|
secondLastSlash := strings.LastIndex(pattern[:lastSlash], "/")
|
|
|
|
// Extract the core pattern
|
|
corePattern := pattern[secondLastSlash+1 : lastSlash]
|
|
|
|
// Extract and process flags
|
|
flagSection := pattern[lastSlash+1:]
|
|
flagString := ""
|
|
processedFlags := make(map[rune]bool)
|
|
for _, flag := range flagSection {
|
|
if replacement, ok := flags[flag]; ok && !processedFlags[flag] {
|
|
flagString += replacement
|
|
processedFlags[flag] = true
|
|
}
|
|
}
|
|
|
|
// Combine the processed flags with the core pattern
|
|
finalPattern := flagString + corePattern
|
|
|
|
// Validate pattern
|
|
if finalPattern == "" || finalPattern == flagString {
|
|
return nil
|
|
}
|
|
|
|
return regexp.MustCompile(finalPattern)
|
|
}
|