From 644107f612b6c037b77c6852aabccdafb30b0174 Mon Sep 17 00:00:00 2001 From: NatureFreshMilk Date: Fri, 14 Jun 2019 10:57:29 +0200 Subject: [PATCH 1/2] mediahandler stub --- media/repository.go | 40 ++++++++++++++++++++++++++ media/repository_test.go | 17 +++++++++++ media/scan_recursive.go | 62 ++++++++++++++++++++++++++++++++++++++++ web/media.go | 26 +++++++++++++++++ web/serve.go | 1 + 5 files changed, 146 insertions(+) create mode 100644 media/repository.go create mode 100644 media/repository_test.go create mode 100644 media/scan_recursive.go create mode 100644 web/media.go diff --git a/media/repository.go b/media/repository.go new file mode 100644 index 0000000..8911afb --- /dev/null +++ b/media/repository.go @@ -0,0 +1,40 @@ +package media + +import ( + "io/ioutil" + "os" + "strings" +) + +func ScanDir(repo map[string][]byte, path string, ignore []string) error { + _, files := scan_recursive(path, ignore) + + for _, filename := range files { + if strings.HasSuffix(filename, ".png") { + + file, err := os.Open(filename) + + if err != nil { + return err + } + + content, err := ioutil.ReadAll(file) + + if err != nil { + return err + } + + simplefilename := filename + lastSlashIndex := strings.LastIndex(filename, "/") + + if lastSlashIndex >= 0 { + simplefilename = filename[lastSlashIndex+1:] + } + + repo[simplefilename] = content + } + + } + + return nil +} diff --git a/media/repository_test.go b/media/repository_test.go new file mode 100644 index 0000000..8967602 --- /dev/null +++ b/media/repository_test.go @@ -0,0 +1,17 @@ +package media + +import ( + "fmt" + "testing" +) + +func TestNew(t *testing.T) { + + repo := make(map[string][]byte) + + ScanDir(repo, "../", []string{"mapserver.tiles", ".git"}) + + for key := range repo { + fmt.Println(key) + } +} diff --git a/media/scan_recursive.go b/media/scan_recursive.go new file mode 100644 index 0000000..19fe412 --- /dev/null +++ b/media/scan_recursive.go @@ -0,0 +1,62 @@ +package media + +import ( + "log" + "os" + "path/filepath" + "strings" +) + +// https://gist.github.com/mustafaydemir/c90db8fcefeb4eb89696e6ccb5b28685 +func scan_recursive(dir_path string, ignore []string) ([]string, []string) { + + folders := []string{} + files := []string{} + + // Scan + filepath.Walk(dir_path, func(path string, f os.FileInfo, err error) error { + + _continue := false + + // Loop : Ignore Files & Folders + for _, i := range ignore { + + // If ignored path + if strings.Index(path, i) != -1 { + + // Continue + _continue = true + } + } + + if _continue == false { + + f, err = os.Stat(path) + + // If no error + if err != nil { + log.Fatal(err) + } + + // File & Folder Mode + f_mode := f.Mode() + + // Is folder + if f_mode.IsDir() { + + // Append to Folders Array + folders = append(folders, path) + + // Is file + } else if f_mode.IsRegular() { + + // Append to Files Array + files = append(files, path) + } + } + + return nil + }) + + return folders, files +} diff --git a/web/media.go b/web/media.go new file mode 100644 index 0000000..f9b13ff --- /dev/null +++ b/web/media.go @@ -0,0 +1,26 @@ +package web + +import ( + "mapserver/app" + "net/http" + "strings" +) + +type MediaHandler struct { + ctx *app.App +} + +func (h *MediaHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) { + str := strings.TrimPrefix(req.URL.Path, "/api/media/") + parts := strings.Split(str, "/") + if len(parts) != 1 { + resp.WriteHeader(500) + resp.Write([]byte("wrong number of arguments")) + return + } + + filename := parts[0] + + resp.WriteHeader(500) + resp.Write([]byte(filename)) +} diff --git a/web/serve.go b/web/serve.go index 5996b06..abee49e 100644 --- a/web/serve.go +++ b/web/serve.go @@ -30,6 +30,7 @@ func Serve(ctx *app.App) { tiles.Init() mux.Handle("/api/tile/", tiles) mux.Handle("/api/config", &ConfigHandler{ctx: ctx}) + mux.Handle("/api/media", &MediaHandler{ctx: ctx}) mux.Handle("/api/minetest", &Minetest{ctx: ctx}) mux.Handle("/api/mapobjects/", &MapObjects{ctx: ctx}) From fbf2c1c1907b7350eaa2857bfca625e263ea5575 Mon Sep 17 00:00:00 2001 From: NatureFreshMilk Date: Fri, 14 Jun 2019 11:21:07 +0200 Subject: [PATCH 2/2] /api/media/{filename} endpoint --- app/app.go | 2 ++ app/setup.go | 8 ++++++++ mapobjectdb/sqlite/mapobjects_test.go | 1 - web/media.go | 13 +++++++++++-- web/serve.go | 2 +- 5 files changed, 22 insertions(+), 4 deletions(-) diff --git a/app/app.go b/app/app.go index 20b9573..b9cb3aa 100644 --- a/app/app.go +++ b/app/app.go @@ -28,5 +28,7 @@ type App struct { Mapblockrenderer *mapblockrenderer.MapBlockRenderer Tilerenderer *tilerenderer.TileRenderer + MediaRepo map[string][]byte + WebEventbus *eventbus.Eventbus } diff --git a/app/setup.go b/app/setup.go index e0672c0..1f6652f 100644 --- a/app/setup.go +++ b/app/setup.go @@ -9,6 +9,7 @@ import ( "mapserver/mapblockrenderer" postgresobjdb "mapserver/mapobjectdb/postgres" sqliteobjdb "mapserver/mapobjectdb/sqlite" + "mapserver/media" "mapserver/params" "mapserver/settings" "mapserver/tiledb" @@ -143,5 +144,12 @@ func Setup(p params.ParamsType, cfg *Config) *App { a.Config.Layers, ) + //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") + + a.MediaRepo = repo + return &a } diff --git a/mapobjectdb/sqlite/mapobjects_test.go b/mapobjectdb/sqlite/mapobjects_test.go index bf60b82..9b468b7 100644 --- a/mapobjectdb/sqlite/mapobjects_test.go +++ b/mapobjectdb/sqlite/mapobjects_test.go @@ -145,7 +145,6 @@ func TestMapObjectsQueryWithAttribute(t *testing.T) { } } - func TestMapObjectsQueryWithAttributeIgnoreCase(t *testing.T) { tmpfile, err := ioutil.TempFile("", "TestMapObjects.*.sqlite") if err != nil { diff --git a/web/media.go b/web/media.go index f9b13ff..849aaac 100644 --- a/web/media.go +++ b/web/media.go @@ -21,6 +21,15 @@ func (h *MediaHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) { filename := parts[0] - resp.WriteHeader(500) - resp.Write([]byte(filename)) + content := h.ctx.MediaRepo[filename] + + if content != nil { + resp.Write(content) + resp.Header().Add("content-type", "image/png") + + } else { + resp.WriteHeader(404) + resp.Write([]byte(filename)) + + } } diff --git a/web/serve.go b/web/serve.go index abee49e..c94dc87 100644 --- a/web/serve.go +++ b/web/serve.go @@ -30,7 +30,7 @@ func Serve(ctx *app.App) { tiles.Init() mux.Handle("/api/tile/", tiles) mux.Handle("/api/config", &ConfigHandler{ctx: ctx}) - mux.Handle("/api/media", &MediaHandler{ctx: ctx}) + mux.Handle("/api/media/", &MediaHandler{ctx: ctx}) mux.Handle("/api/minetest", &Minetest{ctx: ctx}) mux.Handle("/api/mapobjects/", &MapObjects{ctx: ctx})