From 8bcbd877e8736813a7b02beb78aa6d6f3b3bf823 Mon Sep 17 00:00:00 2001 From: NatureFreshMilk Date: Fri, 14 Jun 2019 13:13:21 +0200 Subject: [PATCH 1/2] show repo size of media catalog --- app/setup.go | 8 ++++++-- media/repository.go | 11 +++++++---- 2 files changed, 13 insertions(+), 6 deletions(-) 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 } From 54804b9c3d4039c42d0cb4054669e30a043e576d Mon Sep 17 00:00:00 2001 From: NatureFreshMilk Date: Fri, 14 Jun 2019 13:28:10 +0200 Subject: [PATCH 2/2] fallback media url --- web/media.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) 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)) }