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