neko/internal/api/room/clipboard.go

48 lines
1.0 KiB
Go
Raw Normal View History

2020-10-30 10:23:30 +13:00
package room
2020-11-01 00:35:02 +13:00
import (
"net/http"
"github.com/go-chi/render"
"demodesk/neko/internal/api/utils"
)
type ClipboardData struct {
Text string `json:"text"`
}
func (a *ClipboardData) Bind(r *http.Request) error {
// Bind will run after the unmarshalling is complete, its a
// good time to focus some post-processing after a decoding.
return nil
}
func (a *ClipboardData) Render(w http.ResponseWriter, r *http.Request) error {
// Pre-processing before a response is marshalled and sent
// across the wire
return nil
}
func (h *RoomHandler) ClipboardRead(w http.ResponseWriter, r *http.Request) {
// TODO: error check?
2020-11-02 04:54:06 +13:00
text := h.desktop.ReadClipboard()
2020-11-01 00:35:02 +13:00
render.JSON(w, r, ClipboardData{
Text: text,
})
}
func (h *RoomHandler) ClipboardWrite(w http.ResponseWriter, r *http.Request) {
data := &ClipboardData{}
if err := render.Bind(r, data); err != nil {
2020-11-01 09:58:57 +13:00
_ = render.Render(w, r, utils.ErrBadRequest(err))
2020-11-01 00:35:02 +13:00
return
}
// TODO: error check?
2020-11-02 04:54:06 +13:00
h.desktop.WriteClipboard(data.Text)
2020-11-01 00:35:02 +13:00
w.WriteHeader(http.StatusNoContent)
}