From d930e4a64e73d41624cf8390998bbf477e01852c Mon Sep 17 00:00:00 2001 From: Akhil Gupta Date: Tue, 9 Mar 2021 15:00:59 +0530 Subject: [PATCH] start capturing size in db --- db/dbfunctions.go | 37 +++++++++++++++++++++++-------------- db/podcast.go | 2 ++ main.go | 1 + service/fileService.go | 28 ++++++++++++++++++++++++++++ service/podcastService.go | 39 ++++++++++++++++++++++++++++++++++++++- 5 files changed, 92 insertions(+), 15 deletions(-) diff --git a/db/dbfunctions.go b/db/dbfunctions.go index c677d8d..dfd9b91 100644 --- a/db/dbfunctions.go +++ b/db/dbfunctions.go @@ -23,16 +23,20 @@ func GetAllPodcasts(podcasts *[]Podcast, sorting string) error { if sorting == "" { sorting = "created_at" } - result := DB.Debug().Preload("Tags").Order(sorting).Find(&podcasts) + result := DB.Preload("Tags").Order(sorting).Find(&podcasts) return result.Error } func GetAllPodcastItems(podcasts *[]PodcastItem) error { - result := DB.Preload("Podcast").Order("pub_date desc").Find(&podcasts) return result.Error } +func GetAllPodcastItemsWithoutSize() (*[]PodcastItem, error) { + var podcasts []PodcastItem + result := DB.Where("file_size<=?", 0).Order("pub_date desc").Find(&podcasts) + return &podcasts, result.Error +} func GetPaginatedPodcastItems(page int, count int, downloadedOnly *bool, playedOnly *bool, fromDate time.Time, podcasts *[]PodcastItem, total *int64) error { - query := DB.Debug().Preload("Podcast") + query := DB.Preload("Podcast") if downloadedOnly != nil { if *downloadedOnly { query = query.Where("download_status=?", Downloaded) @@ -108,30 +112,35 @@ func GetAllPodcastItemsByPodcastIds(podcastIds []string, podcastItems *[]Podcast } func SetAllEpisodesToDownload(podcastId string) error { - result := DB.Debug().Model(PodcastItem{}).Where(&PodcastItem{PodcastID: podcastId, DownloadStatus: Deleted}).Update("download_status", NotDownloaded) + result := DB.Model(PodcastItem{}).Where(&PodcastItem{PodcastID: podcastId, DownloadStatus: Deleted}).Update("download_status", NotDownloaded) return result.Error } func UpdateLastEpisodeDateForPodcast(podcastId string, lastEpisode time.Time) error { - result := DB.Debug().Model(Podcast{}).Where("id=?", podcastId).Update("last_episode", lastEpisode) + result := DB.Model(Podcast{}).Where("id=?", podcastId).Update("last_episode", lastEpisode) + return result.Error +} + +func UpdatePodcastItemFileSize(podcastItemId string, size int64) error { + result := DB.Model(PodcastItem{}).Where("id=?", podcastItemId).Update("file_size", size) return result.Error } func GetAllPodcastItemsWithoutImage() (*[]PodcastItem, error) { var podcastItems []PodcastItem - result := DB.Debug().Preload(clause.Associations).Where("local_image is ?", nil).Where("image != ?", "").Where("download_status=?", Downloaded).Order("created_at desc").Find(&podcastItems) + result := DB.Preload(clause.Associations).Where("local_image is ?", nil).Where("image != ?", "").Where("download_status=?", Downloaded).Order("created_at desc").Find(&podcastItems) //fmt.Println("To be downloaded : " + string(len(podcastItems))) return &podcastItems, result.Error } func GetAllPodcastItemsToBeDownloaded() (*[]PodcastItem, error) { var podcastItems []PodcastItem - result := DB.Debug().Preload(clause.Associations).Where("download_status=?", NotDownloaded).Find(&podcastItems) + result := DB.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_status=?", Downloaded).Find(&podcastItems) + result := DB.Preload(clause.Associations).Where("download_status=?", Downloaded).Find(&podcastItems) return &podcastItems, result.Error } @@ -246,7 +255,7 @@ func Unlock(name string) { func UnlockMissedJobs() { var jobLocks []JobLock - result := DB.Debug().Find(&jobLocks) + result := DB.Find(&jobLocks) if result.Error != nil { return } @@ -269,7 +278,7 @@ func GetAllTags(sorting string) (*[]Tag, error) { if sorting == "" { sorting = "created_at" } - result := DB.Debug().Preload(clause.Associations).Order(sorting).Find(&tags) + result := DB.Preload(clause.Associations).Order(sorting).Find(&tags) return &tags, result.Error } @@ -295,7 +304,7 @@ func GetTagByLabel(label string) (*Tag, error) { } func CreateTag(tag *Tag) error { - tx := DB.Debug().Omit("Podcasts").Create(&tag) + tx := DB.Omit("Podcasts").Create(&tag) return tx.Error } func UpdateTag(tag *Tag) error { @@ -303,15 +312,15 @@ func UpdateTag(tag *Tag) error { return tx.Error } func AddTagToPodcast(id, tagId string) error { - tx := DB.Debug().Exec("INSERT INTO `podcast_tags` (`podcast_id`,`tag_id`) VALUES (?,?) ON CONFLICT DO NOTHING", id, tagId) + tx := DB.Exec("INSERT INTO `podcast_tags` (`podcast_id`,`tag_id`) VALUES (?,?) ON CONFLICT DO NOTHING", id, tagId) return tx.Error } func RemoveTagFromPodcast(id, tagId string) error { - tx := DB.Debug().Exec("DELETE FROM `podcast_tags` WHERE `podcast_id`=? AND `tag_id`=?", id, tagId) + tx := DB.Exec("DELETE FROM `podcast_tags` WHERE `podcast_id`=? AND `tag_id`=?", id, tagId) return tx.Error } func UntagAllByTagId(tagId string) error { - tx := DB.Debug().Exec("DELETE FROM `podcast_tags` WHERE `tag_id`=?", tagId) + tx := DB.Exec("DELETE FROM `podcast_tags` WHERE `tag_id`=?", tagId) return tx.Error } diff --git a/db/podcast.go b/db/podcast.go index 6a82897..98fc5ec 100644 --- a/db/podcast.go +++ b/db/podcast.go @@ -56,6 +56,8 @@ type PodcastItem struct { BookmarkDate time.Time LocalImage string + + FileSize int64 } type DownloadStatus int diff --git a/main.go b/main.go index c9b9e4e..2a7bb0f 100644 --- a/main.go +++ b/main.go @@ -196,6 +196,7 @@ func intiCron() { gocron.Every(uint64(checkFrequency)).Minutes().Do(service.RefreshEpisodes) gocron.Every(uint64(checkFrequency)).Minutes().Do(service.CheckMissingFiles) gocron.Every(uint64(checkFrequency) * 2).Minutes().Do(service.UnlockMissedJobs) + gocron.Every(uint64(checkFrequency) * 3).Minutes().Do(service.UpdateAllFileSizes) gocron.Every(uint64(checkFrequency)).Minutes().Do(service.DownloadMissingImages) gocron.Every(2).Days().Do(service.CreateBackup) <-gocron.Start() diff --git a/service/fileService.go b/service/fileService.go index ea25a9c..50d4dea 100644 --- a/service/fileService.go +++ b/service/fileService.go @@ -136,6 +136,14 @@ func GetAllBackupFiles() ([]string, error) { return files, err } +func GetFileSize(path string) (int64, error) { + info, err := os.Stat(path) + if err != nil { + return 0, err + } + return info.Size(), nil +} + func deleteOldBackup() { files, err := GetAllBackupFiles() if err != nil { @@ -152,6 +160,26 @@ func deleteOldBackup() { } } +func GetFileSizeFromUrl(url string) (int64, error) { + resp, err := http.Head(url) + if err != nil { + return 0, err + } + + // Is our request ok? + + if resp.StatusCode != http.StatusOK { + return 0, fmt.Errorf("Did not receive 200") + } + + size, err := strconv.Atoi(resp.Header.Get("Content-Length")) + if err != nil { + return 0, err + } + + return int64(size), nil +} + func CreateBackup() (string, error) { backupFileName := "podgrab_backup_" + time.Now().Format("2006.01.02_150405") + ".tar.gz" diff --git a/service/podcastService.go b/service/podcastService.go index c1c9778..b878f2a 100644 --- a/service/podcastService.go +++ b/service/podcastService.go @@ -239,7 +239,7 @@ func AddPodcastItems(podcast *db.Podcast, newPodcast bool) error { keyMap[item.GUID] = 1 } var latestDate = time.Time{} - + var itemsAdded = make(map[string]string) for i := 0; i < len(data.Channel.Item); i++ { obj := data.Channel.Item[i] var podcastItem db.PodcastItem @@ -302,14 +302,45 @@ func AddPodcastItems(podcast *db.Podcast, newPodcast bool) error { DownloadStatus: downloadStatus, } db.CreatePodcastItem(&podcastItem) + itemsAdded[podcastItem.ID] = podcastItem.FileURL } } if (latestDate != time.Time{}) { db.UpdateLastEpisodeDateForPodcast(podcast.ID, latestDate) } + //go updateSizeFromUrl(itemsAdded) return err } +func updateSizeFromUrl(itemUrlMap map[string]string) { + + for id, url := range itemUrlMap { + size, err := GetFileSizeFromUrl(url) + if err != nil { + size = 1 + } + + db.UpdatePodcastItemFileSize(id, size) + } + +} + +func UpdateAllFileSizes() { + items, err := db.GetAllPodcastItemsWithoutSize() + if err != nil { + return + } + for _, item := range *items { + var size int64 = 1 + if item.DownloadStatus == db.Downloaded { + size, _ = GetFileSize(item.DownloadPath) + } else { + size, _ = GetFileSizeFromUrl(item.FileURL) + } + db.UpdatePodcastItemFileSize(item.ID, size) + } +} + func SetPodcastItemAsQueuedForDownload(id string) error { var podcastItem db.PodcastItem err := db.GetPodcastItemById(id, &podcastItem) @@ -376,6 +407,12 @@ func SetPodcastItemAsDownloaded(id string, location string) error { fmt.Println("Location", err.Error()) return err } + + size, err := GetFileSize(location) + if err == nil { + podcastItem.FileSize = size + } + podcastItem.DownloadDate = time.Now() podcastItem.DownloadPath = location podcastItem.DownloadStatus = db.Downloaded