mapserver/web/media.go

41 lines
844 B
Go
Raw Normal View History

2019-06-14 11:57:29 +03:00
package web
import (
2021-04-12 14:03:10 +03:00
"mapserver/public"
2019-06-14 11:57:29 +03:00
"net/http"
"strings"
)
2021-04-12 14:55:00 +03:00
func (api *Api) GetMedia(resp http.ResponseWriter, req *http.Request) {
2019-06-14 11:57:29 +03:00
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
2021-04-12 14:55:00 +03:00
content := api.Context.MediaRepo[filename]
2019-06-14 12:21:07 +03:00
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
}