Merge branch 'master' into es6-modules

This commit is contained in:
NatureFreshMilk 2019-06-17 07:12:40 +02:00
commit c4f52ee923
3 changed files with 23 additions and 11 deletions

View File

@ -146,8 +146,12 @@ func Setup(p params.ParamsType, cfg *Config) *App {
//create media repo //create media repo
repo := make(map[string][]byte) repo := make(map[string][]byte)
media.ScanDir(repo, ".", []string{"mapserver.tiles", ".git"}) mediasize, _ := media.ScanDir(repo, ".", []string{"mapserver.tiles", ".git"})
logrus.WithFields(logrus.Fields{"count": len(repo)}).Info("Created media repository") fields := logrus.Fields{
"count": len(repo),
"bytes": mediasize,
}
logrus.WithFields(fields).Info("Created media repository")
a.MediaRepo = repo a.MediaRepo = repo

View File

@ -6,8 +6,9 @@ import (
"strings" "strings"
) )
func ScanDir(repo map[string][]byte, path string, ignore []string) error { func ScanDir(repo map[string][]byte, path string, ignore []string) (int, error) {
_, files := scan_recursive(path, ignore) _, files := scan_recursive(path, ignore)
size := 0
for _, filename := range files { for _, filename := range files {
if strings.HasSuffix(filename, ".png") { if strings.HasSuffix(filename, ".png") {
@ -15,15 +16,17 @@ func ScanDir(repo map[string][]byte, path string, ignore []string) error {
file, err := os.Open(filename) file, err := os.Open(filename)
if err != nil { if err != nil {
return err return 0, err
} }
content, err := ioutil.ReadAll(file) content, err := ioutil.ReadAll(file)
if err != nil { if err != nil {
return err return 0, err
} }
size += len(content)
simplefilename := filename simplefilename := filename
lastSlashIndex := strings.LastIndex(filename, "/") lastSlashIndex := strings.LastIndex(filename, "/")
@ -36,5 +39,5 @@ func ScanDir(repo map[string][]byte, path string, ignore []string) error {
} }
return nil return size, nil
} }

View File

@ -2,6 +2,7 @@ package web
import ( import (
"mapserver/app" "mapserver/app"
"mapserver/vfs"
"net/http" "net/http"
"strings" "strings"
) )
@ -20,16 +21,20 @@ func (h *MediaHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
} }
filename := parts[0] filename := parts[0]
fallback, hasfallback := req.URL.Query()["fallback"]
content := h.ctx.MediaRepo[filename] content := h.ctx.MediaRepo[filename]
if content == nil && hasfallback && len(fallback) > 0 {
content, _ = vfs.FSByte(h.ctx.Config.Webdev, "/pics/"+fallback[0])
}
if content != nil { if content != nil {
resp.Write(content) resp.Write(content)
resp.Header().Add("content-type", "image/png") resp.Header().Add("content-type", "image/png")
return
} else {
resp.WriteHeader(404)
resp.Write([]byte(filename))
} }
resp.WriteHeader(404)
resp.Write([]byte(filename))
} }