diff --git a/app/setup.go b/app/setup.go index 1f6652f..1138fc6 100644 --- a/app/setup.go +++ b/app/setup.go @@ -146,8 +146,12 @@ func Setup(p params.ParamsType, cfg *Config) *App { //create media repo repo := make(map[string][]byte) - media.ScanDir(repo, ".", []string{"mapserver.tiles", ".git"}) - logrus.WithFields(logrus.Fields{"count": len(repo)}).Info("Created media repository") + mediasize, _ := media.ScanDir(repo, ".", []string{"mapserver.tiles", ".git"}) + fields := logrus.Fields{ + "count": len(repo), + "bytes": mediasize, + } + logrus.WithFields(fields).Info("Created media repository") a.MediaRepo = repo diff --git a/media/repository.go b/media/repository.go index 8911afb..30ae765 100644 --- a/media/repository.go +++ b/media/repository.go @@ -6,8 +6,9 @@ import ( "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) + size := 0 for _, filename := range files { 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) if err != nil { - return err + return 0, err } content, err := ioutil.ReadAll(file) if err != nil { - return err + return 0, err } + size += len(content) + simplefilename := 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 } diff --git a/web/media.go b/web/media.go index 849aaac..a19785d 100644 --- a/web/media.go +++ b/web/media.go @@ -2,6 +2,7 @@ package web import ( "mapserver/app" + "mapserver/vfs" "net/http" "strings" ) @@ -20,16 +21,20 @@ func (h *MediaHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) { } filename := parts[0] + fallback, hasfallback := req.URL.Query()["fallback"] 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 { resp.Write(content) resp.Header().Add("content-type", "image/png") - - } else { - resp.WriteHeader(404) - resp.Write([]byte(filename)) - + return } + + resp.WriteHeader(404) + resp.Write([]byte(filename)) }