From 4ebac7d7fc6f0e4ae6c6c4f264f9afb0269e89bf Mon Sep 17 00:00:00 2001 From: 2mal3 <56305732+2mal3@users.noreply.github.com> Date: Tue, 4 Nov 2025 17:21:23 +0100 Subject: [PATCH] refactor: move browser open into shared modules --- display/main.go | 3 ++- display/pkg/file_preview.go | 9 ++++--- display/pkg/main.go | 52 ++++--------------------------------- display/web/main.go | 2 +- shared/embed.go | 8 ++++++ shared/main.go | 36 ++++++++++++++++++++++--- shared/open_browser.go | 37 ++++++++++++++++++++++++++ 7 files changed, 91 insertions(+), 56 deletions(-) create mode 100644 shared/embed.go create mode 100644 shared/open_browser.go diff --git a/display/main.go b/display/main.go index 93cd418..00d0992 100644 --- a/display/main.go +++ b/display/main.go @@ -6,6 +6,7 @@ import ( "plg-mudics/display/pkg" "plg-mudics/display/web" + "plg-mudics/shared" ) const VERSION = "0.1.0" @@ -25,7 +26,7 @@ func main() { go web.StartWebServer(VERSION) - err = pkg.OpenBrowserWindow("http://127.0.0.1:1323") + err = shared.OpenBrowserWindow("https://example.com", true, true) if err != nil { slog.Error("Failed to open browser window", "error", err) os.Exit(1) diff --git a/display/pkg/file_preview.go b/display/pkg/file_preview.go index 4fe1b21..f85f56b 100644 --- a/display/pkg/file_preview.go +++ b/display/pkg/file_preview.go @@ -6,6 +6,7 @@ import ( "os" "os/exec" "path/filepath" + "plg-mudics/shared" "strings" "time" ) @@ -36,7 +37,7 @@ func GenerateFilePreview(inputPath string) (string, error) { func generateImagePreview(inputPath string, outputPath string) error { cmd := exec.Command("magick", inputPath, "-thumbnail", "100x100", "-quality", "50", outputPath) - result := RunShellCommand(cmd) + result := shared.RunShellCommand(cmd) if result.ExitCode != 0 { if result.ExitCode == 127 { return ErrFilePreviewToolsMissing @@ -48,12 +49,12 @@ func generateImagePreview(inputPath string, outputPath string) error { func generatePDFPreview(inputPath string, outputPath string) error { testCmd := exec.Command("which", "gs") - if result := RunShellCommand(testCmd); result.ExitCode != 0 { + if result := shared.RunShellCommand(testCmd); result.ExitCode != 0 { return ErrFilePreviewToolsMissing } cmd := exec.Command("magick", fmt.Sprintf("%s[0]", inputPath), "-thumbnail", "100x100", "-quality", "50", outputPath) - result := RunShellCommand(cmd) + result := shared.RunShellCommand(cmd) if result.ExitCode != 0 { if result.ExitCode == 127 { return ErrFilePreviewToolsMissing @@ -67,7 +68,7 @@ func generateVideoPreview(inputPath string, outputPath string) error { tempFilePath := filepath.Join(os.TempDir(), fmt.Sprintf("preview_temp_%d.webp", time.Now().Unix())) ffmpegCmd := exec.Command("ffmpeg", "-i", inputPath, "-ss", "00:00:01.000", "-vframes", "1", tempFilePath) - result := RunShellCommand(ffmpegCmd) + result := shared.RunShellCommand(ffmpegCmd) if result.ExitCode != 0 { if result.ExitCode == 127 { return ErrFilePreviewToolsMissing diff --git a/display/pkg/main.go b/display/pkg/main.go index e01ebe2..f919791 100644 --- a/display/pkg/main.go +++ b/display/pkg/main.go @@ -1,7 +1,6 @@ package pkg import ( - "bytes" "errors" "fmt" "log/slog" @@ -12,14 +11,12 @@ import ( "strings" "time" + "plg-mudics/shared" + "github.com/micmonay/keybd_event" ) -type CommandResponse struct { - Stdout string `json:"stdout"` - Stderr string `json:"stderr"` - ExitCode int `json:"exitCode"` -} + func GetDeviceIp() (string, error) { addrs, err := net.InterfaceAddrs() @@ -54,7 +51,7 @@ func GetDeviceMac() (string, error) { func OpenPresentation(path string) error { cmd := exec.Command("bash", "-c", "-r", fmt.Sprintf("soffice --show %s -nologo -norestore", path)) - result := RunShellCommand(cmd) + result := shared.RunShellCommand(cmd) if result.ExitCode != 0 { return errors.New(result.Stderr) } @@ -84,7 +81,7 @@ func TakeScreenshot() (string, error) { exec.Command("spectacle", "--fullscreen", "--nonotify", "--background", "--output", tempFilePath), } for _, cmd := range cmds { - commandOutput := RunShellCommand(cmd) + commandOutput := shared.RunShellCommand(cmd) if commandOutput.ExitCode == 0 { return tempFilePath, nil } @@ -93,45 +90,6 @@ func TakeScreenshot() (string, error) { return "", errors.New("no screenshot utility found or all failed") } -func RunShellCommand(cmd *exec.Cmd) CommandResponse { - var stdout, stderr bytes.Buffer - cmd.Stdout = &stdout - cmd.Stderr = &stderr - err := cmd.Run() - - commandOutput := CommandResponse{ - Stdout: stdout.String(), - Stderr: stderr.String(), - ExitCode: cmd.ProcessState.ExitCode(), - } - if err != nil { - var exitErr *exec.ExitError - if errors.As(err, &exitErr) { - commandOutput.ExitCode = exitErr.ExitCode() - } else { - commandOutput.Stderr = err.Error() - } - } - - return commandOutput -} - -func OpenBrowserWindow(url string) error { - template := "%s --app='%s' --start-fullscreen --user-data-dir=$(mktemp -d) --autoplay-policy=no-user-gesture-required" - - cmds := []*exec.Cmd{ - exec.Command("bash", "-c", fmt.Sprintf(template, "chromium", url)), - exec.Command("bash", "-c", fmt.Sprintf(template, "chromium-browser", url)), - } - for _, cmd := range cmds { - commandOutput := RunShellCommand(cmd) - if commandOutput.ExitCode == 0 { - return nil - } - } - - return errors.New("chromium not found in PATH") -} func GetStoragePath() (string, error) { var storagePath string diff --git a/display/web/main.go b/display/web/main.go index 47c0026..416fdcb 100644 --- a/display/web/main.go +++ b/display/web/main.go @@ -155,7 +155,7 @@ func shellCommandRoute(ctx echo.Context) error { } cmd.Dir = storagePath - commandOutput := pkg.RunShellCommand(cmd) + commandOutput := shared.RunShellCommand(cmd) if commandOutput.ExitCode != 0 { slog.Error("Shell command execution error", "error", commandOutput.Stderr) } diff --git a/shared/embed.go b/shared/embed.go new file mode 100644 index 0000000..23e0f9a --- /dev/null +++ b/shared/embed.go @@ -0,0 +1,8 @@ +package shared + +import ( + _ "embed" +) + +//go:embed splash_screen.html +var SplashScreenTemplate string diff --git a/shared/main.go b/shared/main.go index 23e0f9a..ffdf305 100644 --- a/shared/main.go +++ b/shared/main.go @@ -1,8 +1,38 @@ package shared import ( - _ "embed" + "bytes" + "errors" + "os/exec" ) -//go:embed splash_screen.html -var SplashScreenTemplate string +type CommandResponse struct { + Stdout string `json:"stdout"` + Stderr string `json:"stderr"` + ExitCode int `json:"exitCode"` +} + + + +func RunShellCommand(cmd *exec.Cmd) CommandResponse { + var stdout, stderr bytes.Buffer + cmd.Stdout = &stdout + cmd.Stderr = &stderr + err := cmd.Run() + + commandOutput := CommandResponse{ + Stdout: stdout.String(), + Stderr: stderr.String(), + ExitCode: cmd.ProcessState.ExitCode(), + } + if err != nil { + var exitErr *exec.ExitError + if errors.As(err, &exitErr) { + commandOutput.ExitCode = exitErr.ExitCode() + } else { + commandOutput.Stderr = err.Error() + } + } + + return commandOutput +} \ No newline at end of file diff --git a/shared/open_browser.go b/shared/open_browser.go new file mode 100644 index 0000000..e2ff862 --- /dev/null +++ b/shared/open_browser.go @@ -0,0 +1,37 @@ +package shared + +import ( + "errors" + "fmt" + "os" + "os/exec" +) + + +func OpenBrowserWindow(url string, fullscreen bool, temp bool) error { + bins := []string{"chromium", "chromium-browser"} + + args := []string{fmt.Sprintf("--app=%s", url), "--autoplay-policy=no-user-gesture-required"} + if fullscreen { + args = append(args, "--start-fullscreen") + } + if temp { + tempDirPath, err := os.MkdirTemp("", "plg-mudics-browser-") + if err != nil { + return err + } + arg := fmt.Sprintf("--user-data-dir=%s", tempDirPath) + args = append(args, arg) + } + + for _, bin := range bins { + cmd := exec.Command(bin, args...) + fmt.Println(cmd) + commandOutput := RunShellCommand(cmd) + if commandOutput.ExitCode == 0 { + return nil + } + } + + return errors.New("chromium not found in PATH") +}