gomp/cache/cache.go
aditya-K2 4d55f233d0 Better Way of Caching
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
2021-12-09 02:25:46 +05:30

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)
}