neko/internal/api/room/clipboard.go

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