1
0
forked from MTSR/mapserver

Merge branch 'master' into es6-modules

This commit is contained in:
Thomas Rudin 2019-06-16 12:16:58 +02:00
commit 44022e4160
8 changed files with 165 additions and 1 deletions

View File

@ -28,5 +28,7 @@ type App struct {
Mapblockrenderer *mapblockrenderer.MapBlockRenderer Mapblockrenderer *mapblockrenderer.MapBlockRenderer
Tilerenderer *tilerenderer.TileRenderer Tilerenderer *tilerenderer.TileRenderer
MediaRepo map[string][]byte
WebEventbus *eventbus.Eventbus WebEventbus *eventbus.Eventbus
} }

View File

@ -9,6 +9,7 @@ import (
"mapserver/mapblockrenderer" "mapserver/mapblockrenderer"
postgresobjdb "mapserver/mapobjectdb/postgres" postgresobjdb "mapserver/mapobjectdb/postgres"
sqliteobjdb "mapserver/mapobjectdb/sqlite" sqliteobjdb "mapserver/mapobjectdb/sqlite"
"mapserver/media"
"mapserver/params" "mapserver/params"
"mapserver/settings" "mapserver/settings"
"mapserver/tiledb" "mapserver/tiledb"
@ -143,5 +144,12 @@ func Setup(p params.ParamsType, cfg *Config) *App {
a.Config.Layers, 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 return &a
} }

View File

@ -145,7 +145,6 @@ func TestMapObjectsQueryWithAttribute(t *testing.T) {
} }
} }
func TestMapObjectsQueryWithAttributeIgnoreCase(t *testing.T) { func TestMapObjectsQueryWithAttributeIgnoreCase(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "TestMapObjects.*.sqlite") tmpfile, err := ioutil.TempFile("", "TestMapObjects.*.sqlite")
if err != nil { if err != nil {

40
media/repository.go Normal file
View File

@ -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
}

17
media/repository_test.go Normal file
View File

@ -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)
}
}

62
media/scan_recursive.go Normal file
View File

@ -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
}

35
web/media.go Normal file
View File

@ -0,0 +1,35 @@
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]
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))
}
}

View File

@ -26,6 +26,7 @@ func Serve(ctx *app.App) {
tiles.Init() tiles.Init()
mux.Handle("/api/tile/", tiles) mux.Handle("/api/tile/", tiles)
mux.Handle("/api/config", &ConfigHandler{ctx: ctx}) mux.Handle("/api/config", &ConfigHandler{ctx: ctx})
mux.Handle("/api/media/", &MediaHandler{ctx: ctx})
mux.Handle("/api/minetest", &Minetest{ctx: ctx}) mux.Handle("/api/minetest", &Minetest{ctx: ctx})
mux.Handle("/api/mapobjects/", &MapObjects{ctx: ctx}) mux.Handle("/api/mapobjects/", &MapObjects{ctx: ctx})