From 965b5957e10723f4eca0445e6bc639c953a8251a Mon Sep 17 00:00:00 2001 From: milarin Date: Sun, 15 Jan 2023 21:49:49 +0100 Subject: [PATCH] delete old anime episode when overwriting with higher priority file --- handle_file.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) 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) + } + } +}