downloader/local_file_check.go

93 lines
1.8 KiB
Go
Raw Normal View History

package main
import (
2022-08-23 11:48:26 +02:00
"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)
}
2022-08-23 11:48:26 +02:00
return filepath.Join(AnimePath, b.String())
}
func AnimeEpExistsLocally(animeEp model.AnimeEpisode) bool {
2022-08-23 11:48:26 +02:00
animeEpPath := GetAnimeEpFilepath(animeEp, "*")
files, err := filepath.Glob(animeEpPath)
if err != nil {
panic(err)
}
return len(files) > 0
}
2022-08-23 13:24:03 +02:00
func GetAnimeEpProps(animeEp model.AnimeEpisode) (*FilePriority, bool) {
2022-08-23 11:48:26 +02:00
animeEpPath := GetAnimeEpFilepath(animeEp, "*")
files, err := filepath.Glob(animeEpPath)
if err != nil {
2022-08-23 13:24:03 +02:00
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
}
}
2022-08-23 13:24:03 +02:00
return mostPrio, mostPrio != nil
}
2022-08-23 13:24:03 +02:00
func IsCurrentlyDownloading(animeEp model.AnimeEpisode) bool {
2022-08-23 11:48:26 +02:00
animeEpPath := GetAnimeEpFilepath(animeEp, "lock")
_, err := os.Stat(animeEpPath)
return !errors.Is(err, os.ErrNotExist)
}
2022-08-23 13:24:03 +02:00
func SetCurrentlyDownloading(animeEp model.AnimeEpisode) error {
2022-08-23 11:48:26 +02:00
animeEpPath := GetAnimeEpFilepath(animeEp, "lock")
2022-08-25 23:25:48 +02:00
dir := filepath.Dir(animeEpPath)
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
2022-08-23 11:48:26 +02:00
file, err := os.Create(animeEpPath)
if err != nil {
defer file.Close()
}
2022-08-23 11:48:26 +02:00
return err
}