diff --git a/cache/cache.go b/cache/cache.go index 80eba89..cb9228f 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -3,70 +3,30 @@ package cache import ( "errors" "fmt" - "io/ioutil" "os" "strings" ) var ( - USER_CACHE_DIR, err = os.UserCacheDir() - CACHE_LIST map[[2]string]string = make(map[[2]string]string) - CACHE_DIR string = USER_CACHE_DIR - DEFAULT_IMG string + CACHE_DIR string + DEFAULT_IMG string ) func SetCacheDir(path string) { CACHE_DIR = path } -func SetDefaultPath(path string) { - DEFAULT_IMG = path -} - -func LoadCache(path string) error { - cacheFileContent, err := ioutil.ReadFile(path) - if err != nil { - return errors.New("Could Not Read From Cache File") - } - lineSlice := strings.Split(string(cacheFileContent), "\n") - for _, line := range lineSlice { - if len(line) != 0 { - param := strings.Split(line, "\t") - if len(param) == 3 { - CACHE_LIST[[2]string{param[0], param[1]}] = param[2] - } - } - } - return nil -} - -func GetFromCache(artist, album string) (string, error) { - if val, ok := CACHE_LIST[[2]string{artist, album}]; ok { - return val, nil +func Exists(artist, album string) bool { + if _, err := os.Stat(GenerateName(artist, album)); errors.Is(err, os.ErrNotExist) { + return false } else { - return "", errors.New("Element Not In Cache") - } -} - -func PointToDefault(artist, album string) { - CACHE_LIST[[2]string{artist, album}] = DEFAULT_IMG -} - -func AddToCache(artist, album string) string { - fileName := CACHE_DIR + GenerateName(artist, album) - CACHE_LIST[[2]string{artist, album}] = fileName - return fileName -} - -func WriteCache(path string) { - b, err := os.Create(path) - if err == nil { - for k, v := range CACHE_LIST { - b.Write([]byte(fmt.Sprintf("%s\t%s\t%s\n", k[0], k[1], v))) - } + return true } } func GenerateName(artist, album string) string { - return strings.Replace(strings.Replace(fmt.Sprintf("%s-%s.jpg", artist, album), " ", "_", -1), "/", "_", -1) + 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) }