diff --git a/controllers/podcast.go b/controllers/podcast.go index c143613..e07399f 100644 --- a/controllers/podcast.go +++ b/controllers/podcast.go @@ -132,7 +132,7 @@ func DownloadAllEpisodesByPodcastId(c *gin.Context) { if c.ShouldBindUri(&searchByIdQuery) == nil { - err := db.SetAllEpisodesToDownload(searchByIdQuery.Id) + err := service.SetAllEpisodesToDownload(searchByIdQuery.Id) fmt.Println(err) go service.RefreshEpisodes() c.JSON(200, gin.H{}) @@ -236,10 +236,7 @@ func AddPodcast(c *gin.Context) { if err == nil { pod, err := service.AddPodcast(addPodcastData.Url) if err == nil { - setting := c.MustGet("setting").(*db.Setting) - if setting.DownloadOnAdd { - go service.RefreshEpisodes() - } + go service.RefreshEpisodes() c.JSON(200, pod) } else { if v, ok := err.(*model.PodcastAlreadyExistsError); ok { diff --git a/db/dbfunctions.go b/db/dbfunctions.go index c32ed7a..eaa5653 100644 --- a/db/dbfunctions.go +++ b/db/dbfunctions.go @@ -67,9 +67,9 @@ func DeletePodcastById(id string) error { return result.Error } -func GetAllPodcastItemsByPodcastId(podcastId string, podcasts *[]PodcastItem) error { +func GetAllPodcastItemsByPodcastId(podcastId string, podcastItems *[]PodcastItem) error { - result := DB.Preload(clause.Associations).Where(&PodcastItem{PodcastID: podcastId}).Find(&podcasts) + result := DB.Preload(clause.Associations).Where(&PodcastItem{PodcastID: podcastId}).Find(&podcastItems) return result.Error } diff --git a/db/podcast.go b/db/podcast.go index d205b2d..dccca33 100644 --- a/db/podcast.go +++ b/db/podcast.go @@ -19,7 +19,7 @@ type Podcast struct { LastEpisode *time.Time - PodcastItems []PodcastItem `json:"-"` + PodcastItems []PodcastItem DownloadedEpisodesCount int `gorm:"-"` DownloadingEpisodesCount int `gorm:"-"` diff --git a/service/fileService.go b/service/fileService.go index 01ea1fe..40df737 100644 --- a/service/fileService.go +++ b/service/fileService.go @@ -35,6 +35,12 @@ func Download(link string, episodeTitle string, podcastName string, prefix strin } folder := createIfFoldeDoesntExist(podcastName) finalPath := path.Join(folder, fileName) + + if _, err := os.Stat(finalPath); !os.IsNotExist(err) { + changeOwnership(finalPath) + return finalPath, nil + } + file, err := os.Create(finalPath) if err != nil { Logger.Errorw("Error creating file"+link, err) diff --git a/service/podcastService.go b/service/podcastService.go index 9d5194c..e3249b6 100644 --- a/service/podcastService.go +++ b/service/podcastService.go @@ -72,7 +72,6 @@ func AddOpml(content string) error { return errors.New("Invalid file format") } var wg sync.WaitGroup - setting := db.GetOrCreateSetting() for _, outline := range model.Body.Outline { if outline.XmlUrl != "" { wg.Add(1) @@ -94,9 +93,7 @@ func AddOpml(content string) error { } } wg.Wait() - if setting.DownloadOnAdd { - go RefreshEpisodes() - } + go RefreshEpisodes() return nil } @@ -154,7 +151,7 @@ func AddPodcast(url string) (db.Podcast, error) { } -func AddPodcastItems(podcast *db.Podcast) error { +func AddPodcastItems(podcast *db.Podcast, newPodcast bool) error { //fmt.Println("Creating: " + podcast.ID) data, err := FetchURL(podcast.URL) if err != nil { @@ -210,6 +207,11 @@ func AddPodcastItems(podcast *db.Podcast) error { } else { downloadStatus = db.Deleted } + + if newPodcast && !setting.DownloadOnAdd { + downloadStatus = db.Deleted + } + podcastItem = db.PodcastItem{ PodcastID: podcast.ID, Title: obj.Title, @@ -277,6 +279,12 @@ func SetPodcastItemPlayedStatus(id string, isPlayed bool) error { return db.UpdatePodcastItem(&podcastItem) } func SetAllEpisodesToDownload(podcastId string) error { + var podcast db.Podcast + err := db.GetPodcastById(podcastId, &podcast) + if err != nil { + return err + } + AddPodcastItems(&podcast, false) return db.SetAllEpisodesToDownload(podcastId) } @@ -380,11 +388,12 @@ func RefreshEpisodes() error { return err } for _, item := range data { - if item.LastEpisode == nil { + isNewPodcast := item.LastEpisode == nil + if isNewPodcast { fmt.Println(item.Title) db.ForceSetLastEpisodeDate(item.ID) } - AddPodcastItems(&item) + AddPodcastItems(&item, isNewPodcast) } setting := db.GetOrCreateSetting() if setting.AutoDownload {