logic/file_props.go
2023-01-15 22:32:59 +01:00

59 lines
1.2 KiB
Go

package logic
import (
"path/filepath"
"git.milar.in/milarin/slices"
"git.milar.in/nyaanime/model"
"git.milar.in/nyaanime/parsers"
)
func GetAnimeEpProps(animeEp model.AnimeEpisode) (*FilePriority, bool) {
animeEpPath := GetAnimeEpFilepath(animeEp, "*")
files, err := filepath.Glob(animeEpPath)
if err != nil {
panic(ErrInvalidGlobSyntax.Wrap(err, animeEpPath))
}
var mostPrio *FilePriority
for _, file := range files {
props, err := parsers.AnalyzeFile(file)
if err != nil {
continue
}
if !HasFileEssentialProperties(props) {
continue
}
fp := NewFilePriority(props)
if mostPrio == nil || fp.Priority > mostPrio.Priority {
mostPrio = fp
}
}
return mostPrio, mostPrio != nil
}
func HasFileEssentialProperties(props model.PropertyHolder) bool {
if props.GetResolution() < MinResolution || props.GetResolution() > MaxResolution {
return false
}
for _, essentialLanguage := range EssentialLanguages {
if !slices.Contains(props.GetLanguages(), essentialLanguage) {
return false
}
}
for _, essentialSubtitle := range EssentialSubtitles {
if !slices.Contains(props.GetSubtitles(), essentialSubtitle) {
return false
}
}
return true
}