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"
|
2020-11-01 00:35:02 +13:00
|
|
|
"net/http"
|
|
|
|
|
2021-01-30 08:05:13 +13:00
|
|
|
"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 {
|
2021-01-29 10:51:55 +13:00
|
|
|
Text string `json:"text,omitempty"`
|
|
|
|
HTML string `json:"html,omitempty"`
|
2020-11-01 00:35:02 +13:00
|
|
|
}
|
|
|
|
|
2021-01-30 08:05:13 +13:00
|
|
|
func (h *RoomHandler) clipboardGetText(w http.ResponseWriter, r *http.Request) {
|
|
|
|
data, err := h.desktop.ClipboardGetText()
|
2021-01-29 09:12:35 +13:00
|
|
|
if err != nil {
|
|
|
|
utils.HttpInternalServerError(w, err)
|
|
|
|
return
|
|
|
|
}
|
2020-11-01 00:35:02 +13:00
|
|
|
|
2020-11-19 09:56:42 +13:00
|
|
|
utils.HttpSuccess(w, ClipboardPayload{
|
2021-01-30 08:05:13 +13:00
|
|
|
Text: data.Text,
|
|
|
|
HTML: data.HTML,
|
2020-11-01 00:35:02 +13:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-30 08:05:13 +13:00
|
|
|
func (h *RoomHandler) clipboardSetText(w http.ResponseWriter, r *http.Request) {
|
2021-01-29 10:51:55 +13:00
|
|
|
data := &ClipboardPayload{}
|
|
|
|
if !utils.HttpJsonRequest(w, r, data) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-30 08:05:13 +13:00
|
|
|
err := h.desktop.ClipboardSetText(types.ClipboardText{
|
|
|
|
Text: data.Text,
|
|
|
|
HTML: data.HTML,
|
2021-01-29 10:51:55 +13:00
|
|
|
})
|
2020-11-01 00:35:02 +13:00
|
|
|
|
2021-01-29 09:12:35 +13:00
|
|
|
if err != nil {
|
|
|
|
utils.HttpInternalServerError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-15 05:51:18 +13:00
|
|
|
utils.HttpSuccess(w)
|
2020-11-01 00:35:02 +13:00
|
|
|
}
|
2021-01-29 12:03:22 +13:00
|
|
|
|
|
|
|
func (h *RoomHandler) clipboardGetImage(w http.ResponseWriter, r *http.Request) {
|
|
|
|
bytes, err := h.desktop.ClipboardGetBinary("image/png")
|
|
|
|
if err != nil {
|
|
|
|
utils.HttpInternalServerError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
|
|
|
|
w.Header().Set("Content-Type", "image/png")
|
|
|
|
//nolint
|
|
|
|
w.Write(bytes)
|
|
|
|
}
|
|
|
|
|
2021-02-02 06:29:39 +13:00
|
|
|
/* TODO: Unused now.
|
2021-01-29 12:03:22 +13:00
|
|
|
func (h *RoomHandler) clipboardSetImage(w http.ResponseWriter, r *http.Request) {
|
|
|
|
err := r.ParseMultipartForm(MAX_UPLOAD_SIZE)
|
|
|
|
if err != nil {
|
|
|
|
utils.HttpBadRequest(w, "Failed to parse multipart form.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//nolint
|
|
|
|
defer r.MultipartForm.RemoveAll()
|
|
|
|
|
|
|
|
file, header, err := r.FormFile("file")
|
|
|
|
if err != nil {
|
|
|
|
utils.HttpBadRequest(w, "No file received.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
mime := header.Header.Get("Content-Type")
|
|
|
|
if !strings.HasPrefix(mime, "image/") {
|
|
|
|
utils.HttpBadRequest(w, "File must be image.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer := new(bytes.Buffer)
|
2021-02-02 06:29:39 +13:00
|
|
|
_, err = buffer.ReadFrom(file)
|
|
|
|
if err != nil {
|
|
|
|
utils.HttpInternalServerError(w, err)
|
|
|
|
return
|
|
|
|
}
|
2021-01-29 12:03:22 +13:00
|
|
|
|
|
|
|
err = h.desktop.ClipboardSetBinary("image/png", buffer.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
utils.HttpInternalServerError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
utils.HttpSuccess(w)
|
|
|
|
}
|
2021-02-02 06:29:39 +13:00
|
|
|
|
|
|
|
func (h *RoomHandler) clipboardGetTargets(w http.ResponseWriter, r *http.Request) {
|
|
|
|
targets, err := h.desktop.ClipboardGetTargets()
|
|
|
|
if err != nil {
|
|
|
|
utils.HttpInternalServerError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
utils.HttpSuccess(w, targets)
|
|
|
|
}
|
|
|
|
*/
|