diff --git a/Dockerfile b/Dockerfile index d0a8e78..9506070 100644 --- a/Dockerfile +++ b/Dockerfile @@ -20,6 +20,7 @@ ENV CONFIG=/config ENV DATA=/assets ENV UID=998 ENV PID=100 +ENV GIN_MODE=release VOLUME ["/config", "/assets"] RUN apk update && apk add ca-certificates && rm -rf /var/cache/apk/* RUN mkdir -p /config; \ diff --git a/client/episodes.html b/client/episodes.html index 64b1cc0..9f45010 100644 --- a/client/episodes.html +++ b/client/episodes.html @@ -69,9 +69,13 @@ hr{ Delete {{else}} - {{if not $setting.AutoDownload}} - Download to Disk - {{end}} + {{if not $setting.AutoDownload}} + Download to Disk + {{else}} + {{if eq .DownloadStatus 3}} + Download to Disk + {{end}} + {{end}} {{end }}
diff --git a/db/db.go b/db/db.go index e4c0d80..2535005 100644 --- a/db/db.go +++ b/db/db.go @@ -35,8 +35,8 @@ func Init() (*gorm.DB, error) { //Migrate Database func Migrate() { - DB.AutoMigrate(&Podcast{}, &PodcastItem{}, &Setting{}) - + DB.AutoMigrate(&Podcast{}, &PodcastItem{}, &Setting{}, &Migration{}) + RunMigrations() } // Using this function to get a connection, you can create your connection pool here. diff --git a/db/dbfunctions.go b/db/dbfunctions.go index 7667348..3053110 100644 --- a/db/dbfunctions.go +++ b/db/dbfunctions.go @@ -2,7 +2,6 @@ package db import ( "errors" - "time" "gorm.io/gorm" "gorm.io/gorm/clause" @@ -66,12 +65,13 @@ func GetAllPodcastItemsByPodcastId(podcastId string, podcasts *[]PodcastItem) er func GetAllPodcastItemsToBeDownloaded() (*[]PodcastItem, error) { var podcastItems []PodcastItem - result := DB.Debug().Preload(clause.Associations).Where("download_date=?", time.Time{}).Find(&podcastItems) + result := DB.Debug().Preload(clause.Associations).Where("download_status=?", NotDownloaded).Find(&podcastItems) + //fmt.Println("To be downloaded : " + string(len(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) + result := DB.Debug().Preload(clause.Associations).Where("download_status=?", Downloaded).Find(&podcastItems) return &podcastItems, result.Error } diff --git a/db/migrations.go b/db/migrations.go new file mode 100644 index 0000000..c5b03b3 --- /dev/null +++ b/db/migrations.go @@ -0,0 +1,43 @@ +package db + +import ( + "errors" + "fmt" + "time" + + "gorm.io/gorm" +) + +type localMigration struct { + Name string + Query string +} + +var migrations = []localMigration{ + { + Name: "2020_11_03_04_42_SetDefaultDownloadStatus", + Query: "update podcast_items set download_status=2 where download_path!='' and download_status=0", + }, +} + +func RunMigrations() { + for _, mig := range migrations { + ExecuteAndSaveMigration(mig.Name, mig.Query) + } +} +func ExecuteAndSaveMigration(name string, query string) error { + var migration Migration + result := DB.Where("name=?", name).First(&migration) + if errors.Is(result.Error, gorm.ErrRecordNotFound) { + fmt.Println(query) + result = DB.Debug().Exec(query) + if result.Error == nil { + DB.Save(&Migration{ + Date: time.Now(), + Name: name, + }) + } + return result.Error + } + return nil +} diff --git a/db/podcast.go b/db/podcast.go index a68ae5c..dfa4f27 100644 --- a/db/podcast.go +++ b/db/podcast.go @@ -39,13 +39,28 @@ type PodcastItem struct { GUID string Image string - DownloadDate time.Time - DownloadPath string + DownloadDate time.Time + DownloadPath string + DownloadStatus DownloadStatus `gorm:"default:0"` } +type DownloadStatus int + +const ( + NotDownloaded DownloadStatus = iota + Downloading + Downloaded + Deleted +) + type Setting struct { Base DownloadOnAdd bool `gorm:"default:true"` InitialDownloadCount int `gorm:"default:5"` AutoDownload bool `gorm:"default:true"` } +type Migration struct { + Base + Date time.Time + Name string +} diff --git a/service/fileService.go b/service/fileService.go index b02f3d5..6179ba8 100644 --- a/service/fileService.go +++ b/service/fileService.go @@ -55,7 +55,7 @@ func changeOwnership(path string) { } func DeleteFile(filePath string) error { if _, err := os.Stat(filePath); os.IsNotExist(err) { - return errors.New("File does not exist") + return err } if err := os.Remove(filePath); err != nil { return err diff --git a/service/podcastService.go b/service/podcastService.go index 7606dfd..6ae921b 100644 --- a/service/podcastService.go +++ b/service/podcastService.go @@ -6,6 +6,7 @@ import ( "fmt" "io/ioutil" "net/http" + "os" "strconv" "time" @@ -75,15 +76,16 @@ func AddPodcastItems(podcast *db.Podcast) error { duration, _ := strconv.Atoi(obj.Duration) pubDate, _ := time.Parse(time.RFC1123Z, obj.PubDate) podcastItem = db.PodcastItem{ - PodcastID: podcast.ID, - Title: obj.Title, - Summary: strip.StripTags(obj.Summary), - EpisodeType: obj.EpisodeType, - Duration: duration, - PubDate: pubDate, - FileURL: obj.Enclosure.URL, - GUID: obj.Guid.Text, - Image: obj.Image.Href, + PodcastID: podcast.ID, + Title: obj.Title, + Summary: strip.StripTags(obj.Summary), + EpisodeType: obj.EpisodeType, + Duration: duration, + PubDate: pubDate, + FileURL: obj.Enclosure.URL, + GUID: obj.Guid.Text, + Image: obj.Image.Href, + DownloadStatus: db.NotDownloaded, } db.CreatePodcastItem(&podcastItem) } @@ -99,10 +101,11 @@ func SetPodcastItemAsDownloaded(id string, location string) error { } podcastItem.DownloadDate = time.Now() podcastItem.DownloadPath = location + podcastItem.DownloadStatus = db.Downloaded return db.UpdatePodcastItem(&podcastItem) } -func SetPodcastItemAsNotDownloaded(id string) error { +func SetPodcastItemAsNotDownloaded(id string, downloadStatus db.DownloadStatus) error { var podcastItem db.PodcastItem err := db.GetPodcastItemById(id, &podcastItem) if err != nil { @@ -110,6 +113,7 @@ func SetPodcastItemAsNotDownloaded(id string) error { } podcastItem.DownloadDate = time.Time{} podcastItem.DownloadPath = "" + podcastItem.DownloadStatus = downloadStatus return db.UpdatePodcastItem(&podcastItem) } @@ -117,7 +121,7 @@ func SetPodcastItemAsNotDownloaded(id string) error { func DownloadMissingEpisodes() error { data, err := db.GetAllPodcastItemsToBeDownloaded() - //fmt.Println("Processing episodes: ", strconv.Itoa(len(*data))) + fmt.Println("Processing episodes: ", strconv.Itoa(len(*data))) if err != nil { return err } @@ -138,7 +142,7 @@ func CheckMissingFiles() error { for _, item := range *data { fileExists := FileExists(item.DownloadPath) if !fileExists { - SetPodcastItemAsNotDownloaded(item.ID) + SetPodcastItemAsNotDownloaded(item.ID, db.NotDownloaded) } } return nil @@ -154,10 +158,12 @@ func DeleteEpisodeFile(podcastItemId string) error { } err = DeleteFile(podcastItem.DownloadPath) - if err != nil { + + if !os.IsNotExist(err) { return err } - return SetPodcastItemAsNotDownloaded(podcastItem.ID) + fmt.Println("Setting file as deleted") + return SetPodcastItemAsNotDownloaded(podcastItem.ID, db.Deleted) } func DownloadSingleEpisode(podcastItemId string) error { var podcastItem db.PodcastItem