From 013a3ca4878e1def31b43647bfc8a98bf37106b2 Mon Sep 17 00:00:00 2001 From: 2mal3 <56305732+2mal3@users.noreply.github.com> Date: Wed, 3 Dec 2025 16:18:14 +0100 Subject: [PATCH] refactor: use mime types for file identification --- display/go.mod | 1 + display/go.sum | 2 ++ display/web/main.go | 22 ++++++++++++++-------- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/display/go.mod b/display/go.mod index d4936df..80f594c 100644 --- a/display/go.mod +++ b/display/go.mod @@ -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 ( diff --git a/display/go.sum b/display/go.sum index f669e4e..a26e451 100644 --- a/display/go.sum +++ b/display/go.sum @@ -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= diff --git a/display/web/main.go b/display/web/main.go index 7bc84ac..809bb66 100644 --- a/display/web/main.go +++ b/display/web/main.go @@ -14,8 +14,8 @@ import ( "os/exec" "path/filepath" shared "plg-mudics/shared" - "strings" + "github.com/gabriel-vasile/mimetype" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" "github.com/skip2/go-qrcode" @@ -306,23 +306,29 @@ func openFileRoute(ctx echo.Context) error { 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": + case "application/vnd.openxmlformats-officedocument.presentationml.presentation", "application/vnd.oasis.opendocument.presentation": err = pkg.OpenPresentation(fullPath) - case ".pdf": + case "application/pdf": err = pkg.OpenPDF(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 {