mirror of
https://codeberg.org/PLG-Development/PLG-MuDiCS
synced 2026-07-05 16:37:09 +00:00
feat(display): screenshot api
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user