fix github issue #26 / don't download file if already exists

pull/18/merge
Akhil Gupta 4 years ago
parent eb96373e4e
commit af7d421127

@ -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 {

@ -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
}

@ -19,7 +19,7 @@ type Podcast struct {
LastEpisode *time.Time
PodcastItems []PodcastItem `json:"-"`
PodcastItems []PodcastItem
DownloadedEpisodesCount int `gorm:"-"`
DownloadingEpisodesCount int `gorm:"-"`

@ -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)

@ -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 {

Loading…
Cancel
Save