mapserver/web/media.go

46 lines
907 B
Go
Raw Normal View History

2019-06-14 11:57:29 +03:00
package web
import (
"mapserver/app"
2021-04-12 14:03:10 +03:00
"mapserver/public"
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 {
2021-04-12 14:03:10 +03:00
var err error
content, err = public.Files.ReadFile("pics/" + fallback[0])
if err != nil {
resp.WriteHeader(http.StatusInternalServerError)
return
}
2019-06-14 14:28:10 +03:00
}
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
}