feat(display): screenshot api

This commit is contained in:
2025-10-15 16:02:25 +02:00
parent 2e0af663c4
commit bd70a35e5f
2 changed files with 46 additions and 0 deletions
+31
View File
@@ -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