From 8f60041b2db9774945b7f4f6525b876b15b39aa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Mon, 18 Jan 2021 10:34:33 +0100 Subject: [PATCH] fix dialog close and add API. --- internal/api/room/handler.go | 3 ++- internal/api/room/upload.go | 17 +++++++++++++++-- internal/desktop/filechooser.go | 28 ++++++++++++++++------------ internal/types/desktop.go | 2 +- 4 files changed, 34 insertions(+), 16 deletions(-) diff --git a/internal/api/room/handler.go b/internal/api/room/handler.go index 247d3a14..310b1817 100644 --- a/internal/api/room/handler.go +++ b/internal/api/room/handler.go @@ -69,7 +69,8 @@ func (h *RoomHandler) Route(r chi.Router) { r.With(h.uploadMiddleware).Route("/upload", func(r chi.Router) { r.Post("/drop", h.uploadDrop) - r.Post("/dialog", h.uploadDialog) + r.Post("/dialog", h.uploadDialogPost) + r.Delete("/dialog", h.uploadDialogClose) }) } diff --git a/internal/api/room/upload.go b/internal/api/room/upload.go index cec1d8e3..6e8a098c 100644 --- a/internal/api/room/upload.go +++ b/internal/api/room/upload.go @@ -87,8 +87,7 @@ func (h *RoomHandler) uploadDrop(w http.ResponseWriter, r *http.Request) { utils.HttpSuccess(w) } - -func (h *RoomHandler) uploadDialog(w http.ResponseWriter, r *http.Request) { +func (h *RoomHandler) uploadDialogPost(w http.ResponseWriter, r *http.Request) { r.ParseMultipartForm(MAX_UPLOAD_SIZE) if r.MultipartForm == nil { @@ -148,3 +147,17 @@ func (h *RoomHandler) uploadDialog(w http.ResponseWriter, r *http.Request) { utils.HttpSuccess(w) } + +func (h *RoomHandler) uploadDialogClose(w http.ResponseWriter, r *http.Request) { + if !h.desktop.IsFileChooserDialogOpen() { + utils.HttpBadRequest(w, "File chooser dialog is not open.") + return + } + + if !h.desktop.CloseFileChooserDialog() { + utils.HttpInternalServerError(w, "Unable to close file chooser dialog.") + return + } + + utils.HttpSuccess(w) +} diff --git a/internal/desktop/filechooser.go b/internal/desktop/filechooser.go index 9fc25af8..df2922c5 100644 --- a/internal/desktop/filechooser.go +++ b/internal/desktop/filechooser.go @@ -27,20 +27,24 @@ func (manager *DesktopManagerCtx) HandleFileChooserDialog(uri string) error { return err } -func (manager *DesktopManagerCtx) CloseFileChooserDialog() error { - mu.Lock() - defer mu.Unlock() +func (manager *DesktopManagerCtx) CloseFileChooserDialog() bool { + for i := 0; i < 5; i++ { + // TOOD: Use native API. + mu.Lock() + exec.Command( + "xdotool", + "search", "--name", "Open", "windowfocus", + "sleep", "0.2", + "key", "--clearmodifiers", "alt+F4", + ).Output() + mu.Unlock() - // TOOD: Use native API. - cmd := exec.Command( - "xdotool", - "search", "--name", "Open", "windowfocus", - "sleep", "0.2", - "key", "--clearmodifiers", "alt+f4", - ) + if !manager.IsFileChooserDialogOpen() { + return true + } + } - _, err := cmd.Output() - return err + return false } func (manager *DesktopManagerCtx) IsFileChooserDialogOpen() bool { diff --git a/internal/types/desktop.go b/internal/types/desktop.go index 7fa60538..50333d20 100644 --- a/internal/types/desktop.go +++ b/internal/types/desktop.go @@ -69,6 +69,6 @@ type DesktopManager interface { // filechooser HandleFileChooserDialog(uri string) error - CloseFileChooserDialog() error + CloseFileChooserDialog() bool IsFileChooserDialogOpen() bool }