1
0
forked from MTSR/mapserver

Merge branch 'es6-modules' of https://github.com/thomasrudin-mt/mapserver into es6-modules

This commit is contained in:
NatureFreshMilk 2019-06-17 07:06:05 +02:00
commit 7b4e9a9687
7 changed files with 149 additions and 1 deletions

View File

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

View File

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

View File

@ -145,7 +145,6 @@ func TestMapObjectsQueryWithAttribute(t *testing.T) {
}
}
func TestMapObjectsQueryWithAttributeIgnoreCase(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "TestMapObjects.*.sqlite")
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
}

63
media/scan_recursive.go Normal file
View File

@ -0,0 +1,63 @@
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)
return nil
}
// 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

@ -25,6 +25,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})