Moving imageUtils.go to utils package

This commit is contained in:
aditya-K2 2021-12-13 01:43:20 +05:30
parent 9b60cbc98e
commit da47c9b10c
2 changed files with 30 additions and 30 deletions

View File

@ -1,8 +1,12 @@
package main package main
import ( import (
"image"
"os"
"github.com/aditya-K2/goMP/cache" "github.com/aditya-K2/goMP/cache"
"github.com/aditya-K2/goMP/utils" "github.com/aditya-K2/goMP/utils"
"github.com/nfnt/resize"
"github.com/spf13/viper" "github.com/spf13/viper"
"gitlab.com/diamondburned/ueberzug-go" "gitlab.com/diamondburned/ueberzug-go"
) )
@ -46,7 +50,7 @@ func openImage(path string, c chan string) {
var im *ueberzug.Image var im *ueberzug.Image
if path != "stop" { if path != "stop" {
extractedImage := getImagePath(path) extractedImage := getImagePath(path)
img2, _ := getImg(extractedImage) 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")) im, _ = ueberzug.NewImage(img2, int(float32(IMG_X)*fw)+viper.GetInt("ADDITIONAL_PADDING_X"), int(float32(IMG_Y)*fh)+viper.GetInt("ADDITIONAL_PADDING_Y"))
} }
d := <-c d := <-c
@ -81,7 +85,7 @@ func getImagePath(path string) string {
} else { } else {
imagePath := cache.GenerateName(a[0]["artist"], a[0]["album"]) imagePath := cache.GenerateName(a[0]["artist"], a[0]["album"])
absPath := utils.CheckDirectoryFmt(viper.GetString("MUSIC_DIRECTORY")) + path absPath := utils.CheckDirectoryFmt(viper.GetString("MUSIC_DIRECTORY")) + path
extractedImage = extractImageFromFile(absPath, imagePath) extractedImage = utils.ExtractImageFromFile(absPath, imagePath)
if extractedImage == viper.GetString("DEFAULT_IMAGE_PATH") && viper.GetString("GET_COVER_ART_FROM_LAST_FM") == "TRUE" { 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) downloadedImage, err := getImageFromLastFM(a[0]["artist"], a[0]["album"], imagePath)
if err == nil { if err == nil {
@ -97,3 +101,22 @@ func getImagePath(path string) string {
} }
return extractedImage return extractedImage
} }
func GetImg(uri string) (image.Image, error) {
f, err := os.Open(uri)
if err != nil {
return nil, err
}
defer f.Close()
img, _, err := image.Decode(f)
if err != nil {
return nil, err
}
fw, fh := utils.GetFontWidth()
img = resize.Resize(
uint(float32(IMG_W)*(fw+float32(viper.GetFloat64("IMAGE_WIDTH_EXTRA_X")))), uint(float32(IMG_H)*(fh+float32(viper.GetFloat64("IMAGE_WIDTH_EXTRA_Y")))),
img,
resize.Bilinear,
)
return img, nil
}

View File

@ -1,16 +1,12 @@
package main package utils
import ( import (
"image"
"os" "os"
"strings" "strings"
"github.com/aditya-K2/goMP/utils"
"github.com/bogem/id3v2" "github.com/bogem/id3v2"
"github.com/mewkiz/flac" "github.com/mewkiz/flac"
"github.com/mewkiz/flac/meta" "github.com/mewkiz/flac/meta"
"github.com/nfnt/resize"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -66,42 +62,23 @@ func GetFlacImage(songPath, imagePath string) string {
return "" return ""
} }
func extractImageFromFile(uri string, imagePath string) string { func ExtractImageFromFile(uri string, imagePath string) string {
_i := imagePath _i := imagePath
if strings.HasSuffix(uri, ".mp3") { if strings.HasSuffix(uri, ".mp3") {
imagePath := GetMp3Image(uri, imagePath) imagePath := GetMp3Image(uri, imagePath)
if imagePath == "" { if imagePath == "" {
utils.Copy(viper.GetString("DEFAULT_IMAGE_PATH"), _i) Copy(viper.GetString("DEFAULT_IMAGE_PATH"), _i)
return viper.GetString("DEFAULT_IMAGE_PATH") return viper.GetString("DEFAULT_IMAGE_PATH")
} }
} else if strings.HasSuffix(uri, ".flac") { } else if strings.HasSuffix(uri, ".flac") {
imagePath := GetFlacImage(uri, imagePath) imagePath := GetFlacImage(uri, imagePath)
if imagePath == "" { if imagePath == "" {
utils.Copy(viper.GetString("DEFAULT_IMAGE_PATH"), _i) Copy(viper.GetString("DEFAULT_IMAGE_PATH"), _i)
return viper.GetString("DEFAULT_IMAGE_PATH") return viper.GetString("DEFAULT_IMAGE_PATH")
} }
} else { } else {
utils.Copy(viper.GetString("DEFAULT_IMAGE_PATH"), _i) Copy(viper.GetString("DEFAULT_IMAGE_PATH"), _i)
return viper.GetString("DEFAULT_IMAGE_PATH") return viper.GetString("DEFAULT_IMAGE_PATH")
} }
return imagePath return imagePath
} }
func getImg(uri string) (image.Image, error) {
f, err := os.Open(uri)
if err != nil {
return nil, err
}
defer f.Close()
img, _, err := image.Decode(f)
if err != nil {
return nil, err
}
fw, fh := utils.GetFontWidth()
img = resize.Resize(
uint(float32(IMG_W)*(fw+float32(viper.GetFloat64("IMAGE_WIDTH_EXTRA_X")))), uint(float32(IMG_H)*(fh+float32(viper.GetFloat64("IMAGE_WIDTH_EXTRA_Y")))),
img,
resize.Bilinear,
)
return img, nil
}