mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
xclip minor changes + image.
This commit is contained in:
parent
1e479dc03f
commit
5c9a57ee91
@ -1,6 +1,8 @@
|
||||
package room
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
"net/http"
|
||||
|
||||
"demodesk/neko/internal/utils"
|
||||
@ -74,3 +76,52 @@ func (h *RoomHandler) clipboardSetRichText(w http.ResponseWriter, r *http.Reques
|
||||
|
||||
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)
|
||||
}
|
||||
|
@ -43,6 +43,9 @@ func (h *RoomHandler) Route(r chi.Router) {
|
||||
r.Get("/targets", h.clipboardGetTargets)
|
||||
r.Get("/html", h.clipboardGetRichText)
|
||||
r.Post("/html", h.clipboardSetRichText)
|
||||
r.Get("/image", h.clipboardGetImage)
|
||||
// TODO: Refactor.
|
||||
//r.Post("/image", h.clipboardSetImage)
|
||||
})
|
||||
|
||||
r.Route("/keyboard", func(r chi.Router) {
|
||||
|
@ -8,7 +8,7 @@ import (
|
||||
)
|
||||
|
||||
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
|
||||
cmd.Stdout = &stdout
|
||||
@ -24,23 +24,32 @@ func (manager *DesktopManagerCtx) ClipboardGetBinary(mime string) ([]byte, error
|
||||
}
|
||||
|
||||
func (manager *DesktopManagerCtx) ClipboardSetBinary(mime string, data []byte) error {
|
||||
cmd := exec.Command("xclip", "-selection", "clipboard", "-i", "-t", mime)
|
||||
cmd.Stdin = bytes.NewReader(data)
|
||||
cmd := exec.Command("xclip", "-selection", "clipboard", "-in", "-target", mime)
|
||||
|
||||
var stderr bytes.Buffer
|
||||
cmd.Stderr = &stderr
|
||||
|
||||
err := cmd.Run()
|
||||
stdin, err := cmd.StdinPipe()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = cmd.Start()
|
||||
if err != nil {
|
||||
msg := strings.TrimSpace(string(stderr.Bytes()))
|
||||
return fmt.Errorf("%s", msg)
|
||||
}
|
||||
|
||||
stdin.Write(data)
|
||||
stdin.Close()
|
||||
|
||||
// TODO: Refactor.
|
||||
// cmd.Wait()
|
||||
return nil
|
||||
}
|
||||
|
||||
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
|
||||
cmd.Stdout = &stdout
|
||||
|
Loading…
Reference in New Issue
Block a user