fix delete issues and added migrations

pull/24/head
Akhil Gupta 4 years ago
parent a069a5ed99
commit 1f24024839

@ -20,6 +20,7 @@ ENV CONFIG=/config
ENV DATA=/assets ENV DATA=/assets
ENV UID=998 ENV UID=998
ENV PID=100 ENV PID=100
ENV GIN_MODE=release
VOLUME ["/config", "/assets"] VOLUME ["/config", "/assets"]
RUN apk update && apk add ca-certificates && rm -rf /var/cache/apk/* RUN apk update && apk add ca-certificates && rm -rf /var/cache/apk/*
RUN mkdir -p /config; \ RUN mkdir -p /config; \

@ -71,6 +71,10 @@ hr{
{{else}} {{else}}
{{if not $setting.AutoDownload}} {{if not $setting.AutoDownload}}
<a class="button button-primary" onclick="downloadToDisk('{{.ID}}')" download>Download to Disk</a> <a class="button button-primary" onclick="downloadToDisk('{{.ID}}')" download>Download to Disk</a>
{{else}}
{{if eq .DownloadStatus 3}}
<a class="button button-primary" onclick="downloadToDisk('{{.ID}}')" download>Download to Disk</a>
{{end}}
{{end}} {{end}}
{{end }} {{end }}
</div> </div>

@ -35,8 +35,8 @@ func Init() (*gorm.DB, error) {
//Migrate Database //Migrate Database
func Migrate() { 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. // Using this function to get a connection, you can create your connection pool here.

@ -2,7 +2,6 @@ package db
import ( import (
"errors" "errors"
"time"
"gorm.io/gorm" "gorm.io/gorm"
"gorm.io/gorm/clause" "gorm.io/gorm/clause"
@ -66,12 +65,13 @@ func GetAllPodcastItemsByPodcastId(podcastId string, podcasts *[]PodcastItem) er
func GetAllPodcastItemsToBeDownloaded() (*[]PodcastItem, error) { func GetAllPodcastItemsToBeDownloaded() (*[]PodcastItem, error) {
var podcastItems []PodcastItem 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 return &podcastItems, result.Error
} }
func GetAllPodcastItemsAlreadyDownloaded() (*[]PodcastItem, error) { func GetAllPodcastItemsAlreadyDownloaded() (*[]PodcastItem, error) {
var podcastItems []PodcastItem 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 return &podcastItems, result.Error
} }

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

@ -41,11 +41,26 @@ type PodcastItem struct {
DownloadDate time.Time DownloadDate time.Time
DownloadPath string DownloadPath string
DownloadStatus DownloadStatus `gorm:"default:0"`
} }
type DownloadStatus int
const (
NotDownloaded DownloadStatus = iota
Downloading
Downloaded
Deleted
)
type Setting struct { type Setting struct {
Base Base
DownloadOnAdd bool `gorm:"default:true"` DownloadOnAdd bool `gorm:"default:true"`
InitialDownloadCount int `gorm:"default:5"` InitialDownloadCount int `gorm:"default:5"`
AutoDownload bool `gorm:"default:true"` AutoDownload bool `gorm:"default:true"`
} }
type Migration struct {
Base
Date time.Time
Name string
}

@ -55,7 +55,7 @@ func changeOwnership(path string) {
} }
func DeleteFile(filePath string) error { func DeleteFile(filePath string) error {
if _, err := os.Stat(filePath); os.IsNotExist(err) { if _, err := os.Stat(filePath); os.IsNotExist(err) {
return errors.New("File does not exist") return err
} }
if err := os.Remove(filePath); err != nil { if err := os.Remove(filePath); err != nil {
return err return err

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os"
"strconv" "strconv"
"time" "time"
@ -84,6 +85,7 @@ func AddPodcastItems(podcast *db.Podcast) error {
FileURL: obj.Enclosure.URL, FileURL: obj.Enclosure.URL,
GUID: obj.Guid.Text, GUID: obj.Guid.Text,
Image: obj.Image.Href, Image: obj.Image.Href,
DownloadStatus: db.NotDownloaded,
} }
db.CreatePodcastItem(&podcastItem) db.CreatePodcastItem(&podcastItem)
} }
@ -99,10 +101,11 @@ func SetPodcastItemAsDownloaded(id string, location string) error {
} }
podcastItem.DownloadDate = time.Now() podcastItem.DownloadDate = time.Now()
podcastItem.DownloadPath = location podcastItem.DownloadPath = location
podcastItem.DownloadStatus = db.Downloaded
return db.UpdatePodcastItem(&podcastItem) return db.UpdatePodcastItem(&podcastItem)
} }
func SetPodcastItemAsNotDownloaded(id string) error { func SetPodcastItemAsNotDownloaded(id string, downloadStatus db.DownloadStatus) error {
var podcastItem db.PodcastItem var podcastItem db.PodcastItem
err := db.GetPodcastItemById(id, &podcastItem) err := db.GetPodcastItemById(id, &podcastItem)
if err != nil { if err != nil {
@ -110,6 +113,7 @@ func SetPodcastItemAsNotDownloaded(id string) error {
} }
podcastItem.DownloadDate = time.Time{} podcastItem.DownloadDate = time.Time{}
podcastItem.DownloadPath = "" podcastItem.DownloadPath = ""
podcastItem.DownloadStatus = downloadStatus
return db.UpdatePodcastItem(&podcastItem) return db.UpdatePodcastItem(&podcastItem)
} }
@ -117,7 +121,7 @@ func SetPodcastItemAsNotDownloaded(id string) error {
func DownloadMissingEpisodes() error { func DownloadMissingEpisodes() error {
data, err := db.GetAllPodcastItemsToBeDownloaded() data, err := db.GetAllPodcastItemsToBeDownloaded()
//fmt.Println("Processing episodes: ", strconv.Itoa(len(*data))) fmt.Println("Processing episodes: ", strconv.Itoa(len(*data)))
if err != nil { if err != nil {
return err return err
} }
@ -138,7 +142,7 @@ func CheckMissingFiles() error {
for _, item := range *data { for _, item := range *data {
fileExists := FileExists(item.DownloadPath) fileExists := FileExists(item.DownloadPath)
if !fileExists { if !fileExists {
SetPodcastItemAsNotDownloaded(item.ID) SetPodcastItemAsNotDownloaded(item.ID, db.NotDownloaded)
} }
} }
return nil return nil
@ -154,10 +158,12 @@ func DeleteEpisodeFile(podcastItemId string) error {
} }
err = DeleteFile(podcastItem.DownloadPath) err = DeleteFile(podcastItem.DownloadPath)
if err != nil {
if !os.IsNotExist(err) {
return err return err
} }
return SetPodcastItemAsNotDownloaded(podcastItem.ID) fmt.Println("Setting file as deleted")
return SetPodcastItemAsNotDownloaded(podcastItem.ID, db.Deleted)
} }
func DownloadSingleEpisode(podcastItemId string) error { func DownloadSingleEpisode(podcastItemId string) error {
var podcastItem db.PodcastItem var podcastItem db.PodcastItem

Loading…
Cancel
Save