xclip minor changes + image.

This commit is contained in:
Miroslav Šedivý 2021-01-29 00:03:22 +01:00
parent 1e479dc03f
commit 5c9a57ee91
3 changed files with 68 additions and 5 deletions

View File

@ -1,6 +1,8 @@
package room package room
import ( import (
"bytes"
"strings"
"net/http" "net/http"
"demodesk/neko/internal/utils" "demodesk/neko/internal/utils"
@ -74,3 +76,52 @@ func (h *RoomHandler) clipboardSetRichText(w http.ResponseWriter, r *http.Reques
utils.HttpSuccess(w) utils.HttpSuccess(w)
} }
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)
}
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)
buffer.ReadFrom(file)
err = h.desktop.ClipboardSetBinary("image/png", buffer.Bytes())
if err != nil {
utils.HttpInternalServerError(w, err)
return
}
utils.HttpSuccess(w)
}

View File

@ -43,6 +43,9 @@ func (h *RoomHandler) Route(r chi.Router) {
r.Get("/targets", h.clipboardGetTargets) r.Get("/targets", h.clipboardGetTargets)
r.Get("/html", h.clipboardGetRichText) r.Get("/html", h.clipboardGetRichText)
r.Post("/html", h.clipboardSetRichText) r.Post("/html", h.clipboardSetRichText)
r.Get("/image", h.clipboardGetImage)
// TODO: Refactor.
//r.Post("/image", h.clipboardSetImage)
}) })
r.Route("/keyboard", func(r chi.Router) { r.Route("/keyboard", func(r chi.Router) {

View File

@ -8,7 +8,7 @@ import (
) )
func (manager *DesktopManagerCtx) ClipboardGetBinary(mime string) ([]byte, error) { func (manager *DesktopManagerCtx) ClipboardGetBinary(mime string) ([]byte, error) {
cmd := exec.Command("xclip", "-selection", "clipboard", "-o", "-t", mime) cmd := exec.Command("xclip", "-selection", "clipboard", "-out", "-target", mime)
var stdout, stderr bytes.Buffer var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout cmd.Stdout = &stdout
@ -24,23 +24,32 @@ func (manager *DesktopManagerCtx) ClipboardGetBinary(mime string) ([]byte, error
} }
func (manager *DesktopManagerCtx) ClipboardSetBinary(mime string, data []byte) error { func (manager *DesktopManagerCtx) ClipboardSetBinary(mime string, data []byte) error {
cmd := exec.Command("xclip", "-selection", "clipboard", "-i", "-t", mime) cmd := exec.Command("xclip", "-selection", "clipboard", "-in", "-target", mime)
cmd.Stdin = bytes.NewReader(data)
var stderr bytes.Buffer var stderr bytes.Buffer
cmd.Stderr = &stderr cmd.Stderr = &stderr
err := cmd.Run() stdin, err := cmd.StdinPipe()
if err != nil {
return err
}
err = cmd.Start()
if err != nil { if err != nil {
msg := strings.TrimSpace(string(stderr.Bytes())) msg := strings.TrimSpace(string(stderr.Bytes()))
return fmt.Errorf("%s", msg) return fmt.Errorf("%s", msg)
} }
stdin.Write(data)
stdin.Close()
// TODO: Refactor.
// cmd.Wait()
return nil return nil
} }
func (manager *DesktopManagerCtx) ClipboardGetTargets() ([]string, error) { func (manager *DesktopManagerCtx) ClipboardGetTargets() ([]string, error) {
cmd := exec.Command("xclip", "-selection", "clipboard", "-o", "-t", "TARGETS") cmd := exec.Command("xclip", "-selection", "clipboard", "-out", "-target", "TARGETS")
var stdout, stderr bytes.Buffer var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout cmd.Stdout = &stdout