diff --git a/handle_file.go b/handle_file.go index 6de5578..62af091 100644 --- a/handle_file.go +++ b/handle_file.go @@ -8,6 +8,7 @@ import ( "path/filepath" "time" + "git.milar.in/milarin/adverr" "git.milar.in/nyaanime/logic" "git.milar.in/nyaanime/model" "git.milar.in/nyaanime/parsers" @@ -78,7 +79,7 @@ func HandleParsedFile(w io.Writer, parsedFile *model.ParsedFile) { } if !animeEpNotExistLocally || newFilePrio.Priority > oldFilePrio.Priority { - // TODO remove old file when overwriting existing episode (might have other file extension) + DeleteOldAnimeEpisode(parsedFile.AnimeEpisode()) if err := OrganizeAnimeEpisode(w, parsedFile); err != nil { fmt.Fprint(w, color.RedString("\terror: %s\n", err.Error())) } @@ -139,3 +140,21 @@ func OrganizeAnimeEpisode(w io.Writer, parsedFile *model.ParsedFile) error { return nil } + +func DeleteOldAnimeEpisode(animeEp model.AnimeEpisode) { + animeEpPath := logic.GetAnimeEpFilepath(animeEp, "*") + files, err := filepath.Glob(animeEpPath) + if err != nil { + panic(logic.ErrInvalidGlobSyntax.Wrap(err, animeEpPath)) + } + + for _, file := range files { + if filepath.Ext(file) == ".lock" { + continue + } + + if err := os.Remove(file); err != nil { + adverr.Println(err) + } + } +}