mirror of
https://codeberg.org/PLG-Development/PLG-MuDiCS
synced 2026-07-06 00:47:09 +00:00
feat(display): screenshot api
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
|||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"github.com/micmonay/keybd_event"
|
"github.com/micmonay/keybd_event"
|
||||||
@@ -83,6 +84,7 @@ func main() {
|
|||||||
apiGroup.PATCH("/shellCommand", shellCommandRoute)
|
apiGroup.PATCH("/shellCommand", shellCommandRoute)
|
||||||
apiGroup.PATCH("/keyboardInput", keyboardInputRoute)
|
apiGroup.PATCH("/keyboardInput", keyboardInputRoute)
|
||||||
apiGroup.PATCH("/showHTML", showHTMLRoute)
|
apiGroup.PATCH("/showHTML", showHTMLRoute)
|
||||||
|
apiGroup.PATCH("/takeScreenshot", takeScreenshotRoute)
|
||||||
|
|
||||||
fileGroup := apiGroup.Group("/file")
|
fileGroup := apiGroup.Group("/file")
|
||||||
fileGroup.Use(extractFilePathMiddleware)
|
fileGroup.Use(extractFilePathMiddleware)
|
||||||
@@ -431,6 +433,35 @@ func resetView() error {
|
|||||||
return nil
|
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 {
|
func runShellCommand(cmd *exec.Cmd) CommandResponse {
|
||||||
var stdout, stderr bytes.Buffer
|
var stdout, stderr bytes.Buffer
|
||||||
cmd.Stdout = &stdout
|
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