1
0
forked from MTSR/mapserver

mediahandler stub

This commit is contained in:
NatureFreshMilk 2019-06-14 10:57:29 +02:00
parent 1938e59998
commit 644107f612
5 changed files with 146 additions and 0 deletions

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
}

26
web/media.go Normal file
View File

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

View File

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