Instead of Caching by maintaining a cache file as mentioned here https://github.com/aditya-K2/goMP/issues/14#issuecomment-989141798 We can directly check if the file exists if it exists then we can just pass the path to it else we can copy the default image to the path of the imagePath
33 lines
635 B
Go
33 lines
635 B
Go
package cache
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
var (
|
|
CACHE_DIR string
|
|
DEFAULT_IMG string
|
|
)
|
|
|
|
func SetCacheDir(path string) {
|
|
CACHE_DIR = path
|
|
}
|
|
|
|
func Exists(artist, album string) bool {
|
|
if _, err := os.Stat(GenerateName(artist, album)); errors.Is(err, os.ErrNotExist) {
|
|
return false
|
|
} else {
|
|
return true
|
|
}
|
|
}
|
|
|
|
func GenerateName(artist, album string) string {
|
|
if (artist == "" && album == "") || (artist == " " && album == " ") {
|
|
return CACHE_DIR + "UnknownArtist-UnknownAlbum.jpg"
|
|
}
|
|
return CACHE_DIR + strings.Replace(strings.Replace(fmt.Sprintf("%s-%s.jpg", artist, album), " ", "_", -1), "/", "_", -1)
|
|
}
|