mirror of
https://codeberg.org/PLG-Development/PLG-MuDiCS
synced 2026-07-06 17:07:08 +00:00
Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| cfb26a0b47 | |||
| df1ea7b163 | |||
| 013a3ca487 | |||
| 4cf0f12a6f | |||
| 9cafb293d0 | |||
| 78fde49329 | |||
| a7184847bb | |||
| fe847339e4 | |||
| 84c01d791a | |||
| 54902357b0 | |||
| 88b80fab58 |
@@ -10,6 +10,7 @@ Open source solution for advanced remote control and coordination of one or more
|
||||
- Bash
|
||||
- Chromium
|
||||
- LibreOffice (optional, presentation view)
|
||||
- Xreader (optional, pdf view)
|
||||
- ImageMagick (optional, image preview)
|
||||
- ffmpeg (optional, video preview)
|
||||
- Ghostscript (optional, pdf preview)
|
||||
|
||||
@@ -7,6 +7,7 @@ require (
|
||||
github.com/labstack/echo/v4 v4.13.4
|
||||
github.com/micmonay/keybd_event v1.1.2
|
||||
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
|
||||
github.com/gabriel-vasile/mimetype v1.4.11
|
||||
)
|
||||
|
||||
require (
|
||||
|
||||
@@ -14,6 +14,8 @@ github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
|
||||
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
|
||||
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
|
||||
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
|
||||
github.com/gabriel-vasile/mimetype v1.4.11 h1:AQvxbp830wPhHTqc1u7nzoLT+ZFxGY7emj5DR5DYFik=
|
||||
github.com/gabriel-vasile/mimetype v1.4.11/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/labstack/echo/v4 v4.13.4 h1:oTZZW+T3s9gAu5L8vmzihV7/lkXGZuITzTQkTEhcXEA=
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"plg-mudics/shared"
|
||||
"syscall"
|
||||
|
||||
"github.com/gabriel-vasile/mimetype"
|
||||
)
|
||||
|
||||
var FileHandler fileHandler = fileHandler{}
|
||||
|
||||
type fileHandler struct {
|
||||
runningProgram *exec.Cmd
|
||||
}
|
||||
|
||||
func (fh *fileHandler) OpenFile(path string) error {
|
||||
var err error
|
||||
|
||||
mType, err := mimetype.DetectFile(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to detect mime type: %w", err)
|
||||
}
|
||||
|
||||
tempDirPath, err := os.MkdirTemp("", "plg-mudics-program-profile-")
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create temporary profile directory: %w", err)
|
||||
}
|
||||
|
||||
err = fh.CloseRunningProgram()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch mType.String() {
|
||||
case "application/vnd.openxmlformats-officedocument.presentationml.presentation", "application/vnd.oasis.opendocument.presentation":
|
||||
fh.runningProgram = exec.Command("soffice", "--show", path, "--nologo", "--view", "--norestore", fmt.Sprintf("-env:UserInstallation=file://%s", tempDirPath))
|
||||
case "application/pdf":
|
||||
fh.runningProgram = exec.Command("xreader", path, "--presentation")
|
||||
default:
|
||||
return fmt.Errorf("unsupported file type: %s", mType.String())
|
||||
}
|
||||
|
||||
fh.runningProgram.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
||||
|
||||
result := shared.RunShellCommandNonBlocking(fh.runningProgram)
|
||||
if result.ExitCode != 0 {
|
||||
return fmt.Errorf("could not open pdf: %s (%d)", result.Stderr, result.ExitCode)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (fh *fileHandler) CloseRunningProgram() error {
|
||||
if fh.runningProgram == nil {
|
||||
return nil
|
||||
}
|
||||
err := syscall.Kill(-fh.runningProgram.Process.Pid, syscall.SIGTERM)
|
||||
fh.runningProgram = nil
|
||||
return err
|
||||
}
|
||||
@@ -47,15 +47,6 @@ func GetDeviceMac() (string, error) {
|
||||
return "", fmt.Errorf("no suitable MAC address found")
|
||||
}
|
||||
|
||||
func OpenPresentation(path string) error {
|
||||
cmd := exec.Command("bash", "-c", "-r", fmt.Sprintf("soffice --show %s -nologo -norestore", path))
|
||||
result := shared.RunShellCommand(cmd)
|
||||
if result.ExitCode != 0 {
|
||||
return errors.New(result.Stderr)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type KeyAction int
|
||||
|
||||
const (
|
||||
|
||||
+57
-34
@@ -14,12 +14,10 @@ import (
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
shared "plg-mudics/shared"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gabriel-vasile/mimetype"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
"github.com/micmonay/keybd_event"
|
||||
"github.com/skip2/go-qrcode"
|
||||
|
||||
"plg-mudics/display/pkg"
|
||||
@@ -224,10 +222,13 @@ func keyboardInputRoute(ctx echo.Context) error {
|
||||
}
|
||||
|
||||
func uploadFileRoute(ctx echo.Context) error {
|
||||
var err error
|
||||
|
||||
fullPath := ctx.Get("fullPath").(string)
|
||||
|
||||
// Ensure parent directories exist
|
||||
if err := os.MkdirAll(filepath.Dir(fullPath), os.ModePerm); err != nil {
|
||||
slog.Error("Failed to create storage path", "error", err, "path", fullPath)
|
||||
return ctx.JSON(http.StatusInternalServerError, shared.ErrorResponse{Description: "Failed to prepare storage directory"})
|
||||
}
|
||||
|
||||
@@ -235,12 +236,30 @@ func uploadFileRoute(ctx echo.Context) error {
|
||||
return ctx.JSON(http.StatusConflict, shared.ErrorResponse{Description: "File already exists"})
|
||||
}
|
||||
|
||||
data, err := io.ReadAll(ctx.Request().Body)
|
||||
file, err := os.Create(fullPath)
|
||||
if err != nil {
|
||||
return ctx.JSON(http.StatusBadRequest, shared.ErrorResponse{Description: shared.BadRequestDescription})
|
||||
slog.Error("Failed to create file", "file", fullPath, "error", err)
|
||||
return ctx.JSON(http.StatusInternalServerError, shared.ErrorResponse{Description: "Failed to create file"})
|
||||
}
|
||||
defer func() {
|
||||
fileCloseErr := file.Close()
|
||||
if fileCloseErr != nil {
|
||||
slog.Error("Failed to close file", "file", fullPath, "error", fileCloseErr)
|
||||
}
|
||||
if err != nil {
|
||||
os.Remove(fullPath)
|
||||
}
|
||||
}()
|
||||
|
||||
_, err = io.Copy(file, ctx.Request().Body)
|
||||
if err != nil {
|
||||
slog.Error("Failed to write file", "file", fullPath, "error", err)
|
||||
return ctx.JSON(http.StatusInternalServerError, shared.ErrorResponse{Description: "Failed to write file"})
|
||||
}
|
||||
|
||||
if err := os.WriteFile(fullPath, data, os.ModePerm); err != nil {
|
||||
err = file.Sync() // ensure data is flushed to disk
|
||||
if err != nil {
|
||||
slog.Error("Failed to sync file to disk", "file", fullPath, "error", err)
|
||||
return ctx.JSON(http.StatusInternalServerError, shared.ErrorResponse{Description: "Failed to save file"})
|
||||
}
|
||||
|
||||
@@ -255,17 +274,21 @@ func downloadFileRoute(ctx echo.Context) error {
|
||||
return ctx.JSON(http.StatusNotFound, shared.ErrorResponse{Description: "File not found"})
|
||||
}
|
||||
|
||||
err := ctx.File(fullPath)
|
||||
if err != nil {
|
||||
slog.Error("Failed to serve file", "file", fullPath, "error", err)
|
||||
return ctx.JSON(http.StatusInternalServerError, shared.ErrorResponse{Description: "Failed to serve file"})
|
||||
}
|
||||
slog.Info("Serving file for download", "path", fullPath)
|
||||
|
||||
slog.Info("File downloaded successfully", "path", fullPath)
|
||||
return nil
|
||||
file, err := os.Open(fullPath)
|
||||
if err != nil {
|
||||
slog.Error("Failed to open file", "file", fullPath, "error", err)
|
||||
return ctx.JSON(http.StatusInternalServerError, shared.ErrorResponse{Description: "Failed to open file"})
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
return ctx.Stream(http.StatusOK, "application/octet-stream", file)
|
||||
}
|
||||
|
||||
func openFileRoute(ctx echo.Context) error {
|
||||
var err error
|
||||
|
||||
pathParam := ctx.Param("path")
|
||||
fullPath := ctx.Get("fullPath").(string)
|
||||
|
||||
@@ -277,31 +300,38 @@ func openFileRoute(ctx echo.Context) error {
|
||||
return ctx.JSON(http.StatusInternalServerError, shared.ErrorResponse{Description: "Cant connect to display browser client"})
|
||||
}
|
||||
|
||||
err := resetView()
|
||||
err = resetView()
|
||||
if err != nil {
|
||||
slog.Error("Failed to reset view", "error", err)
|
||||
return ctx.JSON(http.StatusInternalServerError, shared.ErrorResponse{Description: "Failed to reset view"})
|
||||
}
|
||||
|
||||
ext := strings.ToLower(filepath.Ext(fullPath))
|
||||
switch ext {
|
||||
case ".mp4":
|
||||
mType, err := mimetype.DetectFile(fullPath)
|
||||
if err != nil {
|
||||
slog.Error("Failed to detect mime type", "file", pathParam, "error", err)
|
||||
return ctx.JSON(http.StatusInternalServerError, shared.ErrorResponse{Description: "Failed to detect file type"})
|
||||
}
|
||||
|
||||
switch mType.String() {
|
||||
case "video/mp4":
|
||||
var templateBuffer bytes.Buffer
|
||||
videoTemplate(pathParam).Render(context.Background(), &templateBuffer)
|
||||
|
||||
sseConnection <- templateBuffer.String()
|
||||
case ".jpg", ".jpeg", ".png", ".gif":
|
||||
case "image/jpeg", "image/png", "image/gif":
|
||||
var templateBuffer bytes.Buffer
|
||||
imageTemplate(pathParam).Render(context.Background(), &templateBuffer)
|
||||
sseConnection <- templateBuffer.String()
|
||||
case ".pptx", ".odp":
|
||||
err := pkg.OpenPresentation(fullPath)
|
||||
if err != nil {
|
||||
slog.Error("Failed to open presentation", "file", pathParam, "error", err)
|
||||
return ctx.JSON(http.StatusInternalServerError, shared.ErrorResponse{Description: "Failed to open presentation"})
|
||||
}
|
||||
case "application/pdf", "application/vnd.openxmlformats-officedocument.presentationml.presentation", "application/vnd.oasis.opendocument.presentation":
|
||||
err = pkg.FileHandler.OpenFile(fullPath)
|
||||
default:
|
||||
return ctx.JSON(http.StatusUnsupportedMediaType, shared.ErrorResponse{Description: "Unsupported file type"})
|
||||
slog.Info("Unsupported file type", "type", mType)
|
||||
return ctx.JSON(http.StatusUnsupportedMediaType, shared.ErrorResponse{Description: "Unsupported file type: " + mType.String()})
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
slog.Error("Failed to open file", "file", pathParam, "error", err)
|
||||
return ctx.JSON(http.StatusInternalServerError, shared.ErrorResponse{Description: "Failed to open file"})
|
||||
}
|
||||
|
||||
slog.Info("Successfully run file", "file", pathParam)
|
||||
@@ -377,16 +407,9 @@ func previewRoute(ctx echo.Context) error {
|
||||
|
||||
// Reset previous file views so they dont collide with the new one
|
||||
func resetView() error {
|
||||
var err error
|
||||
|
||||
err = pkg.KeyboardInput(keybd_event.VK_ESC, pkg.KeyPress)
|
||||
err := pkg.FileHandler.CloseRunningProgram()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to send ESC key: %w", err)
|
||||
}
|
||||
time.Sleep(400 * time.Millisecond)
|
||||
err = pkg.KeyboardInput(keybd_event.VK_ESC, pkg.KeyRelease)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to send ESC key: %w", err)
|
||||
return fmt.Errorf("failed to close running program: %w", err)
|
||||
}
|
||||
|
||||
sseConnection <- ""
|
||||
|
||||
@@ -12,6 +12,8 @@
|
||||
boot.loader.systemd-boot.enable = true;
|
||||
boot.loader.efi.canTouchEfiVariables = true;
|
||||
|
||||
hardware.steam-hardware.enable = true;
|
||||
|
||||
networking.networkmanager.enable = true;
|
||||
|
||||
nix.settings.experimental-features = ["nix-command" "flakes"];
|
||||
@@ -87,6 +89,9 @@
|
||||
xfce.thunar-archive-plugin
|
||||
git
|
||||
nushell
|
||||
unzip
|
||||
iputils
|
||||
xreader
|
||||
|
||||
# Libraries
|
||||
imagemagick
|
||||
@@ -98,7 +103,7 @@
|
||||
wantedBy = ["multi-user.target"];
|
||||
after = ["network-online.target"];
|
||||
wants = ["network-online.target"];
|
||||
path = with pkgs; [nushell unzip iputils];
|
||||
path = config.environment.systemPackages;
|
||||
script = "nu ${./update.sh}";
|
||||
serviceConfig = {
|
||||
WorkingDirectory = "/home/mudics/mudics";
|
||||
@@ -111,7 +116,7 @@
|
||||
wantedBy = ["default.target"];
|
||||
after = ["update-mudics.service" "graphical.target"];
|
||||
wants = ["graphical.target"];
|
||||
path = with pkgs; [ungoogled-chromium];
|
||||
path = config.environment.systemPackages;
|
||||
script = "./plg-mudics-display";
|
||||
serviceConfig = {
|
||||
WorkingDirectory = "/home/mudics/mudics";
|
||||
|
||||
+8
-8
@@ -8,14 +8,14 @@
|
||||
nixpkgs,
|
||||
...
|
||||
} @ inputs: {
|
||||
nixosConfigurations.plg-mudics = nixpkgs.lib.nixosSystem {
|
||||
system = "x86_64-linux";
|
||||
specialArgs = {
|
||||
inherit inputs;
|
||||
};
|
||||
modules = [
|
||||
./configuration.nix
|
||||
];
|
||||
nixosConfigurations.mudics = nixpkgs.lib.nixosSystem {
|
||||
system = "x86_64-linux";
|
||||
specialArgs = {
|
||||
inherit inputs;
|
||||
};
|
||||
modules = [
|
||||
./configuration.nix
|
||||
];
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
@@ -2,8 +2,10 @@ package shared
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"os/exec"
|
||||
"time"
|
||||
)
|
||||
|
||||
type CommandResponse struct {
|
||||
@@ -24,6 +26,34 @@ func RunShellCommand(cmd *exec.Cmd) CommandResponse {
|
||||
cmd.Stderr = &stderr
|
||||
err := cmd.Run()
|
||||
|
||||
return parseCmdResult(cmd, stdout, stderr, err)
|
||||
|
||||
}
|
||||
|
||||
func RunShellCommandNonBlocking(cmd *exec.Cmd) CommandResponse {
|
||||
var stdout, stderr bytes.Buffer
|
||||
cmd.Stdout = &stdout
|
||||
cmd.Stderr = &stderr
|
||||
cmd.Start()
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
|
||||
defer cancel()
|
||||
|
||||
done := make(chan error, 1)
|
||||
|
||||
go func() {
|
||||
done <- cmd.Wait()
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return CommandResponse{}
|
||||
case err := <-done:
|
||||
return parseCmdResult(cmd, stdout, stderr, err)
|
||||
}
|
||||
}
|
||||
|
||||
func parseCmdResult(cmd *exec.Cmd, stdout, stderr bytes.Buffer, err error) CommandResponse {
|
||||
commandOutput := CommandResponse{
|
||||
Stdout: stdout.String(),
|
||||
Stderr: stderr.String(),
|
||||
|
||||
Reference in New Issue
Block a user