mapserver/web/media.go

41 lines
811 B
Go
Raw Normal View History

2019-06-14 11:57:29 +03:00
package web
import (
"mapserver/app"
2019-06-14 14:28:10 +03:00
"mapserver/vfs"
2019-06-14 11:57:29 +03:00
"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]
2019-06-14 14:28:10 +03:00
fallback, hasfallback := req.URL.Query()["fallback"]
2019-06-14 11:57:29 +03:00
2019-06-14 12:21:07 +03:00
content := h.ctx.MediaRepo[filename]
2019-06-14 14:28:10 +03:00
if content == nil && hasfallback && len(fallback) > 0 {
content, _ = vfs.FSByte(h.ctx.Config.Webdev, "/pics/"+fallback[0])
}
2019-06-14 12:21:07 +03:00
if content != nil {
resp.Write(content)
resp.Header().Add("content-type", "image/png")
2019-06-14 14:28:10 +03:00
return
2019-06-14 12:21:07 +03:00
}
2019-06-14 14:28:10 +03:00
resp.WriteHeader(404)
resp.Write([]byte(filename))
2019-06-14 11:57:29 +03:00
}