From e9bcb8ff63832b02fab972ceb8d5f620c6369d12 Mon Sep 17 00:00:00 2001 From: aditya-K2 Date: Sat, 20 Nov 2021 22:15:10 +0530 Subject: [PATCH] Extracted the process into a new function : getImagePath --- render.go | 45 +++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/render.go b/render.go index 448c6ae..41ed3e9 100644 --- a/render.go +++ b/render.go @@ -44,24 +44,7 @@ func openImage(path string, c chan string) { fw, fh := getFontWidth() var im *ueberzug.Image if path != "stop" { - a, err := CONN.ListInfo(path) - var extractedImage string - if err == nil && len(a) != 0 { - if val, err := cache.GetFromCache(a[0]["artist"], a[0]["album"]); err == nil { - extractedImage = val - } else { - imagePath := cache.AddToCache(a[0]["artist"], a[0]["album"]) - absPath := viper.GetString("MUSIC_DIRECTORY") + path - extractedImage = extractImageFromFile(absPath, imagePath) - if extractedImage == viper.GetString("DEFAULT_IMAGE_PATH") && viper.GetString("GET_COVER_ART_FROM_LAST_FM") == "TRUE" { - downloadedImage, err := getImageFromLastFM(a[0]["artist"], a[0]["album"], imagePath) - if err == nil { - NOTIFICATION_SERVER.Send("Image From LastFM") - extractedImage = downloadedImage - } - } - } - } + extractedImage := getImagePath(path) img2, _ := getImg(extractedImage) im, _ = ueberzug.NewImage(img2, int(float32(IMG_X)*fw)+viper.GetInt("ADDITIONAL_PADDING_X"), int(float32(IMG_Y)*fh)+viper.GetInt("ADDITIONAL_PADDING_Y")) } @@ -83,3 +66,29 @@ func openImage(path string, c chan string) { func (self *Renderer) Start(path string) { go openImage(path, self.c) } + +/* + This Function returns the path to the image that is to be rendered it checks first for the image in the cache + else it adds the image to the cache and then extracts it and renders it. +*/ +func getImagePath(path string) string { + a, err := CONN.ListInfo(path) + var extractedImage string + if err == nil && len(a) != 0 { + if val, err := cache.GetFromCache(a[0]["artist"], a[0]["album"]); err == nil { + extractedImage = val + } else { + imagePath := cache.AddToCache(a[0]["artist"], a[0]["album"]) + absPath := viper.GetString("MUSIC_DIRECTORY") + path + extractedImage = extractImageFromFile(absPath, imagePath) + if extractedImage == viper.GetString("DEFAULT_IMAGE_PATH") && viper.GetString("GET_COVER_ART_FROM_LAST_FM") == "TRUE" { + downloadedImage, err := getImageFromLastFM(a[0]["artist"], a[0]["album"], imagePath) + if err == nil { + NOTIFICATION_SERVER.Send("Image From LastFM") + extractedImage = downloadedImage + } + } + } + } + return extractedImage +}