diff --git a/display/main.go b/display/main.go index f11ec6a..0a33cbe 100644 --- a/display/main.go +++ b/display/main.go @@ -13,6 +13,7 @@ import ( "os/exec" "path/filepath" "strings" + "time" "github.com/labstack/echo/v4" "github.com/micmonay/keybd_event" @@ -83,6 +84,7 @@ func main() { apiGroup.PATCH("/shellCommand", shellCommandRoute) apiGroup.PATCH("/keyboardInput", keyboardInputRoute) apiGroup.PATCH("/showHTML", showHTMLRoute) + apiGroup.PATCH("/takeScreenshot", takeScreenshotRoute) fileGroup := apiGroup.Group("/file") fileGroup.Use(extractFilePathMiddleware) @@ -431,6 +433,35 @@ func resetView() error { return nil } +func takeScreenshotRoute(ctx echo.Context) error { + var err error + + screenshotPath, err := takeScreenshot() + if err != nil { + slog.Error("Failed to take screenshot", "error", err) + return ctx.JSON(http.StatusInternalServerError, ErrorResponse{Error: "Internal server error"}) + } + + err = ctx.File(screenshotPath) + if err != nil { + slog.Error("Failed to serve file", "file", screenshotPath, "error", err) + return ctx.JSON(http.StatusInternalServerError, ErrorResponse{Error: "Internal server error"}) + } + + return nil +} + +func takeScreenshot() (string, error) { + tempFilePath := filepath.Join(os.TempDir(), fmt.Sprintf("screenshot_%d.png", time.Now().Unix())) + + cmd := exec.Command("gnome-screenshot", "-f", tempFilePath) + commandOutput := runShellCommand(cmd) + if commandOutput.ExitCode != 0 { + return "", errors.New(commandOutput.Stderr) + } + return tempFilePath, nil +} + func runShellCommand(cmd *exec.Cmd) CommandResponse { var stdout, stderr bytes.Buffer cmd.Stdout = &stdout diff --git a/display/requests/takeScreenshot.bru b/display/requests/takeScreenshot.bru new file mode 100644 index 0000000..df42665 --- /dev/null +++ b/display/requests/takeScreenshot.bru @@ -0,0 +1,15 @@ +meta { + name: takeScreenshot + type: http + seq: 8 +} + +patch { + url: 127.0.0.1:1323/api/takeScreenshot + body: none + auth: inherit +} + +settings { + encodeUrl: true +}