2021-11-13 02:07:07 -07:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2021-11-20 07:51:54 -07:00
|
|
|
"errors"
|
2021-11-13 02:07:07 -07:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-11-20 09:08:38 -07:00
|
|
|
USER_CACHE_DIR, err = os.UserCacheDir()
|
|
|
|
CACHE_LIST map[[2]string]string = make(map[[2]string]string)
|
|
|
|
CACHE_DIR string = USER_CACHE_DIR
|
2021-11-26 09:29:50 -07:00
|
|
|
DEFAULT_IMG string
|
2021-11-13 02:07:07 -07:00
|
|
|
)
|
|
|
|
|
2021-11-20 09:08:38 -07:00
|
|
|
func SetCacheDir(path string) {
|
|
|
|
CACHE_DIR = path
|
|
|
|
}
|
|
|
|
|
2021-11-26 09:29:50 -07:00
|
|
|
func SetDefaultPath(path string) {
|
|
|
|
DEFAULT_IMG = path
|
|
|
|
}
|
|
|
|
|
2021-11-20 07:51:54 -07:00
|
|
|
func LoadCache(path string) error {
|
2021-11-13 02:07:07 -07:00
|
|
|
cacheFileContent, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
2021-11-20 07:51:54 -07:00
|
|
|
return errors.New("Could Not Read From Cache File")
|
2021-11-13 02:07:07 -07:00
|
|
|
}
|
|
|
|
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]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-20 07:51:54 -07:00
|
|
|
return nil
|
2021-11-13 02:07:07 -07:00
|
|
|
}
|
|
|
|
|
2021-11-20 07:51:54 -07:00
|
|
|
func GetFromCache(artist, album string) (string, error) {
|
|
|
|
if val, ok := CACHE_LIST[[2]string{artist, album}]; ok {
|
|
|
|
return val, nil
|
|
|
|
} else {
|
|
|
|
return "", errors.New("Element Not In Cache")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-26 09:29:50 -07:00
|
|
|
func PointToDefault(artist, album string) {
|
|
|
|
CACHE_LIST[[2]string{artist, album}] = DEFAULT_IMG
|
|
|
|
}
|
|
|
|
|
2021-11-20 09:08:38 -07:00
|
|
|
func AddToCache(artist, album string) string {
|
|
|
|
fileName := CACHE_DIR + GenerateName(artist, album)
|
|
|
|
CACHE_LIST[[2]string{artist, album}] = fileName
|
|
|
|
return fileName
|
2021-11-20 07:51:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func WriteCache(path string) {
|
2021-11-13 02:07:07 -07:00
|
|
|
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)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-13 02:17:26 -07:00
|
|
|
func GenerateName(artist, album string) string {
|
2021-11-25 07:38:51 -07:00
|
|
|
return strings.Replace(strings.Replace(fmt.Sprintf("%s-%s.jpg", artist, album), " ", "_", -1), "/", "_", -1)
|
2021-11-13 02:07:07 -07:00
|
|
|
}
|