/
cache.go
83 lines (66 loc) · 2.12 KB
/
cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* go-mapbox Maps Module Cache
* Provides a simple file system cache to avoid hitting the maps API quite so regularly
*
* https://github.com/ryankurte/go-mapbox
* Copyright 2017 Ryan Kurte
*/
package maps
import (
"fmt"
"image"
"os"
"strings"
)
// FileCache is a simple file-based caching implementation for map tiles
// This does not implement any mechanisms for deletion / removal, and as such is not suitable for production use
type FileCache struct {
basePath string
}
// NewFileCache creates a new file cache instance
func NewFileCache(basePath string) (*FileCache, error) {
fc := &FileCache{basePath}
err := os.Mkdir(basePath, 0777)
if err != nil && !os.IsExist(err) {
return nil, err
}
return fc, nil
}
func (fc *FileCache) getName(mapID MapID, x, y, level uint64, format MapFormat, highDPI bool) string {
dpiString := ""
if highDPI {
dpiString = "@2x"
}
return fmt.Sprintf("%s-%d-%d-%d%s.%s", mapID, x, y, level, dpiString, format)
}
// Save saves an image to the file cache
func (fc *FileCache) Save(mapID MapID, x, y, level uint64, format MapFormat, highDPI bool, img image.Image) error {
name := fc.getName(mapID, x, y, level, format, highDPI)
path := fmt.Sprintf("%s/%s", fc.basePath, name)
// Check if file already exists
if _, err := os.Stat(path); err == nil {
return nil
}
// TODO: cannot currently save pngraw
if strings.Contains(string(format), "png") {
return SaveImagePNG(img, path)
}
if strings.Contains(string(format), "jpg") || strings.Contains(string(format), "jpeg") {
return SaveImageJPG(img, path)
}
return fmt.Errorf("Unrecognized file type (%s)", format)
}
// Fetch fetches an image from the file cache if possible
func (fc *FileCache) Fetch(mapID MapID, x, y, level uint64, format MapFormat, highDPI bool) (image.Image, *image.Config, error) {
name := fc.getName(mapID, x, y, level, format, highDPI)
path := fmt.Sprintf("%s/%s", fc.basePath, name)
// TODO: work out how to load raw png files :-/
if format == MapFormatPngRaw {
return nil, nil, nil
}
if _, err := os.Stat(path); err != nil {
return nil, nil, nil
}
img, cfg, err := LoadImage(path)
return img, cfg, err
}