Now it is neccessary to pass the imagePath

Now it is neccessary to pass imagePath to the image Extracting
functions.
This commit is contained in:
aditya-K2 2021-11-20 21:45:32 +05:30
parent 1014981dcb
commit 84c4b4313c
2 changed files with 8 additions and 8 deletions

View File

@ -64,16 +64,16 @@ func GetFlacImage(songPath, imagePath string) string {
return "" return ""
} }
func extractImageFromFile(uri string) string { func extractImageFromFile(uri string, imagePath string) string {
if strings.HasSuffix(uri, ".mp3") { if strings.HasSuffix(uri, ".mp3") {
imagePath := GetMp3Image(uri, viper.GetString("COVER_IMAGE_PATH")) imagePath := GetMp3Image(uri, imagePath)
if imagePath == "" { if imagePath == "" {
return viper.GetString("DEFAULT_IMAGE_PATH") return viper.GetString("DEFAULT_IMAGE_PATH")
} else { } else {
return imagePath return imagePath
} }
} else if strings.HasSuffix(uri, ".flac") { } else if strings.HasSuffix(uri, ".flac") {
imagePath := GetFlacImage(uri, viper.GetString("COVER_IMAGE_PATH")) imagePath := GetFlacImage(uri, imagePath)
if imagePath == "" { if imagePath == "" {
return viper.GetString("DEFAULT_IMAGE_PATH") return viper.GetString("DEFAULT_IMAGE_PATH")
} else { } else {

View File

@ -12,7 +12,7 @@ import (
"github.com/spf13/viper" "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")) api := lastfm.New(viper.GetString("LASTFM_API_KEY"), viper.GetString("LASTFM_API_SECRET"))
v, err := api.Album.GetInfo(map[string]interface{}{ v, err := api.Album.GetInfo(map[string]interface{}{
"artist": artist, "artist": artist,
@ -22,11 +22,11 @@ func getImageFromLastFM(artist, album string) (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} else { } 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 var reader io.Reader
if strings.HasPrefix(url, "http") { if strings.HasPrefix(url, "http") {
r, err := http.Get(url) r, err := http.Get(url)
@ -37,10 +37,10 @@ func downloadImage(url string) (string, error) {
reader = r.Body reader = r.Body
v, err := io.ReadAll(reader) v, err := io.ReadAll(reader)
if err == nil { if err == nil {
b, err := os.Create(viper.GetString("COVER_IMAGE_PATH")) b, err := os.Create(imagePath)
if err == nil { if err == nil {
b.Write(v) b.Write(v)
return viper.GetString("COVER_IMAGE_PATH"), nil return imagePath, nil
} else { } else {
b.Close() b.Close()
return "", err return "", err