From 841989787020666689fb8c87fd3f456327326e28 Mon Sep 17 00:00:00 2001 From: Akhil Gupta Date: Fri, 30 Oct 2020 18:00:16 +0530 Subject: [PATCH] background checking of deleted files --- db/dbfunctions.go | 5 +++++ main.go | 1 + service/fileService.go | 5 +++++ service/podcastService.go | 15 +++++++++++++++ 4 files changed, 26 insertions(+) diff --git a/db/dbfunctions.go b/db/dbfunctions.go index a26a147..7667348 100644 --- a/db/dbfunctions.go +++ b/db/dbfunctions.go @@ -69,6 +69,11 @@ func GetAllPodcastItemsToBeDownloaded() (*[]PodcastItem, error) { result := DB.Debug().Preload(clause.Associations).Where("download_date=?", time.Time{}).Find(&podcastItems) return &podcastItems, result.Error } +func GetAllPodcastItemsAlreadyDownloaded() (*[]PodcastItem, error) { + var podcastItems []PodcastItem + result := DB.Debug().Preload(clause.Associations).Where("download_date!=?", time.Time{}).Find(&podcastItems) + return &podcastItems, result.Error +} func GetPodcastItemByPodcastIdAndGUID(podcastId string, guid string, podcastItem *PodcastItem) error { diff --git a/main.go b/main.go index c802c14..b6ed03c 100644 --- a/main.go +++ b/main.go @@ -110,6 +110,7 @@ func intiCron() { } //gocron.Every(uint64(checkFrequency)).Minutes().Do(service.DownloadMissingEpisodes) gocron.Every(uint64(checkFrequency)).Minutes().Do(service.RefreshEpisodes) + gocron.Every(uint64(checkFrequency)).Minutes().Do(service.CheckMissingFiles) <-gocron.Start() } diff --git a/service/fileService.go b/service/fileService.go index ceffd23..1ae1501 100644 --- a/service/fileService.go +++ b/service/fileService.go @@ -58,6 +58,11 @@ func DeleteFile(filePath string) error { } return nil } +func FileExists(filePath string) bool { + _, err := os.Stat(filePath) + return err == nil + +} func httpClient() *http.Client { client := http.Client{ CheckRedirect: func(r *http.Request, via []*http.Request) error { diff --git a/service/podcastService.go b/service/podcastService.go index 8cc3061..7606dfd 100644 --- a/service/podcastService.go +++ b/service/podcastService.go @@ -128,6 +128,21 @@ func DownloadMissingEpisodes() error { } return nil } +func CheckMissingFiles() error { + data, err := db.GetAllPodcastItemsAlreadyDownloaded() + + //fmt.Println("Processing episodes: ", strconv.Itoa(len(*data))) + if err != nil { + return err + } + for _, item := range *data { + fileExists := FileExists(item.DownloadPath) + if !fileExists { + SetPodcastItemAsNotDownloaded(item.ID) + } + } + return nil +} func DeleteEpisodeFile(podcastItemId string) error { var podcastItem db.PodcastItem