mirror of
https://codeberg.org/PLG-Development/PLG-MuDiCS
synced 2026-07-05 16:37:09 +00:00
chore: more error handling
This commit is contained in:
+17
-6
@@ -42,8 +42,12 @@ func (b *BrowserType) Init() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BrowserType) OpenPage(url string) {
|
func (b *BrowserType) OpenPage(url string) error {
|
||||||
chromedp.Run(b.Ctx, chromedp.Navigate(url))
|
err := chromedp.Run(b.Ctx, chromedp.Navigate(url))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("navigate to URL: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Yes, we need that trick with creating a temp file and not directly sending html since
|
// Yes, we need that trick with creating a temp file and not directly sending html since
|
||||||
@@ -55,18 +59,25 @@ func (b *BrowserType) OpenHTML(html string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not create tempfile: %w", err)
|
return fmt.Errorf("could not create tempfile: %w", err)
|
||||||
}
|
}
|
||||||
defer tempFile.Close()
|
defer func() { _ = tempFile.Close() }()
|
||||||
|
|
||||||
_, err = tempFile.WriteString(html)
|
_, err = tempFile.WriteString(html)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not write to tempfile: %w", err)
|
return fmt.Errorf("could not write to tempfile: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
chromedp.Run(b.Ctx, chromedp.Navigate("file://"+tempFile.Name()))
|
err = chromedp.Run(b.Ctx, chromedp.Navigate("file://"+tempFile.Name()))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("navigate to URL: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BrowserType) OpenPDF(path string) {
|
func (b *BrowserType) OpenPDF(path string) error {
|
||||||
b.OpenPage("file://" + path + "#toolbar=0&view=Fit")
|
err := b.OpenPage("file://" + path + "#toolbar=0&view=Fit")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("open PDF: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-3
@@ -17,7 +17,6 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Failed to get storage path", "error", err)
|
slog.Error("Failed to get storage path", "error", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
port := "1323"
|
port := "1323"
|
||||||
|
|
||||||
@@ -25,8 +24,16 @@ func main() {
|
|||||||
// and since its the last action in the main go func all other goroutines (e.g. the webserver) are killed
|
// and since its the last action in the main go func all other goroutines (e.g. the webserver) are killed
|
||||||
go web.StartWebServer(port)
|
go web.StartWebServer(port)
|
||||||
|
|
||||||
browser.Browser.Init()
|
err = browser.Browser.Init()
|
||||||
pkg.OpenStartScreen()
|
if err != nil {
|
||||||
|
slog.Error("Initialize browser", "error", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
err = pkg.OpenStartScreen()
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("Open start screen", "error", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
defer browser.Browser.Cancel()
|
defer browser.Browser.Cancel()
|
||||||
<-browser.Browser.Ctx.Done()
|
<-browser.Browser.Ctx.Done()
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -83,7 +83,7 @@ func ShowHTML(html string) error {
|
|||||||
ResetView()
|
ResetView()
|
||||||
|
|
||||||
var templateBuffer bytes.Buffer
|
var templateBuffer bytes.Buffer
|
||||||
htmlTemplate(html).Render(context.Background(), &templateBuffer)
|
_ = htmlTemplate(html).Render(context.Background(), &templateBuffer)
|
||||||
err := browser.Browser.OpenHTML(templateBuffer.String())
|
err := browser.Browser.OpenHTML(templateBuffer.String())
|
||||||
|
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -27,26 +27,30 @@ func OpenFile(path string) error {
|
|||||||
|
|
||||||
mType, err := mimetype.DetectFile(path)
|
mType, err := mimetype.DetectFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Failed to detect mime type", "file", path, "error", err)
|
return fmt.Errorf("detect mime type of file %q: %w", path, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch mType.String() {
|
switch mType.String() {
|
||||||
case "video/mp4":
|
case "video/mp4":
|
||||||
var templateBuffer bytes.Buffer
|
var templateBuffer bytes.Buffer
|
||||||
videoTemplate(path).Render(context.Background(), &templateBuffer)
|
_ = videoTemplate(path).Render(context.Background(), &templateBuffer)
|
||||||
browser.Browser.OpenHTML(templateBuffer.String())
|
err = browser.Browser.OpenHTML(templateBuffer.String())
|
||||||
case "image/jpeg", "image/png", "image/gif":
|
case "image/jpeg", "image/png", "image/gif":
|
||||||
var templateBuffer bytes.Buffer
|
var templateBuffer bytes.Buffer
|
||||||
imageTemplate(path).Render(context.Background(), &templateBuffer)
|
_ = imageTemplate(path).Render(context.Background(), &templateBuffer)
|
||||||
browser.Browser.OpenHTML(templateBuffer.String())
|
err = browser.Browser.OpenHTML(templateBuffer.String())
|
||||||
case "application/pdf":
|
case "application/pdf":
|
||||||
browser.Browser.OpenPDF(path)
|
err = browser.Browser.OpenPDF(path)
|
||||||
case "application/vnd.openxmlformats-officedocument.presentationml.presentation", "application/vnd.oasis.opendocument.presentation":
|
case "application/vnd.openxmlformats-officedocument.presentationml.presentation", "application/vnd.oasis.opendocument.presentation":
|
||||||
err = fileHandler.openFileWithApp(path)
|
err = fileHandler.openFileWithApp(path)
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("unsupported file type: %s", mType.String())
|
return fmt.Errorf("unsupported file type: %s", mType.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to open file: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"image/color"
|
"image/color"
|
||||||
"log/slog"
|
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"plg-mudics/shared"
|
"plg-mudics/shared"
|
||||||
@@ -16,7 +15,7 @@ import (
|
|||||||
"github.com/skip2/go-qrcode"
|
"github.com/skip2/go-qrcode"
|
||||||
)
|
)
|
||||||
|
|
||||||
func OpenStartScreen() {
|
func OpenStartScreen() error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
raw := shared.RawSplashScreenTemplate
|
raw := shared.RawSplashScreenTemplate
|
||||||
@@ -24,11 +23,11 @@ func OpenStartScreen() {
|
|||||||
|
|
||||||
ip, err := getDeviceIp()
|
ip, err := getDeviceIp()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Failed to get device IP", "error", err)
|
return fmt.Errorf("get device IP: %w", err)
|
||||||
}
|
}
|
||||||
mac, err := getDeviceMac()
|
mac, err := getDeviceMac()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Failed to get device MAC address", "error", err)
|
return fmt.Errorf("get device MAC address: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
port := 8080
|
port := 8080
|
||||||
@@ -37,13 +36,17 @@ func OpenStartScreen() {
|
|||||||
if showQrCode {
|
if showQrCode {
|
||||||
qrCodePath, err = generateQRCode(fmt.Sprintf("http://%s:%d", ip, port))
|
qrCodePath, err = generateQRCode(fmt.Sprintf("http://%s:%d", ip, port))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("could not generate qr code", "error", err)
|
return fmt.Errorf("generate QR code: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var templateBuffer bytes.Buffer
|
var templateBuffer bytes.Buffer
|
||||||
startScreenTemplate(html, ip, mac, qrCodePath).Render(context.Background(), &templateBuffer)
|
_ = startScreenTemplate(html, ip, mac, qrCodePath).Render(context.Background(), &templateBuffer)
|
||||||
browser.Browser.OpenHTML(templateBuffer.String())
|
err = browser.Browser.OpenHTML(templateBuffer.String())
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("open start screen in browser: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getDeviceIp() (string, error) {
|
func getDeviceIp() (string, error) {
|
||||||
@@ -106,7 +109,7 @@ func generateQRCode(data string) (string, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("could not save qr code: %w", err)
|
return "", fmt.Errorf("could not save qr code: %w", err)
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer func() { _ = file.Close() }()
|
||||||
|
|
||||||
_, err = file.Write(png)
|
_, err = file.Write(png)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
+11
-2
@@ -162,7 +162,10 @@ func uploadFileRoute(ctx echo.Context) error {
|
|||||||
slog.Error("Failed to close file", "file", fullPath, "error", fileCloseErr)
|
slog.Error("Failed to close file", "file", fullPath, "error", fileCloseErr)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
os.Remove(fullPath)
|
err = os.Remove(fullPath)
|
||||||
|
if err != nil {
|
||||||
|
slog.Warn("could not remove broken file", "file", fullPath, "error", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@@ -280,6 +283,8 @@ func previewRoute(ctx echo.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func openWebsiteRoute(ctx echo.Context) error {
|
func openWebsiteRoute(ctx echo.Context) error {
|
||||||
|
var err error
|
||||||
|
|
||||||
var request struct {
|
var request struct {
|
||||||
URL string `json:"url"`
|
URL string `json:"url"`
|
||||||
}
|
}
|
||||||
@@ -290,7 +295,11 @@ func openWebsiteRoute(ctx echo.Context) error {
|
|||||||
|
|
||||||
slog.Info("Opening url")
|
slog.Info("Opening url")
|
||||||
|
|
||||||
browser.Browser.OpenPage(request.URL)
|
err = browser.Browser.OpenPage(request.URL)
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("Failed to open website", "url", request.URL, "error", err)
|
||||||
|
return ctx.JSON(http.StatusInternalServerError, shared.ErrorResponse{Description: "Failed to open website"})
|
||||||
|
}
|
||||||
|
|
||||||
return ctx.JSON(http.StatusOK, struct{}{})
|
return ctx.JSON(http.StatusOK, struct{}{})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,25 @@
|
|||||||
github.com/a-h/htmlformat v0.0.0-20250209131833-673be874c677/go.mod h1:FMIm5afKmEfarNbIXOaPHFY8X7fo+fRQB6I9MPG2nB0=
|
github.com/a-h/htmlformat v0.0.0-20250209131833-673be874c677/go.mod h1:FMIm5afKmEfarNbIXOaPHFY8X7fo+fRQB6I9MPG2nB0=
|
||||||
|
github.com/ledongthuc/pdf v0.0.0-20220302134840-0c2507a12d80 h1:6Yzfa6GP0rIo/kULo2bwGEkFvCePZ3qHDDTC3/J9Swo=
|
||||||
github.com/ledongthuc/pdf v0.0.0-20220302134840-0c2507a12d80/go.mod h1:imJHygn/1yfhB7XSJJKlFZKl/J+dCPAknuiaGOshXAs=
|
github.com/ledongthuc/pdf v0.0.0-20220302134840-0c2507a12d80/go.mod h1:imJHygn/1yfhB7XSJJKlFZKl/J+dCPAknuiaGOshXAs=
|
||||||
|
github.com/mdlayher/raw v0.0.0-20190313224157-43dbcdd7739d h1:rjAS0af7FIYCScTtEU5KjIldC6qVaEScUJhABHC+ccM=
|
||||||
|
github.com/orisano/pixelmatch v0.0.0-20220722002657-fb0b55479cde h1:x0TT0RDC7UhAVbbWWBzr41ElhJx5tXPWkIHA2HWPRuw=
|
||||||
github.com/orisano/pixelmatch v0.0.0-20220722002657-fb0b55479cde/go.mod h1:nZgzbfBr3hhjoZnS66nKrHmduYNpc34ny7RK4z5/HM0=
|
github.com/orisano/pixelmatch v0.0.0-20220722002657-fb0b55479cde/go.mod h1:nZgzbfBr3hhjoZnS66nKrHmduYNpc34ny7RK4z5/HM0=
|
||||||
|
github.com/rs/cors v1.11.0 h1:0B9GE/r9Bc2UxRMMtymBkHTenPkHDv0CW4Y98GBY+po=
|
||||||
github.com/rs/cors v1.11.0/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
|
github.com/rs/cors v1.11.0/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
|
||||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||||
|
github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE=
|
||||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||||
golang.org/x/mod v0.25.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww=
|
golang.org/x/mod v0.25.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww=
|
||||||
golang.org/x/net v0.41.0/go.mod h1:B/K4NNqkfmg07DQYrbwvSluqCJOOXwUjeb/5lOisjbA=
|
golang.org/x/net v0.41.0/go.mod h1:B/K4NNqkfmg07DQYrbwvSluqCJOOXwUjeb/5lOisjbA=
|
||||||
golang.org/x/net v0.47.0/go.mod h1:/jNxtkgq5yWUGYkaZGqo27cfGZ1c5Nen03aYrrKpVRU=
|
golang.org/x/net v0.47.0/go.mod h1:/jNxtkgq5yWUGYkaZGqo27cfGZ1c5Nen03aYrrKpVRU=
|
||||||
golang.org/x/telemetry v0.0.0-20240521205824-bda55230c457/go.mod h1:pRgIJT+bRLFKnoM1ldnzKoxTIn14Yxz928LQRYYgIN0=
|
golang.org/x/telemetry v0.0.0-20240521205824-bda55230c457/go.mod h1:pRgIJT+bRLFKnoM1ldnzKoxTIn14Yxz928LQRYYgIN0=
|
||||||
golang.org/x/telemetry v0.0.0-20250710130107-8d8967aff50b/go.mod h1:4ZwOYna0/zsOKwuR5X/m0QFOJpSZvAxFfkQT+Erd9D4=
|
golang.org/x/telemetry v0.0.0-20250710130107-8d8967aff50b/go.mod h1:4ZwOYna0/zsOKwuR5X/m0QFOJpSZvAxFfkQT+Erd9D4=
|
||||||
|
golang.org/x/telemetry v0.0.0-20251111182119-bc8e575c7b54 h1:E2/AqCUMZGgd73TQkxUMcMla25GB9i/5HOdLr+uH7Vo=
|
||||||
golang.org/x/telemetry v0.0.0-20251111182119-bc8e575c7b54/go.mod h1:hKdjCMrbv9skySur+Nek8Hd0uJ0GuxJIoIX2payrIdQ=
|
golang.org/x/telemetry v0.0.0-20251111182119-bc8e575c7b54/go.mod h1:hKdjCMrbv9skySur+Nek8Hd0uJ0GuxJIoIX2payrIdQ=
|
||||||
golang.org/x/term v0.32.0/go.mod h1:uZG1FhGx848Sqfsq4/DlJr3xGGsYMu/L5GW4abiaEPQ=
|
golang.org/x/term v0.32.0/go.mod h1:uZG1FhGx848Sqfsq4/DlJr3xGGsYMu/L5GW4abiaEPQ=
|
||||||
golang.org/x/term v0.33.0/go.mod h1:s18+ql9tYWp1IfpV9DmCtQDDSRBUjKaw9M1eAv5UeF0=
|
golang.org/x/term v0.33.0/go.mod h1:s18+ql9tYWp1IfpV9DmCtQDDSRBUjKaw9M1eAv5UeF0=
|
||||||
|
golang.org/x/term v0.38.0 h1:PQ5pkm/rLO6HnxFR7N2lJHOZX6Kez5Y1gDSJla6jo7Q=
|
||||||
golang.org/x/term v0.38.0/go.mod h1:bSEAKrOT1W+VSu9TSCMtoGEOUcKxOKgl3LE5QEF/xVg=
|
golang.org/x/term v0.38.0/go.mod h1:bSEAKrOT1W+VSu9TSCMtoGEOUcKxOKgl3LE5QEF/xVg=
|
||||||
golang.org/x/tools v0.34.0/go.mod h1:pAP9OwEaY1CAW3HOmg3hLZC5Z0CCmzjAF2UQMSqNARg=
|
golang.org/x/tools v0.34.0/go.mod h1:pAP9OwEaY1CAW3HOmg3hLZC5Z0CCmzjAF2UQMSqNARg=
|
||||||
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
|||||||
Reference in New Issue
Block a user