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 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; \

@ -69,9 +69,13 @@ hr{
<a class="button button" onclick="deleteFile('{{.ID}}')" download>Delete</a>
{{else}}
{{if not $setting.AutoDownload}}
<a class="button button-primary" onclick="downloadToDisk('{{.ID}}')" download>Download to Disk</a>
{{end}}
{{if not $setting.AutoDownload}}
<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 }}
</div>
<div class="columns one">

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

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

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

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

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

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

Loading…
Cancel
Save