This repository has been archived by the owner on Oct 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
simpleFileCache.go
72 lines (68 loc) · 1.57 KB
/
simpleFileCache.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
package main
import (
"bytes"
"image"
"image/png"
"io/fs"
"log"
"os"
"path"
mapsdatabase "github.com/maxsupermanhd/go-wz/maps-database"
)
func mapsdbGetTerrain(hash string) (image.Image, error) {
p := path.Join(cfg.GetDString("./mapcache/terrain/", "cache", "terrain"), hash+".png")
b, err := os.ReadFile(p)
if err == nil {
return png.Decode(bytes.NewBuffer(b))
}
if _, ok := err.(*fs.PathError); !ok {
return nil, err
}
log.Printf("Fetching terrain image %v", hash)
out, err := mapsdatabase.FetchMapTerrain(hash)
if err != nil {
return nil, err
}
ob := bytes.NewBufferString("")
err = png.Encode(ob, out)
if err != nil {
return nil, err
}
dperm := fs.FileMode(cfg.GetDInt(493, "dirPerms"))
err = os.MkdirAll(path.Dir(p), dperm)
if err != nil {
return nil, err
}
fperm := fs.FileMode(cfg.GetDInt(420, "filePerms"))
err = os.WriteFile(p, ob.Bytes(), fperm)
if err != nil {
return nil, err
}
return out, err
}
func mapsdbGetBlob(hash string) ([]byte, error) {
p := path.Join(cfg.GetDString("./mapcache/blob/", "cache", "blob"), hash+".wz")
b, err := os.ReadFile(p)
if err == nil {
return b, nil
}
if _, ok := err.(*fs.PathError); !ok {
return nil, err
}
log.Printf("Fetching map blob %v", hash)
out, err := mapsdatabase.FetchMapBlob(hash)
if err != nil {
return nil, err
}
dperm := fs.FileMode(cfg.GetDInt(493, "dirPerms"))
err = os.MkdirAll(path.Dir(p), dperm)
if err != nil {
return nil, err
}
fperm := fs.FileMode(cfg.GetDInt(420, "filePerms"))
err = os.WriteFile(p, out, fperm)
if err != nil {
return nil, err
}
return out, err
}