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)