refactor: move browser open into shared modules

This commit is contained in:
2025-11-04 17:21:23 +01:00
parent fa3d5198d2
commit 4ebac7d7fc
7 changed files with 91 additions and 56 deletions
+5 -4
View File
@@ -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
+5 -47
View File
@@ -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