neko/internal/api/room/clipboard.go

108 lines
2.6 KiB
Go
Raw Normal View History

2020-10-29 22:23:30 +01:00
package room
2020-10-31 12:35:02 +01:00
import (
2021-02-01 18:29:39 +01:00
// TODO: Unused now.
//"bytes"
//"strings"
2021-09-17 00:58:50 +02:00
2020-10-31 12:35:02 +01:00
"net/http"
"github.com/demodesk/neko/pkg/types"
"github.com/demodesk/neko/pkg/utils"
2020-10-31 12:35:02 +01:00
)
2020-11-18 21:56:42 +01:00
type ClipboardPayload struct {
Text string `json:"text,omitempty"`
HTML string `json:"html,omitempty"`
2020-10-31 12:35:02 +01:00
}
2021-09-17 00:58:50 +02:00
func (h *RoomHandler) clipboardGetText(w http.ResponseWriter, r *http.Request) error {
data, err := h.desktop.ClipboardGetText()
2021-01-28 21:12:35 +01:00
if err != nil {
2021-09-17 00:58:50 +02:00
return utils.HttpInternalServerError().WithInternalErr(err)
2021-01-28 21:12:35 +01:00
}
2020-10-31 12:35:02 +01:00
2021-09-17 00:58:50 +02:00
return utils.HttpSuccess(w, ClipboardPayload{
Text: data.Text,
HTML: data.HTML,
2020-10-31 12:35:02 +01:00
})
}
2021-09-17 00:58:50 +02:00
func (h *RoomHandler) clipboardSetText(w http.ResponseWriter, r *http.Request) error {
data := &ClipboardPayload{}
2021-09-17 00:58:50 +02:00
if err := utils.HttpJsonRequest(w, r, data); err != nil {
return err
}
err := h.desktop.ClipboardSetText(types.ClipboardText{
Text: data.Text,
HTML: data.HTML,
})
2020-10-31 12:35:02 +01:00
2021-01-28 21:12:35 +01:00
if err != nil {
2021-09-17 00:58:50 +02:00
return utils.HttpInternalServerError().WithInternalErr(err)
2021-01-28 21:12:35 +01:00
}
2021-09-17 00:58:50 +02:00
return utils.HttpSuccess(w)
2020-10-31 12:35:02 +01:00
}
2021-01-29 00:03:22 +01:00
2021-09-17 00:58:50 +02:00
func (h *RoomHandler) clipboardGetImage(w http.ResponseWriter, r *http.Request) error {
2021-01-29 00:03:22 +01:00
bytes, err := h.desktop.ClipboardGetBinary("image/png")
if err != nil {
2021-09-17 00:58:50 +02:00
return utils.HttpInternalServerError().WithInternalErr(err)
2021-01-29 00:03:22 +01:00
}
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Set("Content-Type", "image/png")
2021-09-17 00:58:50 +02:00
_, err = w.Write(bytes)
return err
2021-01-29 00:03:22 +01:00
}
2021-02-01 18:29:39 +01:00
/* TODO: Unused now.
2021-09-17 00:58:50 +02:00
func (h *RoomHandler) clipboardSetImage(w http.ResponseWriter, r *http.Request) error {
2021-01-29 00:03:22 +01:00
err := r.ParseMultipartForm(MAX_UPLOAD_SIZE)
if err != nil {
2021-09-17 00:58:50 +02:00
return utils.HttpBadRequest("failed to parse multipart form").WithInternalErr(err)
2021-01-29 00:03:22 +01:00
}
//nolint
defer r.MultipartForm.RemoveAll()
file, header, err := r.FormFile("file")
if err != nil {
2021-09-17 00:58:50 +02:00
return utils.HttpBadRequest("no file received").WithInternalErr(err)
2021-01-29 00:03:22 +01:00
}
defer file.Close()
mime := header.Header.Get("Content-Type")
if !strings.HasPrefix(mime, "image/") {
2021-09-17 00:58:50 +02:00
return utils.HttpBadRequest("file must be image")
2021-01-29 00:03:22 +01:00
}
buffer := new(bytes.Buffer)
2021-02-01 18:29:39 +01:00
_, err = buffer.ReadFrom(file)
if err != nil {
2021-09-17 00:58:50 +02:00
return utils.HttpInternalServerError().WithInternalErr(err).WithInternalMsg("unable to read from uploaded file")
2021-02-01 18:29:39 +01:00
}
2021-01-29 00:03:22 +01:00
err = h.desktop.ClipboardSetBinary("image/png", buffer.Bytes())
if err != nil {
2021-09-17 00:58:50 +02:00
return utils.HttpInternalServerError().WithInternalErr(err).WithInternalMsg("unable set image to clipboard")
2021-01-29 00:03:22 +01:00
}
2021-09-17 00:58:50 +02:00
return utils.HttpSuccess(w)
2021-01-29 00:03:22 +01:00
}
2021-02-01 18:29:39 +01:00
2021-09-17 00:58:50 +02:00
func (h *RoomHandler) clipboardGetTargets(w http.ResponseWriter, r *http.Request) error {
2021-02-01 18:29:39 +01:00
targets, err := h.desktop.ClipboardGetTargets()
if err != nil {
2021-09-17 00:58:50 +02:00
return utils.HttpInternalServerError().WithInternalErr(err)
2021-02-01 18:29:39 +01:00
}
2021-09-17 00:58:50 +02:00
return utils.HttpSuccess(w, targets)
2021-02-01 18:29:39 +01:00
}
2021-09-17 00:58:50 +02:00
2021-02-01 18:29:39 +01:00
*/