2021-12-22 08:49:37 -07:00
|
|
|
package render
|
2021-10-24 01:45:45 -06:00
|
|
|
|
|
|
|
import (
|
2021-12-22 08:49:37 -07:00
|
|
|
"github.com/aditya-K2/gomp/utils"
|
2021-10-24 01:45:45 -06:00
|
|
|
"os"
|
2021-11-07 23:18:49 -07:00
|
|
|
"strings"
|
2021-10-24 01:45:45 -06:00
|
|
|
|
2021-11-07 23:18:49 -07:00
|
|
|
"github.com/bogem/id3v2"
|
|
|
|
"github.com/mewkiz/flac"
|
|
|
|
"github.com/mewkiz/flac/meta"
|
2021-11-05 01:10:46 -06:00
|
|
|
"github.com/spf13/viper"
|
2021-10-24 01:45:45 -06:00
|
|
|
)
|
|
|
|
|
2021-11-07 23:18:49 -07:00
|
|
|
func GetMp3Image(songPath, imagePath string) string {
|
|
|
|
tag, err := id3v2.Open(songPath, id3v2.Options{Parse: true})
|
2021-10-24 01:45:45 -06:00
|
|
|
if err != nil {
|
2021-11-07 23:18:49 -07:00
|
|
|
return ""
|
2021-10-24 01:45:45 -06:00
|
|
|
}
|
2021-11-07 23:18:49 -07:00
|
|
|
defer tag.Close()
|
2021-10-24 01:45:45 -06:00
|
|
|
if err != nil {
|
2021-11-07 23:18:49 -07:00
|
|
|
return ""
|
2021-10-24 01:45:45 -06:00
|
|
|
}
|
2021-11-07 23:18:49 -07:00
|
|
|
// Read tags.
|
|
|
|
Frames := tag.GetFrames(tag.CommonID("Attached picture"))
|
|
|
|
var ImageData []byte
|
|
|
|
for _, er := range Frames {
|
|
|
|
pic, ok := er.(id3v2.PictureFrame)
|
|
|
|
if ok {
|
|
|
|
for _, i := range pic.Picture {
|
|
|
|
ImageData = append(ImageData, byte(i))
|
|
|
|
}
|
|
|
|
imageHandler, err := os.Create(imagePath)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
} else {
|
|
|
|
imageHandler.Write(ImageData)
|
|
|
|
return imagePath
|
|
|
|
}
|
2021-10-24 01:45:45 -06:00
|
|
|
}
|
|
|
|
}
|
2021-11-07 23:18:49 -07:00
|
|
|
return ""
|
2021-10-24 01:45:45 -06:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:18:49 -07:00
|
|
|
func GetFlacImage(songPath, imagePath string) string {
|
|
|
|
stream, err := flac.ParseFile(songPath)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
defer stream.Close()
|
|
|
|
for _, block := range stream.Blocks {
|
|
|
|
if block.Type == meta.TypePicture {
|
|
|
|
pic := block.Body.(*meta.Picture)
|
|
|
|
if pic.Type == 3 {
|
|
|
|
imageHandler, err := os.Create(imagePath)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
imageHandler.Write(pic.Data)
|
|
|
|
return imagePath
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2021-12-12 13:13:20 -07:00
|
|
|
func ExtractImageFromFile(uri string, imagePath string) string {
|
2021-12-08 13:59:53 -07:00
|
|
|
_i := imagePath
|
2021-11-07 23:18:49 -07:00
|
|
|
if strings.HasSuffix(uri, ".mp3") {
|
2021-11-20 09:15:32 -07:00
|
|
|
imagePath := GetMp3Image(uri, imagePath)
|
2021-11-07 23:18:49 -07:00
|
|
|
if imagePath == "" {
|
2021-12-22 08:49:37 -07:00
|
|
|
utils.Copy(viper.GetString("DEFAULT_IMAGE_PATH"), _i)
|
2021-11-07 23:18:49 -07:00
|
|
|
return viper.GetString("DEFAULT_IMAGE_PATH")
|
|
|
|
}
|
|
|
|
} else if strings.HasSuffix(uri, ".flac") {
|
2021-11-20 09:15:32 -07:00
|
|
|
imagePath := GetFlacImage(uri, imagePath)
|
2021-11-07 23:18:49 -07:00
|
|
|
if imagePath == "" {
|
2021-12-22 08:49:37 -07:00
|
|
|
utils.Copy(viper.GetString("DEFAULT_IMAGE_PATH"), _i)
|
2021-11-07 23:18:49 -07:00
|
|
|
return viper.GetString("DEFAULT_IMAGE_PATH")
|
|
|
|
}
|
|
|
|
} else {
|
2021-12-22 08:49:37 -07:00
|
|
|
utils.Copy(viper.GetString("DEFAULT_IMAGE_PATH"), _i)
|
2021-11-07 23:18:49 -07:00
|
|
|
return viper.GetString("DEFAULT_IMAGE_PATH")
|
|
|
|
}
|
2021-12-08 13:59:53 -07:00
|
|
|
return imagePath
|
2021-11-07 23:18:49 -07:00
|
|
|
}
|