mirror of
https://codeberg.org/PLG-Development/PLG-MuDiCS
synced 2026-07-05 16:37:09 +00:00
refactor: move browser open into shared modules
This commit is contained in:
+2
-1
@@ -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)
|
||||
|
||||
@@ -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
@@ -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
|
||||
|
||||
+1
-1
@@ -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)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package shared
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
)
|
||||
|
||||
//go:embed splash_screen.html
|
||||
var SplashScreenTemplate string
|
||||
+33
-3
@@ -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
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
Reference in New Issue
Block a user