package main import ( "errors" "os" "path/filepath" "strings" "git.milar.in/milarin/anilist" "git.milar.in/nyaanime/model" ) type AnimePathPatternData struct { Title anilist.MediaTitle Episode int Ext string } func GetAnimeEpFilepath(animeEp model.AnimeEpisode, ext string) string { tmplData := AnimePathPatternData{ Title: animeEp.Anime.Title, Episode: animeEp.Episode, Ext: ext, } b := new(strings.Builder) if err := AnimeEpFilepathPattern.Execute(b, tmplData); err != nil { panic(err) } return filepath.Join(AnimePath, b.String()) } func AnimeEpExistsLocally(animeEp model.AnimeEpisode) bool { animeEpPath := GetAnimeEpFilepath(animeEp, "*") files, err := filepath.Glob(animeEpPath) if err != nil { panic(err) } return len(files) > 0 } 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 := 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 IsCurrentlyDownloading(animeEp model.AnimeEpisode) bool { animeEpPath := GetAnimeEpFilepath(animeEp, "lock") _, err := os.Stat(animeEpPath) return !errors.Is(err, os.ErrNotExist) } func SetCurrentlyDownloading(animeEp model.AnimeEpisode) error { animeEpPath := GetAnimeEpFilepath(animeEp, "lock") file, err := os.Create(animeEpPath) if err != nil { defer file.Close() } return err }