join plain text and rich text to one struct.

This commit is contained in:
Miroslav Šedivý 2021-01-29 20:05:13 +01:00
parent 5c9a57ee91
commit 546cd608c3
6 changed files with 63 additions and 62 deletions

View File

@ -6,6 +6,7 @@ import (
"net/http" "net/http"
"demodesk/neko/internal/utils" "demodesk/neko/internal/utils"
"demodesk/neko/internal/types"
) )
type ClipboardPayload struct { type ClipboardPayload struct {
@ -23,52 +24,30 @@ func (h *RoomHandler) clipboardGetTargets(w http.ResponseWriter, r *http.Request
utils.HttpSuccess(w, targets) utils.HttpSuccess(w, targets)
} }
func (h *RoomHandler) clipboardGetPlainText(w http.ResponseWriter, r *http.Request) { func (h *RoomHandler) clipboardGetText(w http.ResponseWriter, r *http.Request) {
text, err := h.desktop.ClipboardGetPlainText() data, err := h.desktop.ClipboardGetText()
if err != nil { if err != nil {
utils.HttpInternalServerError(w, err) utils.HttpInternalServerError(w, err)
return return
} }
utils.HttpSuccess(w, ClipboardPayload{ utils.HttpSuccess(w, ClipboardPayload{
Text: text, Text: data.Text,
HTML: data.HTML,
}) })
} }
func (h *RoomHandler) clipboardSetPlainText(w http.ResponseWriter, r *http.Request) { func (h *RoomHandler) clipboardSetText(w http.ResponseWriter, r *http.Request) {
data := &ClipboardPayload{} data := &ClipboardPayload{}
if !utils.HttpJsonRequest(w, r, data) { if !utils.HttpJsonRequest(w, r, data) {
return return
} }
err := h.desktop.ClipboardSetPlainText(data.Text) err := h.desktop.ClipboardSetText(types.ClipboardText{
if err != nil { Text: data.Text,
utils.HttpInternalServerError(w, err) HTML: data.HTML,
return
}
utils.HttpSuccess(w)
}
func (h *RoomHandler) clipboardGetRichText(w http.ResponseWriter, r *http.Request) {
html, err := h.desktop.ClipboardGetRichText()
if err != nil {
utils.HttpInternalServerError(w, err)
return
}
utils.HttpSuccess(w, ClipboardPayload{
HTML: html,
}) })
}
func (h *RoomHandler) clipboardSetRichText(w http.ResponseWriter, r *http.Request) {
data := &ClipboardPayload{}
if !utils.HttpJsonRequest(w, r, data) {
return
}
err := h.desktop.ClipboardSetRichText(data.HTML)
if err != nil { if err != nil {
utils.HttpInternalServerError(w, err) utils.HttpInternalServerError(w, err)
return return

View File

@ -38,14 +38,18 @@ func (h *RoomHandler) Route(r chi.Router) {
}) })
r.With(auth.HostsOnly).Route("/clipboard", func(r chi.Router) { r.With(auth.HostsOnly).Route("/clipboard", func(r chi.Router) {
r.Get("/", h.clipboardGetPlainText) r.Get("/", h.clipboardGetText)
r.Post("/", h.clipboardSetPlainText) r.Post("/", h.clipboardSetText)
r.Get("/targets", h.clipboardGetTargets) r.Get("/image.png", h.clipboardGetImage)
r.Get("/html", h.clipboardGetRichText)
r.Post("/html", h.clipboardSetRichText) // TODO: Refactor. xclip is failing to set propper target type
r.Get("/image", h.clipboardGetImage) // and this content is sent back to client as text in another
// TODO: Refactor. // clipboard update. Therefore endpoint is not usable!
//r.Post("/image", h.clipboardSetImage) //r.Post("/image", h.clipboardSetImage)
// TODO: Refactor. If there would be implemented custom target
// retrieval, this endpoint would be useful.
//r.Get("/targets", h.clipboardGetTargets)
}) })
r.Route("/keyboard", func(r chi.Router) { r.Route("/keyboard", func(r chi.Router) {

View File

@ -5,8 +5,37 @@ import (
"bytes" "bytes"
"os/exec" "os/exec"
"strings" "strings"
"demodesk/neko/internal/types"
) )
func (manager *DesktopManagerCtx) ClipboardGetText() (*types.ClipboardText, error) {
text, err := manager.ClipboardGetBinary("STRING")
if err != nil {
return nil, err
}
// Rich text must not always be available, can fail silently.
html, _ := manager.ClipboardGetBinary("text/html")
return &types.ClipboardText{
Text: string(text),
HTML: string(html),
}, nil
}
func (manager *DesktopManagerCtx) ClipboardSetText(data types.ClipboardText) error {
// TODO: Refactor.
// Current implementation is unable to set multiple targets. HTML
// is set, if available. Otherwise plain text.
if data.HTML != "" {
return manager.ClipboardSetBinary("text/html", []byte(data.HTML))
}
return manager.ClipboardSetBinary("STRING", []byte(data.Text))
}
func (manager *DesktopManagerCtx) ClipboardGetBinary(mime string) ([]byte, error) { func (manager *DesktopManagerCtx) ClipboardGetBinary(mime string) ([]byte, error) {
cmd := exec.Command("xclip", "-selection", "clipboard", "-out", "-target", mime) cmd := exec.Command("xclip", "-selection", "clipboard", "-out", "-target", mime)
@ -77,21 +106,3 @@ func (manager *DesktopManagerCtx) ClipboardGetTargets() ([]string, error) {
return response, nil return response, nil
} }
func (manager *DesktopManagerCtx) ClipboardGetPlainText() (string, error) {
bytes, err := manager.ClipboardGetBinary("STRING")
return string(bytes), err
}
func (manager *DesktopManagerCtx) ClipboardSetPlainText(data string) error {
return manager.ClipboardSetBinary("STRING", []byte(data))
}
func (manager *DesktopManagerCtx) ClipboardGetRichText() (string, error) {
bytes, err := manager.ClipboardGetBinary("text/html")
return string(bytes), err
}
func (manager *DesktopManagerCtx) ClipboardSetRichText(data string) error {
return manager.ClipboardSetBinary("text/html", []byte(data))
}

View File

@ -35,6 +35,11 @@ type KeyboardMap struct {
Variant string Variant string
} }
type ClipboardText struct {
Text string
HTML string
}
type DesktopManager interface { type DesktopManager interface {
Start() Start()
Shutdown() error Shutdown() error
@ -67,13 +72,11 @@ type DesktopManager interface {
OnEventError(listener func(error_code uint8, message string, request_code uint8, minor_code uint8)) OnEventError(listener func(error_code uint8, message string, request_code uint8, minor_code uint8))
// clipboard // clipboard
ClipboardGetText() (*ClipboardText, error)
ClipboardSetText(data ClipboardText) error
ClipboardGetBinary(mime string) ([]byte, error) ClipboardGetBinary(mime string) ([]byte, error)
ClipboardSetBinary(mime string, data []byte) error ClipboardSetBinary(mime string, data []byte) error
ClipboardGetTargets() ([]string, error) ClipboardGetTargets() ([]string, error)
ClipboardGetPlainText() (string, error)
ClipboardSetPlainText(data string) error
ClipboardGetRichText() (string, error)
ClipboardSetRichText(data string) error
// drop // drop
DropFiles(x int, y int, files []string) bool DropFiles(x int, y int, files []string) bool

View File

@ -16,5 +16,8 @@ func (h *MessageHandlerCtx) clipboardSet(session types.Session, payload *message
return nil return nil
} }
return h.desktop.ClipboardSetPlainText(payload.Text) return h.desktop.ClipboardSetText(types.ClipboardText{
Text: payload.Text,
// TODO: Send HTML?
})
} }

View File

@ -117,14 +117,15 @@ func (ws *WebSocketManagerCtx) Start() {
return return
} }
text, err := ws.desktop.ClipboardGetPlainText() data, err := ws.desktop.ClipboardGetText()
if err != nil { if err != nil {
ws.logger.Warn().Err(err).Msg("could not get clipboard content") ws.logger.Warn().Err(err).Msg("could not get clipboard content")
} }
if err := session.Send(message.ClipboardData{ if err := session.Send(message.ClipboardData{
Event: event.CLIPBOARD_UPDATED, Event: event.CLIPBOARD_UPDATED,
Text: text, Text: data.Text,
// TODO: Send HTML?
}); err != nil { }); err != nil {
ws.logger.Warn().Err(err).Msg("could not sync clipboard") ws.logger.Warn().Err(err).Msg("could not sync clipboard")
} }