1
0
Fork 0
mirror of https://github.com/Luzifer/staticmap.git synced 2024-12-30 01:31:18 +00:00
staticmap/cache_filesystem.go
Knut Ahlers cfc21d4049
Allow disabling of attribution rendering
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2018-04-03 21:20:34 +02:00

42 lines
1,005 B
Go

package main
import (
"bytes"
"io"
"io/ioutil"
"os"
"path"
"time"
"github.com/golang/geo/s2"
)
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)
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
mapReader, err := generateMap(center, zoom, marker, x, y, disableAttribution)
if err != nil {
return nil, err
}
buf := new(bytes.Buffer)
if _, err = io.Copy(buf, mapReader); err != nil {
return nil, err
}
if err = os.MkdirAll(path.Dir(cacheFileName), 0700); err != nil {
return nil, err
}
if err = ioutil.WriteFile(cacheFileName, buf.Bytes(), 0644); err != nil {
return nil, err
}
return ioutil.NopCloser(buf), err
}