New : New cahce
(path, cacheURL string, caching bool)
| 31 | |
| 32 | // New : New cahce |
| 33 | func New(path, cacheURL string, caching bool) (c *Cache, err error) { |
| 34 | |
| 35 | c = &Cache{} |
| 36 | |
| 37 | c.images = make(map[string]string) |
| 38 | c.path = path |
| 39 | c.cacheURL = cacheURL |
| 40 | c.caching = caching |
| 41 | c.Queue = []string{} |
| 42 | c.Cache = []string{} |
| 43 | |
| 44 | var queue []string |
| 45 | |
| 46 | c.Image.GetURL = func(src string, http_domain string, http_port string, force_https bool, https_port int, https_domain string) (cacheURL string) { |
| 47 | |
| 48 | c.Lock() |
| 49 | defer c.Unlock() |
| 50 | |
| 51 | src = strings.Trim(src, "\r\n") |
| 52 | |
| 53 | if !c.caching { |
| 54 | return src |
| 55 | } |
| 56 | |
| 57 | u, err := url.Parse(src) |
| 58 | |
| 59 | if err != nil || len(filepath.Ext(u.Path)) == 0 { |
| 60 | return src |
| 61 | } |
| 62 | |
| 63 | src_filtered := strings.Split(src, "?") |
| 64 | var filename = fmt.Sprintf("%s%s", strToMD5(src_filtered[0]), filepath.Ext(u.Path)) |
| 65 | |
| 66 | if cacheURL, ok := c.images[filename]; ok { |
| 67 | if c.caching && force_https { |
| 68 | u, err := url.Parse(cacheURL) |
| 69 | if err == nil { |
| 70 | cacheURL = fmt.Sprintf("https://%s:%d%s", https_domain, https_port, u.Path) |
| 71 | } |
| 72 | } else if c.caching && http_domain != "" { |
| 73 | u, err := url.Parse(cacheURL) |
| 74 | if err == nil { |
| 75 | var baseUrl = "" |
| 76 | if strings.Contains(http_domain, ":") { |
| 77 | baseUrl = http_domain |
| 78 | } else { |
| 79 | baseUrl = fmt.Sprintf("%s:%s", http_domain, http_port) |
| 80 | } |
| 81 | cacheURL = fmt.Sprintf("http://%s%s", baseUrl, u.Path) |
| 82 | } |
| 83 | } |
| 84 | return cacheURL |
| 85 | } |
| 86 | |
| 87 | if indexOfString(filename, c.Cache) == -1 { |
| 88 | if indexOfString(src, c.Queue) == -1 { |
| 89 | c.Queue = append(c.Queue, src) |
| 90 | } |
no test coverage detected