From 087e15210ee3a5806ed29658e5a02ccb9bef48bc Mon Sep 17 00:00:00 2001 From: Akhil Gupta Date: Sun, 21 Feb 2021 12:08:11 +0530 Subject: [PATCH] helper function to get images --- client/episodes.html | 2 +- controllers/podcast.go | 21 +++++++++++++++++++++ main.go | 1 + 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/client/episodes.html b/client/episodes.html index 4f72e7d..7411c8d 100644 --- a/client/episodes.html +++ b/client/episodes.html @@ -65,7 +65,7 @@ {{ .Title }} diff --git a/controllers/podcast.go b/controllers/podcast.go index 850ece4..59f747a 100644 --- a/controllers/podcast.go +++ b/controllers/podcast.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "net/http" + "os" "strings" "github.com/akhilrex/podgrab/model" @@ -191,6 +192,26 @@ func GetPodcastItemById(c *gin.Context) { } } +func GetPodcastItemImageById(c *gin.Context) { + var searchByIdQuery SearchByIdQuery + + if c.ShouldBindUri(&searchByIdQuery) == nil { + + var podcast db.PodcastItem + + err := db.GetPodcastItemById(searchByIdQuery.Id, &podcast) + if err == nil { + if _, err = os.Stat(podcast.LocalImage); os.IsNotExist(err) { + c.Redirect(301, podcast.Image) + } else { + c.Redirect(302, fmt.Sprintf("/%s", podcast.LocalImage)) + } + } + } else { + c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"}) + } +} + func MarkPodcastItemAsUnplayed(c *gin.Context) { var searchByIdQuery SearchByIdQuery diff --git a/main.go b/main.go index c2e7de6..54b5489 100644 --- a/main.go +++ b/main.go @@ -115,6 +115,7 @@ func main() { router.GET("/podcastitems", controllers.GetAllPodcastItems) router.GET("/podcastitems/:id", controllers.GetPodcastItemById) + router.GET("/podcastitems/:id/image", controllers.GetPodcastItemImageById) router.GET("/podcastitems/:id/markUnplayed", controllers.MarkPodcastItemAsUnplayed) router.GET("/podcastitems/:id/markPlayed", controllers.MarkPodcastItemAsPlayed) router.GET("/podcastitems/:id/bookmark", controllers.BookmarkPodcastItem)