2017-06-27 20:49:53 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/golang/geo/s2"
|
|
|
|
)
|
|
|
|
|
2018-04-03 19:20:34 +00:00
|
|
|
func filesystemCache(center s2.LatLng, zoom int, marker []marker, x, y int, disableAttribution bool) (io.ReadCloser, error) {
|
|
|
|
cacheKey := cacheKeyHelper(center, zoom, marker, x, y, disableAttribution)
|
2017-06-27 20:49:53 +00:00
|
|
|
cacheFileName := path.Join(cfg.CacheDir, cacheKey[0:2], cacheKey+".png")
|
|
|
|
|
|
|
|
if info, err := os.Stat(cacheFileName); err == nil && info.ModTime().Add(cfg.ForceCache).After(time.Now()) {
|
|
|
|
return os.Open(cacheFileName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// No cache hit, generate a new map
|
2018-04-03 19:20:34 +00:00
|
|
|
mapReader, err := generateMap(center, zoom, marker, x, y, disableAttribution)
|
2017-06-27 20:49:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
2017-06-27 22:59:06 +00:00
|
|
|
if _, err = io.Copy(buf, mapReader); err != nil {
|
2017-06-27 20:49:53 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-06-27 22:59:06 +00:00
|
|
|
if err = os.MkdirAll(path.Dir(cacheFileName), 0700); err != nil {
|
2017-06-27 20:49:53 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-06-27 22:59:06 +00:00
|
|
|
if err = ioutil.WriteFile(cacheFileName, buf.Bytes(), 0644); err != nil {
|
2017-06-27 20:49:53 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ioutil.NopCloser(buf), err
|
|
|
|
}
|