databag/net/server/internal/api_getChannelSubjectField.go

39 lines
890 B
Go
Raw Normal View History

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