cache podcast main image as well

pull/114/head
Akhil Gupta 4 years ago
parent 7168b85e1d
commit 2367c1f7fa

@ -166,7 +166,8 @@
<img
onerror="onImageError(this)"
class="u-full-width"
v-bind:src="podcast.Image"
:src="getPodcastImage(podcast)"
v-bind:alt="podcast.Title"
/>
</a>
@ -455,6 +456,9 @@
},
methods:{
getPodcastImage(item){
return "/podcasts/"+item.ID+"/image"
},
removePodcast(id) {
const index= this.podcasts.findIndex(x=>x.ID===id);
this.podcasts.splice(index,1);

@ -225,6 +225,27 @@ func GetPodcastItemImageById(c *gin.Context) {
}
}
func GetPodcastImageById(c *gin.Context) {
var searchByIdQuery SearchByIdQuery
if c.ShouldBindUri(&searchByIdQuery) == nil {
var podcast db.Podcast
err := db.GetPodcastById(searchByIdQuery.Id, &podcast)
if err == nil {
localPath := service.GetPodcastLocalImagePath(podcast.Image, podcast.Title)
if _, err = os.Stat(localPath); os.IsNotExist(err) {
c.Redirect(301, podcast.Image)
} else {
c.File(localPath)
}
}
} else {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
}
}
func GetPodcastItemFileById(c *gin.Context) {
var searchByIdQuery SearchByIdQuery

@ -147,6 +147,7 @@ func main() {
router.POST("/podcasts", controllers.AddPodcast)
router.GET("/podcasts", controllers.GetAllPodcasts)
router.GET("/podcasts/:id", controllers.GetPodcastById)
router.GET("/podcasts/:id/image", controllers.GetPodcastImageById)
router.DELETE("/podcasts/:id", controllers.DeletePodcastById)
router.GET("/podcasts/:id/items", controllers.GetPodcastItemsByPodcastId)
router.GET("/podcasts/:id/download", controllers.DownloadAllEpisodesByPodcastId)

@ -60,6 +60,51 @@ func Download(link string, episodeTitle string, podcastName string, prefix strin
}
func GetPodcastLocalImagePath(link string, podcastName string) string {
fileName := getFileName(link, "folder", ".jpg")
folder := createDataFolderIfNotExists(podcastName)
finalPath := path.Join(folder, fileName)
return finalPath
}
func DownloadPodcastCoverImage(link string, podcastName string) (string, error) {
if link == "" {
return "", errors.New("Download path empty")
}
client := httpClient()
resp, err := client.Get(link)
if err != nil {
Logger.Errorw("Error getting response: "+link, err)
return "", err
}
fileName := getFileName(link, "folder", ".jpg")
folder := createDataFolderIfNotExists(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)
return "", err
}
defer resp.Body.Close()
_, erra := io.Copy(file, resp.Body)
//fmt.Println(size)
defer file.Close()
if erra != nil {
Logger.Errorw("Error saving file"+link, err)
return "", erra
}
changeOwnership(finalPath)
return finalPath, nil
}
func DownloadImage(link string, episodeId string, podcastName string) (string, error) {
if link == "" {
return "", errors.New("Download path empty")

@ -198,7 +198,7 @@ func getItunesImageUrl(body []byte) string {
func AddPodcast(url string) (db.Podcast, error) {
var podcast db.Podcast
err := db.GetPodcastByURL(url, &podcast)
fmt.Println(url)
if errors.Is(err, gorm.ErrRecordNotFound) {
data, body, err := FetchURL(url)
if err != nil {
@ -220,8 +220,10 @@ func AddPodcast(url string) (db.Podcast, error) {
}
err = db.CreatePodcast(&podcast)
go DownloadPodcastCoverImage(podcast.Image, podcast.Title)
return podcast, err
}
return podcast, &model.PodcastAlreadyExistsError{Url: url}
}

Loading…
Cancel
Save