mediahandler stub
This commit is contained in:
parent
1938e59998
commit
644107f612
40
media/repository.go
Normal file
40
media/repository.go
Normal 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
17
media/repository_test.go
Normal 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
62
media/scan_recursive.go
Normal 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
26
web/media.go
Normal 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))
|
||||||
|
}
|
@ -30,6 +30,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})
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user