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 GetLocalAnimeEpProperties(animeEp model.AnimeEpisode) (*FilePriority, bool, error) { animeEpPath := GetAnimeEpFilepath(animeEp, "*") files, err := filepath.Glob(animeEpPath) if err != nil { return nil, false, 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, nil } func IsAnimeEpDownloading(animeEp model.AnimeEpisode) bool { animeEpPath := GetAnimeEpFilepath(animeEp, "lock") _, err := os.Stat(animeEpPath) return !errors.Is(err, os.ErrNotExist) } func SetAnimeEpDownloading(animeEp model.AnimeEpisode) error { animeEpPath := GetAnimeEpFilepath(animeEp, "lock") file, err := os.Create(animeEpPath) if err != nil { defer file.Close() } return err }