databag/net/server/internal/api_getChannelSubjectField.go

39 lines
845 B
Go
Raw Normal View History

2022-02-17 21:55:26 +00:00
package databag
import (
2022-03-02 07:52:37 +00:00
"time"
"bytes"
2022-02-17 21:55:26 +00:00
"strings"
"net/http"
"encoding/base64"
"github.com/gorilla/mux"
"github.com/valyala/fastjson"
)
func GetChannelSubjectField(w http.ResponseWriter, r *http.Request) {
// scan parameters
params := mux.Vars(r)
field := params["field"]
elements := strings.Split(field, ".")
2022-03-02 21:19:16 +00:00
// get channel stlot
channelSlot, _, err, code := getChannelSlot(r, false)
if err != nil {
ErrResponse(w, code, err)
2022-02-17 21:55:26 +00:00
return
}
// decode data
2022-03-02 21:19:16 +00:00
strData := fastjson.GetString([]byte(channelSlot.Channel.Data), elements...)
2022-02-17 21:55:26 +00:00
binData, err := base64.StdEncoding.DecodeString(strData)
if err != nil {
ErrResponse(w, http.StatusNotFound, err)
return
}
2022-03-02 07:52:37 +00:00
// response with content
2022-03-02 21:19:16 +00:00
http.ServeContent(w, r, field, time.Unix(channelSlot.Channel.Updated, 0), bytes.NewReader(binData))
2022-02-17 21:55:26 +00:00
}