From 84c4b4313c6768edf6c8f4b4fe4c94888f3554f8 Mon Sep 17 00:00:00 2001 From: aditya-K2 Date: Sat, 20 Nov 2021 21:45:32 +0530 Subject: [PATCH] Now it is neccessary to pass the imagePath Now it is neccessary to pass imagePath to the image Extracting functions. --- imageUtils.go | 6 +++--- lastfm.go | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/imageUtils.go b/imageUtils.go index 7172c5e..86f0fd6 100644 --- a/imageUtils.go +++ b/imageUtils.go @@ -64,16 +64,16 @@ func GetFlacImage(songPath, imagePath string) string { return "" } -func extractImageFromFile(uri string) string { +func extractImageFromFile(uri string, imagePath string) string { if strings.HasSuffix(uri, ".mp3") { - imagePath := GetMp3Image(uri, viper.GetString("COVER_IMAGE_PATH")) + imagePath := GetMp3Image(uri, imagePath) if imagePath == "" { return viper.GetString("DEFAULT_IMAGE_PATH") } else { return imagePath } } else if strings.HasSuffix(uri, ".flac") { - imagePath := GetFlacImage(uri, viper.GetString("COVER_IMAGE_PATH")) + imagePath := GetFlacImage(uri, imagePath) if imagePath == "" { return viper.GetString("DEFAULT_IMAGE_PATH") } else { diff --git a/lastfm.go b/lastfm.go index a673101..c1e9b86 100644 --- a/lastfm.go +++ b/lastfm.go @@ -12,7 +12,7 @@ import ( "github.com/spf13/viper" ) -func getImageFromLastFM(artist, album string) (string, error) { +func getImageFromLastFM(artist, album, imagePath string) (string, error) { api := lastfm.New(viper.GetString("LASTFM_API_KEY"), viper.GetString("LASTFM_API_SECRET")) v, err := api.Album.GetInfo(map[string]interface{}{ "artist": artist, @@ -22,11 +22,11 @@ func getImageFromLastFM(artist, album string) (string, error) { if err != nil { return "", err } else { - return downloadImage(v.Images[len(v.Images)-1].Url) + return downloadImage(v.Images[len(v.Images)-1].Url, imagePath) } } -func downloadImage(url string) (string, error) { +func downloadImage(url string, imagePath string) (string, error) { var reader io.Reader if strings.HasPrefix(url, "http") { r, err := http.Get(url) @@ -37,10 +37,10 @@ func downloadImage(url string) (string, error) { reader = r.Body v, err := io.ReadAll(reader) if err == nil { - b, err := os.Create(viper.GetString("COVER_IMAGE_PATH")) + b, err := os.Create(imagePath) if err == nil { b.Write(v) - return viper.GetString("COVER_IMAGE_PATH"), nil + return imagePath, nil } else { b.Close() return "", err