Extracted the process into a new function : getImagePath

This commit is contained in:
aditya-K2 2021-11-20 22:15:10 +05:30
parent b91120252b
commit e9bcb8ff63

View File

@ -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
}